BZOJ4421

来源:互联网 发布:如何用手机淘宝装修 编辑:程序博客网 时间:2024/05/16 10:08

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

First line contains T denoting the number of test cases.
T test cases follow. Each test case
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 10^9+7

Sample Input

2
4 2
1246
4 7
2015

Sample Output

4
0

Explanation

本题要求将给定的数字串分成一个或多个子串,要求分出来的每个子串均为m的整数倍
求不同划分的种数
可利用前缀思想 求出满足条件的 (1,i) 的个数
若 (1,i),(1,j) 均满足条件且 j>i 则 (i,j) 也满足条件
分析下样例1:
12 | 4 | 6 竖线位置为可划分的位置 每个位置均有两种情况 有划分和无划分
则若满足条件的前缀个数为k,则结果为2^(k-1)
要注意的是 若整个数字串无法整除m 则划分种数为0

Code

#include <stdio.h>#define maxn 3000005#define mod 1000000007char num[maxn];int main(){    int t;    scanf("%d",&t);    while(t--){        int n,m;        scanf("%d%d",&n,&m);        scanf("%s",num);        long long sum=0;        long long par=0;        for(int i=0;i<n;i++){            sum=(sum*10+num[i]-'0')%m;            if(!sum){                if(par==0)                    par=1;                else                    par=par*2%mod;            }        }        if(sum)            printf("0\n");        //若整个数字串不满足要求,则输出0        else            printf("%lld\n",par);    }    return 0;}
原创粉丝点击