1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #define MAXN (1000000000 + 5) #define MAXM (20 + 5) using namespace std; struct matrix { int n, m, zh[MAXM][MAXM]; matrix() { memset(zh, 0, sizeof(zh)); } } m1, ma, f; int n, m, p, nex[MAXM], ans; char s[MAXM]; void init() { m1.n = m1.m = m; for (int i = 0; i < m; i++) m1.zh[i][i] = 1; f.n = 1, f.m = m; f.zh[0][0] = 1; for (int i = 1, j = 0; i < m; i++) { j = nex[i]; while (j && s[j] != s[i]) j = nex[j]; nex[i + 1] = (s[i] == s[j]) ? (j + 1) : 0; } ma.n = ma.m = m; for (int i = 0; i < m; i++) { for (int j = 0; j < 10; j++) { int ndq = i; while (ndq && s[ndq] - '0' != j) ndq = nex[ndq]; if (s[ndq] - '0' == j) ++ndq; ++ma.zh[i][ndq]; } } } matrix tim(matrix x, matrix y) { matrix re; re.n = x.n, re.m = y.m; for (int i = 0; i < re.n; i++) for (int j = 0; j < re.m; j++) for (int k = 0; k < x.m; k++) re.zh[i][j] += x.zh[i][k] * y.zh[k][j] % p, re.zh[i][j] %= p; return re; } matrix ksm(int b) { if (!b) return m1; matrix re = ksm(b >> 1); re = tim(re, re); if (b & 1) re = tim(re, ma); return re; } int main() { scanf("%d%d%d", &n, &m, &p); scanf("%s", s); init(); f = tim(f, ksm(n)); for (int i = 0; i < m; i++) ans += f.zh[0][i], ans %= p; printf("%d", ans); return 0; }
|