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的方案数。
转移是很显然的。

  

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; const int N = , MOD = ;
int m, d, lenLow, lenHigh;
char low[N], high[N];
int dp[N][][][N]; int add(int a, int b) {
return (((a + b) % MOD) + MOD) % MOD;
} int work(char *str) {
int n = strlen(str);
for(int full = ; full < ; ++full)
for(int odd = ; odd < ; ++odd)
for(int mod = ; mod < m; ++mod)
dp[][full][odd][mod] = ;
for(int i = ; i <= str[] - ''; ++i) {
if(i == d) continue;
int &nex = dp[][i == str[] - ''][][i % m];
nex = add(nex, );
}
for(int digit = ; digit < n - ; ++digit) {
for(int full = ; full < ; ++full)
for(int odd = ; odd < ; ++odd)
for(int mod = ; mod < m; ++mod)
dp[digit + ][full][odd][mod] = ; int lim = str[digit + ] - '';
for(int full = ; full < ; ++full)
for(int odd = ; odd < ; ++odd)
for(int mod = ; mod < m; ++mod) {
if(!dp[digit][full][odd][mod]) continue;
int now = dp[digit][full][odd][mod];
if(odd) {
if(!full || (full && lim >= d)) {
int &nex = dp[digit + ][full && d == lim][odd ^ ][(mod * + d) % m];
nex = add(nex, now);
}
} else {
if(full) {
for(int i = ; i <= lim; ++i) {
if(i == d) continue;
int &nex = dp[digit + ][i == lim][odd ^ ][(mod * + i) % m];
nex = add(nex, now);
}
} else {
for(int i = ; i < ; ++i) {
if(i == d) continue;
int &nex = dp[digit + ][][odd ^ ][(mod * + i) % m];
nex = add(nex, now);
}
}
}
} for(int i = ; i < ; ++i) {
if(i == d) continue;
int &nex = dp[digit + ][][][i % m];
nex = add(nex, );
}
} int ret = ;
for(int full = ; full < ; ++full)
for(int odd = ; odd < ; ++odd)
ret = add(ret, dp[n - ][full][odd][]);
return ret;
} void solve() {
bool flag = true;
for(int i = ; i < lenLow; i += )
if(low[i] != d + '') flag = false;
for(int i = ; i < lenLow; i += )
if(low[i] == d + '') flag = false;
int sum = ;
for(int i = ; i < lenLow; ++i)
sum = (sum * + low[i] - '') % m;
flag &= sum == ; int ansb = work(high);
int ansa = work(low); printf("%d\n", add(flag, add(ansb, -ansa)));
} int main() {
scanf("%d%d", &m, &d);
scanf("%s", low);
lenLow = strlen(low);
scanf("%s", high);
lenHigh = strlen(high);
solve();
return ;
}
上一篇:安全增强 Linux (SELinux) 剖析


下一篇:20145208 蔡野 《网络对抗》Exp6 信息搜集与漏洞扫描