Skip to content

Commit 7556f91

Browse files
authored
digit dp
1 parent f9c14ab commit 7556f91

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long LL;
5+
6+
LL dp[20][10][2];
7+
8+
9+
LL calc(int pos,int sum,bool less, const string & num){
10+
if(pos ==num.size())
11+
return sum !=0;
12+
if(dp[pos][sum][less] != -1)
13+
return dp[pos][sum][less];
14+
15+
int lmt = less ? num[pos] -'0':8;
16+
if(lmt ==9)lmt =8;
17+
LL ret =0;
18+
for(int i=0 ; i<=lmt ; ++i){
19+
ret += calc(pos +1,(sum +i) % 9, less && i==lmt ,num);
20+
}
21+
return dp[pos][sum][less] = ret;
22+
}
23+
24+
LL solve(LL n){
25+
memset(dp,-1,sizeof(dp));
26+
string s = to_string(n);
27+
return calc(0,0,1,s);
28+
}
29+
30+
int main(){
31+
int T;
32+
cin >> T;
33+
for(int t=1 ; t <= T; ++t){
34+
LL n,m;
35+
cin >> n >> m;
36+
// cout << "solve(n-1) = " <<solve(n-1) << "\n";
37+
// cout << "solve(m) = " <<solve(m) << "\n";
38+
LL ans = solve(m) - solve(n-1);
39+
cout << "Case #"<<t <<": "<< ans << '\n';
40+
}
41+
}

0 commit comments

Comments
 (0)