You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.3 KiB

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 25;
int n, m, mod;
char s[N];
int ne[N];
int a[N][N];
//矩阵乘法
void mul(int a[][N], int b[][N], int c[][N]) {
int t[N][N] = {0};
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
for (int k = 0; k < N; k++)
t[i][j] = (t[i][j] + (LL)(a[i][k] * b[k][j]) % mod) % mod;
}
memcpy(c, t, sizeof t);
}
int main() {
cin >> n >> m >> mod;
cin >> s + 1;
// kmp
for (int i = 2, j = 0; i <= m; i++) {
while (j && s[j + 1] != s[i]) j = ne[j];
if (s[j + 1] == s[i]) j++;
ne[i] = j;
}
// 初始化A[i][j]
for (int i = 0; i < m; i++)
for (int c = '0'; c <= '9'; c++) {
int j = i;
while (j && s[j + 1] != c) j = ne[j];
if (s[j + 1] == c) j++;
if (j < m) a[i][j]++;
}
// f[0][0]=1 base case
int f[N][N] = {1};
//矩阵快速幂
for (int i = n; i; i >>= 1) {
if (i & 1) mul(f, a, f); // f:基底 a:需要计算快速幂的常数矩阵 f:结果存储
mul(a, a, a); //倍增a
}
int res = 0;
for (int i = 0; i < m; i++) res = (res + f[0][i]) % mod;
printf("%d\n", res);
return 0;
}