Digit Division

Digit Division

Time limit: 1 s Memory limit: 512 MiB

  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 first line contains two integers n and m (1 ≤ n ≤ 300 000, 1 ≤ m ≤ 1 000 000) – the length of the sequence and the divisor respectively. The second line contains a string consisting of exactly n digits.

Output

  Output a single integer – the number of different partitions modulo 109 + 7.

Example

input

4 2

1246

output

4

 

 

input

4 7

2015

output

0

 

 

//题意: n 位长的十进制数字,在其中可以任意插入分割线,分割后,要使每一段不为空,并且可以整除 m ,合法分割的方案数

 

//题目是极其简单的,如果前一部分可以整除 m ,那么,这部分乘10的x次方后依然可以整除,然后算出所有可分割的位置后

C(0,all),C(1,all)+...+C(all,all);  这些必然合法

= 2^all

但是,此题如果没想清楚,写代码会进坑,此题是对方案数取模,all 是%m==0的方案数,进了坑半天想不出来,唉,还是太菜啊,一度wa在第三组,真是日狗了

 1 # include <cstdio>
 2 # include <cstring>
 3 # include <cstdlib>
 4 # include <iostream>
 5 # include <vector>
 6 # include <queue>
 7 # include <stack>
 8 # include <map>
 9 # include <bitset>
10 # include <sstream>
11 # include <set>
12 # include <cmath>
13 # include <algorithm>
14 # pragma  comment(linker,"/STACK:102400000,102400000")
15 using namespace std;
16 # define LL          long long
17 # define pr          pair
18 # define mkp         make_pair
19 # define lowbit(x)   ((x)&(-x))
20 # define PI          acos(-1.0)
21 # define INF         0x3f3f3f3f
22 # define eps         1e-8
23 # define MOD         1000000007
24 
25 inline int scan() {
26     int x=0,f=1; char ch=getchar();
27     while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
28     while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
29     return x*f;
30 }
31 inline void Out(int a) {
32     if(a<0) {putchar('-'); a=-a;}
33     if(a>=10) Out(a/10);
34     putchar(a%10+'0');
35 }
36 # define MX 300050
37 /**************************/
38 char num[MX];
39 
40 LL qk_mi(LL base,LL x)
41 {
42     LL res = 1;
43     while (x)
44     {
45         if (x%2==1) res = (res*base)%MOD;
46         base=base*base%MOD;
47         x/=2;
48     }
49     return res;
50 }
51 
52 int main()
53 {
54     int n,m;
55     while (scanf("%d%d",&n,&m)!=EOF)
56     {
57         scanf("%s",num);
58         LL zuo = 0;
59         LL all = 0;
60         for (int i=0;i<n;i++)
61         {
62             zuo=(zuo*10+(num[i]-'0'))%m;
63             if (zuo%m==0) all++;
64         }
65         all--;
66         if (zuo%m!=0)
67             printf("0\n");
68         else
69         {
70             LL ans = qk_mi(2,all);
71             printf("%lld\n",ans);
72         }
73     }
74     return 0;
75 }
View Code

 

posted @ 2017-08-28 20:20  happy_codes  阅读(242)  评论(0编辑  收藏  举报