Description
给出一个数字串,现将其分成一个或多个子串,要求分出来的每个子串能Mod M等于0.
将方案数(mod 10^9+7)
Input
给出N,M,其中1<=N<=300 000,1<=M<=1000 000.
接下来一行,一个数字串,长度为N。
Output
如题
Sample Input
4 2
1246
1246
Sample Output
4
题解:预处理出符合要求的前缀有多少个。
考虑到如果两个前缀都符合要求那么它们之间的串一定也符合要求。
设符合要求的前缀数量为t,则答案就是2^(t-1);
注意如果整个串都不符合要求那么一定无解,最后判一下即可。
代码:
#include<iostream>
#include<cstdio>
#define P 1000000007
using namespace std;
int ans,temp,n,m;
char ch[1000000];
int main(){
scanf("%d%d%s",&n,&m,&ch);
for (int i=0;i<n;i++){
temp=(temp*10+ch[i]-'0')%m;
if (temp==0) if (ans==0) ans=1;else ans=(ans*2)%P;
}
if (temp) ans=0;cout<<ans<<endl;
}