100722024-03-26 11:53:19Error42Számlexikoncpp17Accepted 100/1007ms3680 KiB
#include <cassert>
#include <climits>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

using ll = long long;

void solve() {
    // These are swapped everywhere!
    ll p, k;
    cin >> p >> k;

    string const ps = to_string(p);

    ll lo = 1;
    ll hi = 0;

    for (ll i = 0; i < ps.size(); i++) {
        hi = hi * 10 + (ps[i] - '0');

        k -= hi - lo + 1;

        lo *= 10;
    }

    assert(hi == p);

    if (k < 0) {
        cout << "0\n";
        return;
    }

    if (k == 0) {
        cout << p << "\n";
        return;
    }

    hi--;

    while (true) {
        hi = hi * 10 + 9;

        ll const added = hi - lo + 1;

        if (added == 0) {
            cout << "0\n";
            return;
        }

        if (added >= k) {
            cout << lo - 1 + k << "\n";
            return;
        }

        k -= added;

        lo *= 10;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        solve();
}
SubtaskSumTestVerdictTimeMemory
base100/100
1Accepted0/03ms1832 KiB
2Accepted0/07ms2172 KiB
3Accepted10/103ms2264 KiB
4Accepted10/103ms2620 KiB
5Accepted10/103ms2572 KiB
6Accepted10/103ms2664 KiB
7Accepted10/102ms2752 KiB
8Accepted10/103ms2964 KiB
9Accepted10/107ms3332 KiB
10Accepted10/107ms3264 KiB
11Accepted10/107ms3392 KiB
12Accepted10/106ms3680 KiB