-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathDigit Sum Spoj
57 lines (51 loc) · 1.08 KB
/
Digit Sum Spoj
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll dp[18][2] ;
ll couunt(string s,int n,bool tights){
if(tights==0){
return (pow(10,n)+0.1);
}
if(n==0){
return 1;
}
int ub=tights?(s[s.length()-n]-'0'):9;
ll ans=0;
for(ll i=0;i<=ub;i++){
ans+=couunt(s,n-1,(tights&&(i==ub)));
}
return ans;
}
ll solve(string s,int n,bool tights){
if(n==0){
return 0;
}
if(dp[n][tights]!=-1){
return dp[n][tights];
}
int ub=tights?(s[s.length()-n]-'0'):9;
ll ans=0;
for(ll i=0;i<=ub;i++){
ans+=i*couunt(s,n-1,(tights&&(i==ub)));
ans+=solve(s,n-1,(tights&(i==ub)));
}
return dp[n][tights]=ans;
}
int main(){
ll t;
cin>>t;
while(t--){
ll L,R;
cin>>L>>R;
if(L!=0){
L--;
}
string l=to_string(L);
string r=to_string(R);
memset(dp,-1,sizeof dp);
ll fr=solve(r,r.length(),1);
memset(dp,-1,sizeof dp);
ll fl=solve(l,l.length(),1);
cout<<(fr-fl)<<endl;
}
}