-
Notifications
You must be signed in to change notification settings - Fork 1
/
D.cpp
112 lines (88 loc) · 2.12 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mod 1000000007
ll dp[2][2][2002][2002][2], m, d;
string num;
ll solve(ll isStart, ll isSmall, ll pos, ll ns, ll dgc)
{
if(pos == 0) {
if(ns == 0)
return 1;
else
return 0;
}
ll &ret = dp[isStart][isSmall][pos][ns][dgc];
if(ret != -1 && isSmall)
return ret;
ll lim, pos2 = num.size() - pos;
if(isSmall)
lim = 9;
else
lim = num[pos2] - '0';
ll rt = 0;
if(!isStart) {
for(ll i = 0; i <= lim; i++) {
if( !dgc && i != d)
continue;
if( dgc && i == d)
continue;
//cout << (ns * 10 + i) % m << endl;
rt += solve(0, isSmall | i < (num[pos2] - '0'), pos - 1, (ns * 10 + i) % m , dgc ^ 1);
rt %= mod;
}
}
else {
for(ll i = 1; i <= lim; i++) {
if(i == d)
continue;
//cout << i << " " << pos2 << endl;
rt += solve(0, isSmall | i < (num[pos2] - '0'), pos - 1, (ns * 10 + i) % m , dgc ^ 1);
rt %= mod;
}
rt += solve(1, 1, pos - 1, 0, 1);
rt %= mod;
}
return ret = rt % mod;
}
ll calc(string &n)
{
num = n;
if(num == "0")
return 0;
return solve(1, 0, num.size(), 0, 1) - 1;
}
int main()
{
std::ios_base::sync_with_stdio(false);
ll t, caseno = 0;
memset(dp, -1, sizeof(dp));
cin >> m >> d;
string l, r, l2 = "";
cin >> l >> r;
ll ans = calc(r);
reverse(l.begin(), l.end());
ll carry = 1;
for(ll i = 0; i < l.length(); i++) {
ll dg = l[i] - '0';
ll res = dg - carry;
if(res < 0) {
carry = 1;
res = (res + 10) % 10;
}
else
carry = 0;
l2 += (res + '0');
}
ll indx = l2.length() - 1;
while(l2[indx] == '0') {
l2.pop_back();
indx--;
}
reverse(l2.begin(), l2.end());
if(l2.length() != 0)
ans -= calc(l2);
cout << (ans + mod) % mod << endl;
return 0;
}