4421: [Cerc2015] Digit Division
Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 348 Solved: 202
[Submit][Status][Discuss]
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
1246
Sample Output
4
HINT
Source
只需要求出所有能使得前缀数字串在mod意义下等于0的位置,设为$t$,则从这些位置任意切开,得到的串均满足要求。每个位置有两种选项(切或不切)答案是$2^{t}$。
1 #include <cstdio>
2
3 const int mod = 1e9 + 7;
4
5 int n, m, t;
6 char s[300005];
7
8 inline int pow(long long a, int b)
9 {
10 long long r = 1;
11
12 while (b)
13 {
14 if (b & 1)
15 {
16 r *= a;
17
18 if (r >= mod)
19 r %= mod;
20 }
21
22 b >>= 1;
23 a = a * a;
24
25 if (a >= mod)
26 a %= mod;
27 }
28
29 return r;
30 }
31
32 signed main(void)
33 {
34 scanf("%d%d%s", &n, &m, s);
35
36 int sum = 0;
37
38 for (char *c = s; *c; ++c)
39 {
40 sum = sum * 10 + *c - '0';
41
42 if (sum >= m)
43 sum %= m;
44
45 if (sum == 0)
46 ++t;
47 }
48
49 printf("%d\n", sum ? 0 : pow(2, t - 1));
50 }
@Author: YouSiki
来源:https://www.cnblogs.com/yousiki/p/6281355.html