题目简介
有一个长 的字符串 ,其中只包含数字 以及 ,问你把所有的 都替换成数字后得到的数是 的倍数的情况数。可以包含前导零。答案对 取模。
思路描述
考虑 dp。设 表示前 位填完后余数为 的方案数。
当 时,若 不为 ,则可得到 对 的贡献为 。
同理,若 不为 ,则可得到 对 的贡献为 ,其中 ,。
时间复杂度 ,常数较大。
标准代码
#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
long long n,x=1,k,dp[100005][105];
string s;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>k>>s,s=' '+s,dp[n+1][0]=1;
for(int i=n;i>0;i--){
if('0'<=s[i] && s[i]<='9') for(int j=0;j<k;j++) dp[i][(j+(s[i]-'0')*x)%k]+=dp[i+1][j],dp[i][(j+(s[i]-'0')*x)%k]%=mod;
else for(int l=0;l<10;l++) for(int j=0;j<k;j++) dp[i][(j+l*x)%k]+=dp[i+1][j],dp[i][(j+l*x)%k]%=mod;
x=x*10%k;
}
cout<<dp[1][0];
return 0;
}