UVALive 7327 Digit Division (模拟)

Digit Division

题目链接:

http://acm.hust.edu.cn/vjudge/contest/127407#problem/D

Description

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
##题意: 求有多少种方式把一个数串划分成多段,使得每段组成的数字能被m整除.
##题解: 先从整体考虑,如果能被划分成满足条件的若干段,那么整个原串组成的数字必定满足条件. (若A,B都能被m整除,则AB=A*100...+B一定能被m整除) 再考虑把原串划分成两段,当且仅当某个前缀组成的数能被m整除时,原串才能被分成两段. 划成多段同理. 那么结果就是,求有多少个前缀能恰好被m整除. 若有m个(不包括末尾),结果就是 2^m. (相当于枚举每个位置分割or不分割).
##代码: ``` cpp #include #include #include #include #include #include #include #include #include #include #define LL long long #define eps 1e-8 #define maxn 1010 #define mod 1000000007 #define inf 0x3f3f3f3f #define mid(a,b) ((a+b)>>1) #define IN freopen("in.txt","r",stdin); using namespace std;

LL n,m;

LL quickmod(LL a,LL b,LL m)
{
LL ans = 1;
while(b){
if(b&1){
ans = (ansa)%m;
b--;
}
b/=2;
a = a
a%m;
}
return ans;
}

int main(int argc, char const *argv[])
{
//IN;

while(scanf("%lld %lld", &n,&m) != EOF)
{
    char tmp[80]; gets(tmp);
    LL cnt = 0;
    LL cur = 0;
    for(int i=1; i<=n; i++) {
        char c = getchar(); c -= '0';
        cur = (cur*10LL + c) % m;
        if(cur == 0LL) cnt++;
    }

    if(cur) {
        printf("0
");
        continue;
    }

    LL ans = quickmod(2LL, cnt-1LL, mod);

    printf("%lld
", ans);
    //cout << ans << endl;
}

return 0;

}

原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5758003.html