Translate

Friday, 30 January 2015

CPCRC1C(Sum of Digits) spoj problem solution

 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
HII GUYS I JUST EXPLAIN HOW I DERIVED THIS FORMULA I JUST MENTION 
IN MY SEPERATE POST.............HERE IS MY 


AC C++ SOLUTION IS::::::::::


#include<iostream>
#include<cmath>
using namespace std;
long long sum(long long N)
{
if(N/10==0) return N*(N+1)/2;
int i=0;
long long n=N;
while(n/10!=0){
i++;
n/=10;
}
     int p=pow(10,i);
     return ((n*45*i*p/10)+ n*(n-1)*p/2+ n*(N%p+1) + sum(N%p));
}
int main()
{
long long a,b;
cin>>a>>b;
while(a!=-1 || b!=-1)
{
cout<<sum(b)-sum(a-1)<<endl;
cin>>a>>b;
}
return 0;
}

No comments:

Working With Java Collections