#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int f(int x) {
int d = 0;
while (x > 0) {
d += x % 10;
x /= 10;
}
return d < 10 ? d : f(d);
}
int main() {
int t; cin >> t;
while (t--) {
ll l, r; cin >> l >> r;
if (r-l <= 100) {
ll ans = 0;
for (ll i = l; i <= r; i++) {
ans += f(i);
}
cout << ans << '\n';
continue;
}
ll ans = 0;
while (l <= r && l % 9 != 1) {
ans += f(l);
l++;
}
while (r >= l && r % 9 != 0) {
ans += f(r);
r--;
}
ll num = (r-l+1)/9;
ans += 45*num;
cout << ans << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
248 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |