Skip to content

Commit be7a1ce

Browse files
Create Digit Sum Spoj
1 parent 43ab112 commit be7a1ce

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Digit Sum Spoj

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long int
4+
ll dp[18][2] ;
5+
ll couunt(string s,int n,bool tights){
6+
if(tights==0){
7+
return (pow(10,n)+0.1);
8+
}
9+
10+
if(n==0){
11+
return 1;
12+
}
13+
14+
int ub=tights?(s[s.length()-n]-'0'):9;
15+
ll ans=0;
16+
for(ll i=0;i<=ub;i++){
17+
ans+=couunt(s,n-1,(tights&&(i==ub)));
18+
}
19+
20+
return ans;
21+
}
22+
ll solve(string s,int n,bool tights){
23+
if(n==0){
24+
return 0;
25+
}
26+
if(dp[n][tights]!=-1){
27+
return dp[n][tights];
28+
}
29+
int ub=tights?(s[s.length()-n]-'0'):9;
30+
ll ans=0;
31+
for(ll i=0;i<=ub;i++){
32+
ans+=i*couunt(s,n-1,(tights&&(i==ub)));
33+
ans+=solve(s,n-1,(tights&(i==ub)));
34+
}
35+
36+
return dp[n][tights]=ans;
37+
}
38+
int main(){
39+
ll t;
40+
cin>>t;
41+
while(t--){
42+
ll L,R;
43+
cin>>L>>R;
44+
if(L!=0){
45+
L--;
46+
}
47+
48+
string l=to_string(L);
49+
string r=to_string(R);
50+
memset(dp,-1,sizeof dp);
51+
ll fr=solve(r,r.length(),1);
52+
memset(dp,-1,sizeof dp);
53+
ll fl=solve(l,l.length(),1);
54+
55+
cout<<(fr-fl)<<endl;
56+
}
57+
}

0 commit comments

Comments
 (0)