Ah Seng ready to enroll GT test, ticket number is nn
Digits X1X2 ⋯ XnX1X2 ⋯ Xn
He does not want an unlucky number appears on the ticket number. His unlucky numbers A1A2 ⋯ AmA1A2 ⋯ Am
There mm
Bit, does not appear refers X1X2 ⋯ XnX1X2 ⋯ Xn
Not just in a period equal to A1A2 ⋯ AmA1A2 ⋯ Am
, A1A1
And X1X1
May 00
. Input format of the first input line n, m, Kn, m, K
. The next line of input mm
Bit unlucky number. Ah Seng wondered output format does not appear the unlucky number number number of species, the output module KK
Take the results of. Data range 0≤Xi, Ai≤90≤Xi, Ai≤9
,
1≤n≤1091≤n≤109
,
1≤m≤201≤m≤20
,
2≤K≤10002≤K≤1000
Sample input: 100. 3. 4
111
Output Sample: 81
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 25;
int n, m, mod;
char str[N];
int ne[N];
int a[N][N];
void mul(int c[][N], int a[][N], int b[][N]){
static int t[N][N];
memset(t, 0, sizeof t);
for (int i = 0; i < m ; i ++)
for (int j = 0; j < m ; j ++)
for (int k = 0; k < m; k ++)
t[i][j] = (t[i][j] + a[i][k] * b[k][j]) % mod;
memcpy(c, t, sizeof t);
}
int qmi(int k){
int f0[N][N] = {1};
while(k){
if (k & 1) mul(f0, f0, a);
mul(a, a, a);
k >>= 1;
}
int res = 0;
for (int i = 0; i < m; i ++) res = (res + f0[0][i]) % mod;
return res;
}
int main(){
cin >> n >> m >> mod;
cin >> str + 1;
for (int i = 2, j = 0; i <= m; i ++){
while(j && str[j + 1] != str[i]) j = ne[j];
if (str[j + 1] == str[i]) j ++;
ne[i] = j;
}
for (int j = 0; j < m; j ++)
for (int c = '0'; c <= '9'; c ++){
int k = j;
while(k && str[k + 1] != c) k = ne[k];
if (str[k + 1] == c) k ++;
if (k < m) a[j][k] ++;
}
cout << qmi(n) << endl;
return 0;
}