https://acm.uestc.edu.cn/problem/zi-chuan/description
用滚动数组做,然后dp[i][j] i表示以第i个为结尾的余数为j的方案数
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e7+9;
int dp[2][maxn];
char s[maxn];
int main(){
int n,k;
scanf("%d%d",&n,&k);
scanf("%s",s);
ll ans=0;
for(int i=1;i<=n;i++){
int x=s[i-1]-'0';
for(int j=0;j<k;j++){
dp[i%2][j]=0;
}
for(int j=0;j<k;j++){
int ii = i%2;
dp[ii][(j*10+x)%k]+=dp[(ii+1)%2][j];
}
dp[i%2][x%k]+=1;
// for(int j=0;j<k;j++){
// cout<<dp[i%2][j]<<" ";
// }
// cout<<endl;
// cout<<dp[i%2][0]<<endl;
ans+=dp[i%2][0];
}
printf("%lld\n",ans);
return 0;
}