답안 #199191

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
199191 2020-01-29T23:43:37 Z silxikys Trol (COCI19_trol) C++14
50 / 50
5 ms 376 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll f(ll x) {
    ll 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 <= 1000) {
            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 += 45LL*num;
        cout << ans << endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 5 ms 256 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 256 KB Output is correct