Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--) {
        string x;
        int y;
        cin >> x >> y;
        ll pw = 1;
        int sum = 0;
        int n = x.size();
        for (int i = n - 1; i >= 0; --i) {
            sum = (sum + pw * (x[i] - '0')) % y;
            pw = (pw * 10) % y;
        }
        string need = to_string((y - sum) % y);
        string ans;
        int carry = 0;
        while (need.size() && x.size()) {
            int a = need.back() - '0';
            int b = x.back() - '0';
            carry += a + b;
            ans += (char)(carry % 10 + '0');
            carry /= 10;
            need.pop_back();
            x.pop_back();
        }
        while (x.size()) {
            int a = x.back() - '0';
            carry += a;
            ans += (char)(carry % 10 + '0');
            carry /= 10;
            x.pop_back();
        }
        while (need.size()) {
            int a = need.back() - '0';
            carry += a;
            ans += (char)(carry % 10 + '0');
            carry /= 10;
            need.pop_back();
        }
        reverse(ans.begin(), ans.end());
        if (carry) ans = to_string(carry) + ans;
        cout << ans << '\n';
    }
    return 0;
}
Copy
AZOZ with an O (Needs a Carry) Heartbeat
GNU G++17
256 ms
11.2 MB
Accepted