【bzoj4421】【cerc2015】【Digit Division】

来源:互联网 发布:个体能开淘宝企业店铺 编辑:程序博客网 时间:2024/05/13 13:34

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

Sample Output

4
题解:预处理出符合要求的前缀有多少个。
考虑到如果两个前缀都符合要求那么它们之间的串一定也符合要求。
设符合要求的前缀数量为t,则答案就是2^(t-1);
注意如果整个串都不符合要求那么一定无解,最后判一下即可。
代码:
#include<iostream>#include<cstdio>#define P 1000000007using 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;} 



0 0