题意:给三个数n,k,m。求满足以下条件的数有多少个。
1,. 位数为n(去前导0);
2. 存在一个后缀,后缀数模k等于0。
思路:数位DP
dp[i][j][0]表示有i位,模k等于j且不存在后缀数模k等于0的数的个数。
dp[i][j][1]表示有i位,模k等于j且存在后缀数模k等于0的数的个数。
显然需要从低位向高位扫。
pos[i]表示第i位等于1模k运算应该加上的数。
将c从0到9枚举,令temp=(c*pos[i]+j)%k ;
当temp==0时,此时必然存在后缀数,则dp[i+1][temp][1]+=dp[i+1][temp[0];
该状态也能从原本就存在后缀数的状态转移,则dp[i+1][temp][1]+=dp[i+1][temp][0];
temp!=0时状态转移方程明显。
/*I have given up the treatment-_-||*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
using namespace std;
#define mod 1000000
typedef long long LL;
const int maxn=1010;
const int maxm=110;
const int INF=2*1e9;
int n,m;
LL dp[maxn][maxm][2];
int pos[maxn];
int main()
{
int i,j,c;
int k;
while(scanf("%d%d%d",&n,&k,&m)!=EOF){
pos[0]=1;
for(i=1;i<=n;i++){
pos[i]=(pos[i-1]*10)%k;
}
memset(dp,0,sizeof(dp));
dp[0][0][0]=1;
for(i=0;i<=n-1;i++){
for(j=0;j<k;j++){
for(c=0;c<10;c++){
if(i==n-1&&c==0) continue;
int temp=(c*pos[i]+j)%k;
if(temp==0&&c>0){
dp[i+1][temp][1]+=dp[i][j][0];
dp[i+1][temp][1]+=dp[i][j][1];
dp[i+1][temp][1]%=m;
}
else{
dp[i+1][temp][0]+=dp[i][j][0];
dp[i+1][temp][0]%=m;
dp[i+1][temp][1]+=dp[i][j][1];
dp[i+1][temp][1]%=m;
}
}
}
}
LL ans=0;
for(i=0;i<k;i++){
//printf("*%I64d\n",dp[n][i][1]);
ans+=dp[n][i][1];
ans%=m;
}
printf("%I64d\n",ans);
}
return 0;
}