Submission #299101

# Submission time Handle Problem Language Result Execution time Memory
299101 2020-09-14T13:33:52 Z ryuku1110 Trol (COCI19_trol) C++14
50 / 50
1 ms 512 KB
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
int q; ll l, r;
ll digitsum(ll x) {
    if (x < 10) return x;
    ll res = 0;
    while (x != 0) {
        res += x % 10;
        x /= 10;
    }
    while (res >= 10) res = digitsum(res); 
    return res;
}
int main() {
  //freopen(".INP", "r", stdin);
  //freopen(".OUT", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
     
    cin >> q;
    while (q --) {
        cin >> l >> r;
        ll ans = 0;
        ll lsum = digitsum(l);
        ll rsum = digitsum(r);
        if (r - l + 1 <= 11) {
            for (ll i = l; i <= r; ++ i) ans += digitsum(i);
            cout << ans << "\n";
            continue;
        } 
        ll cnt = r - l + 1;
        ll tmp = 0;
        for (ll i = lsum; i <= 9; ++ i) {
            ++ tmp;
            ans += i;
        }
        for (ll i = rsum; i >= 1; -- i) {
            ++ tmp;
            ans += i;
        }

        if (tmp == cnt) {
            cout << ans << "\n";
            continue;
        }
        ans += ((cnt - tmp) / 9) * 45;
        cout << ans << "\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 384 KB Output is correct