Describe
http://www.lydsy.com/JudgeOnline/problem.php?id=1009
The strings are all composed of 0~9, giving a string s, which is a string of length n, and does not contain the kind of S.
Analysis
The first eye thought it was a combination. And then it's even more comical to use the wrong way to figure out how to calculate a hand. How bad is my maths?
The problem is also seen long time, a bit difficult to understand.
Feel POPOQQQ God Ben speak more clearly. Portal:http://blog.csdn.net/popoqqq/article/details/40188173
We use Dp[i][j] to denote: a string of length I, whose length is the suffix of j and the prefix of S-length J exactly matches the number of species.
The final answer is Ans=sigma{dp[n][i]} (0<=i<m).
There is also a two-dimensional a array, which is more difficult to explain ...
Dp[i][y] can be transferred by Dp[i-1][x], then the matching s prefix from length x to length y,a[x][y] means that after adding a character at the end, the match length from X to Y, such as how many kinds of characters.
Then Dp[i][y]=sigma{dp[i-1][x]*a[x][y]}.
This can be calculated using matrix multiplication.
What about a array? with KMP.
A[x][y] indicates that the prefix match length is changed from X to the number of Y, then from each bit I start to match, if the match to J, then a[i][j+1]++ (array starting from 0, the first I bit matches I, match to the first J bit, should be j+1).
P.s. I'm a good cook.
1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn= -+5;5 intN,m,p,ans;6 CharSTR[MAXN];7 intF[MAXN];8 9 structmatrix{Ten intx[ -][ -]; OneMatrix () {memset (x,0,sizeofx); } A int*operator[] (intID) {returnx[id];} - }dp,a; - void operator*= (Matrix &x,matrix &y) { the Matrix Z; - for(intI=0; i<m;i++) for(intj=0; j<m;j++) for(intk=0; k<m;k++) -Z[I][J]+=X[I][K]*Y[K][J], z[i][j]%=p; -x=Z; + } - voidKMP () { + for(intI=1; i<m;i++){ A intj=F[i]; at while(J&&str[i]!=str[j]) j=F[j]; -f[i+1]=str[i]==str[j]?j+1:0; - } - for(intI=0; i<m;i++) for(Chark='0'; k<='9'; k++){ - intj=i; - while(J&&k!=str[j]) j=F[j]; in if(K==str[j]) a[i][j+1]++; - Elsea[i][0]++; to } + } - voidQuick_power (inty) { the for(; y;a*=a,y>>=1)if(y&1) dp*=A; * } $ intMain () {Panax Notoginsengscanf"%d%d%d",&n,&m,&p); -scanf"%s", str); the KMP (); +dp[0][0]=1; A quick_power (n); the for(intI=0; i<m;i++) ans+=dp[0][i], ans%=p; +printf"%d\n", ans); - return 0; $}
View Code
1009: [HNOI2008]GT exam Time limit:1 Sec Memory limit:162 MB
submit:2815 solved:1738
[Submit] [Status] [Discuss] 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 OutputBayiHintsource
BZOJ_1009_[HNOI2008]_GT Exam _ (Dynamic planning +kmp+ matrix multiplication optimization + fast power)