forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpcrc1c.cpp
31 lines (31 loc) · 826 Bytes
/
cpcrc1c.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
// 2014-05-28
#include <iostream>
#include <cstdio>
using namespace std;
int sum_of_digits(int x) {
if (x == 0) return 0; else return x%10 + sum_of_digits(x/10);
}
long long sum(int x) {
// returns sum of digits in 0 + 1 + ... + (x-1)
long long res = 0;
long long cur = 0;
long long increment = 1;
int exponent = 0;
while (10*increment <= x) { increment *= 10; exponent++; }
while (cur < x) {
if (cur + increment > x) {
increment /= 10; exponent--; continue;
}
long long sod = sum_of_digits(cur);
res += increment*sod + 45*exponent*increment/10;
cur += increment;
}
return res;
}
int main() {
for (;;) {
int a, b; scanf("%d %d", &a, &b);
if (a == -1) return 0;
printf("%lld\n", sum(b+1) - sum(a));
}
}