-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSum of digits.cpp
65 lines (54 loc) · 1.26 KB
/
Sum of digits.cpp
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
58
59
60
61
62
63
64
65
// DP sum of digits problem
// maths + dp
// tutorial geeks for geeks
// http://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from-1-to-n/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
using namespace std;
int dp[12];
long long int func(int n)
{
if(n<=9)
{
return (n*(n+1))/2;
}
int d = log10(n);
long long int a[d+1];
a[0] = 0, a[1] = 45;
for(int i=2;i<=d;i++)
{
a[i] = a[i-1]*10 + 45*ceil(pow(10,i-1));
}
long long int p = ceil(pow(10,d));
int msd = n/p;
return msd*a[d] + ((msd*(msd-1))/2)*p + msd*(1+n%p) + func(n%p);
}
int main() {
int a,b;
cin >> a >> b;
dp[0] = 0;
memset(dp,-1,sizeof(dp));
for (int i = 1; i <=9; i++)
{
dp[i] = dp[i-1] + i;
}
while(a!=-1 && b!=-1)
{
if(a==0){
cout << func(b) << endl;
}
else if(a<=9 && b<=9)
{
cout << dp[b]-dp[a-1] << endl;
}
else
{
long long int l = func(b) - func(a-1);
printf("%lld\n",l);
}
cin >> a >> b;
}
return 0;
}