Codeforces CF#628 Education 8 D. Magic Numbers

D. Magic Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Consider the decimal presentation of an integer. Let's call a number d-magic if digit d appears in decimal presentation of the number on even positions and nowhere else.

For example, the numbers 1727374, 17, 1 are 7-magic but 77, 7, 123, 34, 71 are not 7-magic. On the other hand the number 7 is0-magic, 123 is 2-magic, 34 is 4-magic and 71 is 1-magic.

Find the number of d-magic numbers in the segment [a, b] that are multiple of m. Because the answer can be very huge you should only find its value modulo 109 + 7 (so you should find the remainder after dividing by 109 + 7).

Input

The first line contains two integers m, d (1 ≤ m ≤ 2000, 0 ≤ d ≤ 9) — the parameters from the problem statement.

The second line contains positive integer a in decimal presentation (without leading zeroes).

The third line contains positive integer b in decimal presentation (without leading zeroes).

It is guaranteed that a ≤ b, the number of digits in a and b are the same and don't exceed 2000.

Output

Print the only integer a — the remainder after dividing by 109 + 7 of the number of d-magic numbers in segment [a, b] that are multiple of m.

Examples
input
2 6
10
99
output
8
input
2 0
1
9
output
4
input
19 7
1000
9999
output
6
Note

The numbers from the answer of the first example are 16, 26, 36, 46, 56, 76, 86 and 96.

The numbers from the answer of the second example are 2, 4, 6 and 8.

The numbers from the answer of the third example are 1767, 2717, 5757, 6707, 8797 and 9747.

 

题意:
    定义一个数字为d-magic当且仅当数字d仅在数字偶数位出现。
    问在[a,b]之间的被m整除的d-magic数字有多少个。

 

题解:
    数位dp味道很浓,用十分经典的模型就行了。
    不难想到f[N][2][2][M](f[i][0..1][0..1][j])。
    代表已经确定了前i位数字的选择(不一定要i位都填,可能前面不足i位),
    当前位为奇数位或偶数位,
    当前是否达到前i位数字的上限,
    MOD m后余数为j的方案数。
    转移是很显然的。

  

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <string>
  5 #include <iostream>
  6 #include <algorithm>
  7 using namespace std;
  8 
  9 const int N = 2010, MOD = 1000000007;
 10 int m, d, lenLow, lenHigh;
 11 char low[N], high[N];
 12 int dp[N][2][2][N];
 13 
 14 int add(int a, int b) {
 15     return (((a + b) % MOD) + MOD) % MOD;
 16 }
 17 
 18 int work(char *str) {
 19     int n = strlen(str);
 20     for(int full = 0; full < 2; ++full)
 21         for(int odd = 0; odd < 2; ++odd)
 22             for(int mod = 0; mod < m; ++mod)
 23                 dp[0][full][odd][mod] = 0;
 24     for(int i = 1; i <= str[0] - '0'; ++i) {
 25         if(i == d) continue;
 26         int &nex = dp[0][i == str[0] - '0'][1][i % m];
 27         nex = add(nex, 1);
 28     }
 29     for(int digit = 0; digit < n - 1; ++digit) {
 30         for(int full = 0; full < 2; ++full)
 31             for(int odd = 0; odd < 2; ++odd)
 32                 for(int mod = 0; mod < m; ++mod)
 33                     dp[digit + 1][full][odd][mod] = 0;
 34 
 35         int lim = str[digit + 1] - '0';
 36         for(int full = 0; full < 2; ++full)
 37             for(int odd = 0; odd < 2; ++odd)
 38                 for(int mod = 0; mod < m; ++mod) {
 39                     if(!dp[digit][full][odd][mod]) continue;
 40                     int now = dp[digit][full][odd][mod];
 41                     if(odd) {
 42                         if(!full || (full && lim >= d)) {
 43                             int &nex = dp[digit + 1][full && d == lim][odd ^ 1][(mod * 10 + d) % m];
 44                             nex = add(nex, now);
 45                         }
 46                     } else {
 47                         if(full) {
 48                             for(int i = 0; i <= lim; ++i) {
 49                                 if(i == d) continue;
 50                                 int &nex = dp[digit + 1][i == lim][odd ^ 1][(mod * 10 + i) % m];
 51                                 nex = add(nex, now);
 52                             }
 53                         } else {
 54                             for(int i = 0; i < 10; ++i) {
 55                                 if(i == d) continue;
 56                                 int &nex = dp[digit + 1][0][odd ^ 1][(mod * 10 + i) % m];
 57                                 nex = add(nex, now);
 58                             }
 59                         }
 60                     }
 61                 }
 62 
 63         for(int i = 1; i < 10; ++i) {
 64             if(i == d) continue;
 65             int &nex = dp[digit + 1][0][1][i % m];
 66             nex = add(nex, 1);
 67         }
 68     }
 69 
 70     int ret = 0;
 71     for(int full = 0; full < 2; ++full)
 72         for(int odd = 0; odd < 2; ++odd)
 73             ret = add(ret, dp[n - 1][full][odd][0]);
 74     return ret;
 75 }
 76 
 77 void solve() {
 78     bool flag = true;
 79     for(int i = 1; i < lenLow; i += 2)
 80         if(low[i] != d + '0') flag = false;
 81     for(int i = 0; i < lenLow; i += 2)
 82         if(low[i] == d + '0') flag = false;
 83     int sum = 0;
 84     for(int i = 0; i < lenLow; ++i)
 85         sum = (sum * 10 + low[i] - '0') % m;
 86     flag &= sum == 0;
 87 
 88     int ansb = work(high);
 89     int ansa = work(low);
 90 
 91     printf("%d\n", add(flag, add(ansb, -ansa)));
 92 }
 93 
 94 int main() {
 95     scanf("%d%d", &m, &d);
 96     scanf("%s", low);
 97     lenLow = strlen(low);
 98     scanf("%s", high);
 99     lenHigh = strlen(high);
100     solve();
101     return 0;
102 }
View Code

 

posted @ 2016-12-02 12:16  yanzx6  阅读(158)  评论(0编辑  收藏