Description
Ashen ready to enroll in the GT exam, the ticket number is n-digit x1x2 .... Xn (0<=xi<=9), he does not want to appear on the ticket number of unlucky figures.
His unlucky math a1a2 ... AM (0<=ai<=9) has M-bit, does not appear to refer to x1x2 ... The xn does not have exactly a paragraph equal to A1A2 ... Am. A1 and X1 can think
0
Input
The first line enters N,m,k. The next line is to enter the number of M bits. n<=10^9,m<=20,k<=1000
Output
Ashen want to know how many kinds of numbers do not appear unlucky numbers, the result of output modulus K redundancy.
Sample Input4 3 100
111Sample OutputBayi
/* We use DP to solve this problem w[i,j] indicates the first position of the admission ticket, and the number of unlucky matches to the number of program J, the representation of this state can also be regarded as the current to the first position, the admission ticket after the J-bit is unlucky number of the former J-bit, the program number then our final ans=σw[n,i] 0& Lt;=i<=m-1 so we're going to consider how to transfer the assumed current to the first I bit, match the value to the J-bit, i.e. w[i,j] we have, we can enumerate what the i+1 bit is, and then through the KMP the next array can quickly get the current enumeration of bits that can be matched to the first few, Assuming can be matched to the P-bit, then we w[i+1,p]+=w[i,j], so we can transfer but we see the data range of n is 10^9, so recursion is not complete, this time need to observe the law we found that the transfer of p,j and I is not related, that is, whether I is a few, w[i, J] Fixed will be added to w[i+1,k] so we change the way of transfer, before is updated with W[i,j] w[i,p], now we can write W[i,j]=a0*w[i-1,0]+a1*w[i-1,1]+......+a (m-1) *w[i-1,m-1 ] and the AI array is constant, then this formula is "linear homogeneous recurrence of constant coefficients", which can be optimized with matrix multiplication (not quite understand why). */#include <cstdio> #include <iostream> #define N 25using namespace Std;int n,m,p,fail[n],a[n][n],ans[n][n] , C[n][n];char s[n];void KMP () {fail[1]=0; for (int i=2;i<=m;i++) {int p=fail[i-1]; while (P&&s[p+1]!=s[i]) p=fail[p]; if (s[p+1]==s[i]) fail[i]=p+1; else fail[i]=0; } for (int i=0;i<m;i++) for (int j= ' 0 '; j<= ' 9 '; j + +) {int p=i; while (P&&S[P+1]!=J) p=fail[p]; if (S[P+1]==J) aI [p+1]++; else a[i][0]++; }}int Main () {scanf ("%d%d%d%s", &n,&m,&p,s+1); KMP (); for (int i=0;i<m;i++) ans[i][i]=1; while (n) {if (n&1) {-(int i=0;i<m;i++) for (int j=0;j<m;j++) for (int k=0;k<m;k++) c[i][j]= (c[i][j]+a[i][k]*ans[k][j])%p; for (int i=0;i<m;i++) for (int j=0;j<m;j++) ans[i][j]=c[i][j],c[i][j]=0; } for (int i=0;i<m;i++) for (int j=0;j<m;j++) for (int k=0;k<m;k++) C[i][j]= (C[i][j]+a[i][k]*a[k][j])%p; for (int i=0;i<m;i++) for (int j=0;j<m;j++) a[i][j]=c[i][j],c[i][j]=0; n>>=1; } int sum=0; for (int i=0;i<m;i++) sum= (sum+ans[0][i])%p; printf ("%d", sum); return 0;}
GT Exam (Bzoj 1009)