题面
https://www.luogu.org/problem/P3193
题解
// luogu-judger-enable-o2 #include<cstdio> #include<iostream> #include<cstring> #define ri register int #define N 50 #define LL long long using namespace std; int nex[N],n,m,K; char s[N]; struct matrix{ int v[N][N]; matrix operator * (const matrix &rhs) const { matrix ret; ret.clear(); for (ri k=0;k<m;k++) for (ri i=0;i<m;i++) for (ri j=0;j<m;j++) (ret.v[i][j]+=v[i][k]*rhs.v[k][j])%=K; return ret; } void clear() { memset(v,0,sizeof(v)); } } mat; int main(){ scanf("%d %d %d",&n,&m,&K); scanf("%s",s+1); nex[0]=nex[1]=0; for (ri i=2;i<=m;i++) { int j=nex[i-1]; while (j && s[j+1]!=s[i]) j=nex[j]; if (s[j+1]==s[i]) j++; nex[i]=j; } mat.clear(); for (ri i=0;i<m;i++) { for (ri c='0';c<='9';c++) { int j=i; while (j && s[j+1]!=c) j=nex[j]; if (s[j+1]==c) j++; mat.v[i][j]++; } } matrix fac; fac.clear(); for (ri i=0;i<m;i++) fac.v[i][i]=1; for (;n;n>>=1,mat=mat*mat) if (n&1) fac=fac*mat; //for (ri i=1;i<=n;i++) fac=fac*mat; int ans=0; for (ri i=0;i<m;i++) (ans+=fac.v[0][i])%=K; cout<<ans<<endl; }