-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathDigitSum.cpp
More file actions
63 lines (58 loc) · 1.17 KB
/
Copy pathDigitSum.cpp
File metadata and controls
63 lines (58 loc) · 1.17 KB
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
int D;
int len;
string L, R;
int dp[1010][2][2][100];
int rec(int level, int tlo, int thi, int sumD){
if(level==len){
if(sumD==0) return 1;
else return 0;
}
if(dp[level][tlo][thi][sumD]!= -1){
return dp[level][tlo][thi][sumD];
}
int ans =0;
int lo=0;
if(tlo==1){
lo = L[level]-'0';
}
int hi=9;
if(thi==1){
hi = R[level]-'0';
}
for(int i=lo;i<=hi;i++){
int ntlo= tlo;
if(i!=L[level]-'0') ntlo=0;
int nthi=thi;
if(i!=R[level]-'0') nthi=0;
ans += rec(level+1, ntlo, nthi, (sumD+i) %D);
ans =ans%mod;
}
return dp[level][tlo][thi][sumD]=ans;
}
void shiv_prime94(){
cin>>L>>R;
cin>>D;
int cnt = R.length()- L.length();
string temp="";
while(cnt--){
temp+='0';
}
L= temp+L;
memset(dp, -1, sizeof(dp));
len= R.length();
rec(0,1,1,0);
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll test=1;
cin>>test;
while(test--)
{
shiv_prime94();
}
return 0;
}