[bzoj1009]GT考试

http://www.cnblogs.com/ianaesthetic/p/3714216.html

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <queue>
 7 #include <string>
 8 #include <vector>
 9 #include <cmath>
10 #include <map>
11 using namespace std;
12 
13 int ans,n,m,k,nxt[50];
14 struct M{
15     int T[30][30];
16     M(){memset(T,0,sizeof(T));}
17     M MUL (M b,int l1,int l2,int l3){
18         M ret;
19         for(int i=0;i<l1;i++)
20             for(int j=0;j<l3;j++)
21                 for(int k_=0;k_<l2;k_++)
22                     ret.T[i][j]=(ret.T[i][j]+T[i][k_]*b.T[k_][j])%k;
23         return ret;
24     }
25 }f,ff;
26 M mul(M a,int len,int b){
27     M ret;
28     for(int i=0;i<len;i++)ret.T[i][i]=1;
29     while(b){
30         if(b&1)ret=ret.MUL(a,len,len,len);
31         a=a.MUL(a,len,len,len);
32         b>>=1;
33     }
34     return ret;
35 }
36 char str[50];
37 int main(){
38     scanf("%d%d%d%s",&n,&m,&k,str+1);
39     for(int i=2,j=0;i<=m;i++){
40         while(j&&str[j+1]!=str[i])j=nxt[j];
41         nxt[i]=(j+=str[j+1]==str[i]);
42     }
43     for(int i=0;i<m;i++){
44         for(int j=0;j<=9;j++){
45             int t=i;
46             while(t&&str[t+1]!=j+'0')t=nxt[t];
47             if(str[t+1]==j+'0')f.T[i][t+1]++;
48             else f.T[i][0]++;
49         }
50     }
51     ff.T[0][0]=1;
52     f=mul(f,m,n);
53     ff=ff.MUL(f,1,m,m);
54     for(int i=0;i<m;i++)(ans+=ff.T[0][i])%=k;
55     printf("%d\n",ans);
56 }
View Code

 

posted @ 2017-01-28 23:18  KingSann  阅读(187)  评论(0编辑  收藏  举报