UVALive 7327 Digit Division

来源:互联网 发布:linux源码编译mysql 编辑:程序博客网 时间:2024/06/07 15:58
We are given a sequence of n decimal digits. The sequence needs to be partitioned into one or more
contiguous subsequences such that each subsequence, when interpreted as a decimal number, is divisible
by a given integer m.
Find the number of different such partitions modulo 109+7. When determining if two partitions are
different, we only consider the locations of subsequence boundaries rather than the digits themselves,
e.g. partitions 2|22 and 22|2 are considered different.
Input
The input file contains several test cases, each of them as described below.
The first line contains two integers n and m (1 ≤ n ≤ 300000, 1 ≤ m ≤ 1000000) — the length
of the sequence and the divisor respectively. The second line contains a string consisting of exactly n
digits.
Output
For each test case, output a single integer — the number of different partitions modulo 109 + 7 on a
line by itself.
Sample Input
4 2
1246
4 7
2015
Sample Output
4

0

就是计算(a[i-1]*10+a[i])%m是否为零,若为零,则说明可以在这里分则cnt++,需要特判一下最后一位能否整除m若不能,直接输出0;最后总和为2^(cnt-1)

#include<stdio.h>#include<algorithm>#include<string>#include<string.h>#include<iostream>using namespace std;#define F(x,a,b) for (int x=a;x<=b;x++)#define MOD 1000000007char a[3000005];int pow(int x) {int s=1;F(i,1,x) s=(s*2)%MOD;return s;}int main(){    int n,m;    while (cin>>n>>m)    {        cin>>a;        int k=0;        int ans=0;        int flag=1;        F(i,0,n-1){if (((k*10+a[i]-'0')%m)==0) {ans++;}else{if (i==n-1) {cout<<"0"<<endl;flag=0;break;}} k=(k*10+a[i]-'0')%m;}        if (flag) cout<<pow(ans-1)<<endl;    }}


0 0
原创粉丝点击