Skip to content

Instantly share code, notes, and snippets.

@vishichoudhary
Created July 27, 2017 04:52
Show Gist options
  • Save vishichoudhary/39a01e8b3c19bcf8e6c55319ee92b471 to your computer and use it in GitHub Desktop.
Save vishichoudhary/39a01e8b3c19bcf8e6c55319ee92b471 to your computer and use it in GitHub Desktop.
Without dp solution to cpcr1c
#include<bits/stdc++.h>
using namespace std;
void digits(long long int a, vector<int> &arr){
while(a){
arr.push_back(a%10);
a/=10;
}
}
int digitsum(int indx,int sum,int tight,vector<int> & arr){
if(indx==-1)
return sum;
long long int ret=0;
int k=(tight)?arr[indx]:9;
for(int i=0;i<=k;i++){
int new_tight=(arr[indx]==i)?tight:0;
ret+=digitsum(indx-1,sum+i,new_tight,arr);
}
return ret;
}
int rangesum(long long int a, long long int b){
memset(dp,-1,sizeof(dp));
vector<int> digitA;
digits(a-1,digitA);
long long int ans1=digitsum(digitA.size()-1,0,1,digitA);
vector<int> digitB;
digits(b,digitB);
long long int ans2=digitsum(digitB.size()-1,0,1,digitB);
return (ans2-ans1);
}
int main(){
long long int a,b;
while(1){
cin>>a>>b;
if(a==-1 && b==-1)
break;
cout<<rangesum(a,b)<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment