Portal: http://www.lydsy.com/JudgeOnline/problem.php?id=1009
A good question, so that f (i, j) to indicate that the test number matches to I bit, the post J bit is an unlucky string of the first J-bit, then for each state, is a linear combination of the previous state, so you can use the matrix to accelerate.
#include <cstdio> #include <cstring>const int maxn = 1000000005, Maxm = 25;int n, m, p, MTX[MAXM][MAXM], trie[m AXM][10], FAIL[MAXM], TRANS[MAXM][MAXM], TEM[MAXM][MAXM], Ans;char unf[maxm];inline void mul (int aa[maxm][maxm], int ss[ MAXM][MAXM]) {memset ((void*) TEM, 0, sizeof tem); for (int i = 0; i < m; ++i) {for (int j = 0; j < m; ++j) {for (int k = 0; K < M; ++K) {Tem[i][j] = (Tem[i][j] + aa[i][k] * ss[k][j])% p;}} memcpy ((void*) AA, (void*) TEM, sizeof tem);} inline void poww (int mi) {int i;for (i = n; mi >> I & 1 ^ 1; i); memcpy ((void*) trans, (void*) MTX, sizeof MTX); for (~i; i) {mul (trans, trans), if (Mi >> I & 1) {Mul (trans, MTX);}} int main (void) {//freopen ("In.txt", "R", stdin), scanf ("%d%d%d", &n, &m, &p), scanf ("%s", UNF + 1); for (int i = 0; I < m; ++i) {trie[i][unf[i + 1]-' 0 '] = i + 1;} for (int i = 1, i <= m; ++i) {for (int j = 0; J <; ++j) {if (Trie[i][j]) {Fail[trie[i][j]] = trie[fail[i]][j];} else {Trie[i][j] = Trie[fail[i]][j];}}} for (int i = 0, i < m; ++i) {for (int j = 0; J < ++j) {++mtx[i][trie[i][j]];}} POWW (n); for (int i = 0; i < m; ++i) {ans = (ans + trans[0][i])% p;} printf ("%d\n", ans); return 0;}
_bzoj1009 [HNOI2008]GT Exam "Matrix acceleration DP"