#include <iostream>
#include <cstdio>
using namespace std;
int n, m, mod, nxt[25], too[25][15];
char ss[25];
struct Matrix{
int num[25][25];
Matrix operator*(const Matrix &u)const{
Matrix re;
for(int i=0; i<m; i++)
for(int j=0; j<m; j++){
re.num[i][j] = 0;
for(int k=0; k<m; k++)
re.num[i][j] = (re.num[i][j] + num[i][k]*u.num[k][j]) % mod;
}
return re;
}
}zhu, yua, dan;
void mknxt(){
int i=0, j;
j = nxt[0] = -1;
while(i<m){
if(j==-1 || ss[i]==ss[j]) nxt[++i] = ++j;
else j = nxt[j];
}
}
void mktoo(){
for(int i=0; i<m; i++)
for(int j=0; j<=9; j++){
too[i][j] = ss[i]==j+'0'?i+1:too[max(nxt[i],0)][j];
zhu.num[i][too[i][j]]++;
}
}
int main(){
cin>>n>>m>>mod;
scanf("%s", ss);
mknxt();
mktoo();
for(int i=0; i<m; i++)
dan.num[i][i] = 1;
yua.num[0][0] = 1;
while(n){
if(n&1) dan = dan * zhu;
zhu = zhu * zhu;
n >>= 1;
}
yua = yua * dan;
int ans=0;
for(int i=0; i<m; i++)
ans = (ans + yua.num[0][i]) % mod;
cout<<ans<<endl;
return 0;
}
转载于:https://www.cnblogs.com/poorpool/p/8830078.html