Translate

Friday 30 January 2015

PR003004(Digit Sum) 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
34
HII GUYS THIS IS MY AC C++ SOLUTION

if u want to know the logic behind this u can rerfer my
another post for explanation
http://spojdev.blogspot.in/2015/01/formula-for-calculating-sum-of-digits.html

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

1 comment:

Anonymous said...

Atleast explain the solution, the code wont do any good if the problem is a bit different from this

Working With Java Collections