Source Code
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
using std::chrono::high_resolution_clock;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int>pi;
typedef pair<ll, ll>pl;
typedef vector<pi>vpi;
typedef vector<pl>vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string> vs;
const double PI = acos(-1);
const int oo = 1e9 + 7;
const int MOD = 1e9 + 7;
const int N = 1e6 + 1;
#define endl '\n'
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define read(v) for (auto& it : v) scanf("%d", &it);
#define readL(v) for (auto& it : v) scanf("%lld", &it);
#define print(v) for (auto it : v) printf("%d ", it); puts("");
#define printL(v) for (auto it : v) printf("%lld ", it); puts("");
auto Time() {
	return high_resolution_clock::now();
}
string longDivision(string s, int x) {
	string ans = "";
	int idx = 0, cur = s[idx] - '0';
	while (cur < x) {
		idx++;
		cur = (s[idx] - '0') + cur * 10;
	}
	while (idx < s.size()) {
		idx++;
		ans += char((cur / x) + '0');
		cur = (cur % x) * 10 + (s[idx] - '0');
	}
	return ans.size() == 0 ? "0" : ans;
}
string multiply(string s, string x) {
	int n = s.size(), m = x.size();
	if (!n || !m)
		return "0";
	vi ans(n + m, 0);
	int idx1 = 0, idx2 = 0;
	for (int i = n - 1; i >= 0; i--) {
		int cur = 0, num1 = s[i] - '0';
		idx2 = 0;
		for (int j = m - 1; j >= 0; j--) {
			int num2 = x[j] - '0', sum = num1 * num2 + ans[idx1 + idx2] + cur;
			cur = sum / 10;
			ans[idx1 + idx2] = sum % 10;
			idx2++;
		}
		if (cur > 0)
			ans[idx1 + idx2] += cur;
		idx1++;
	}
	int i = ans.size() - 1;
	while (i >= 0 && !ans[i])
		i--;
	if (i == -1)
		return "0";
	string ret = "";
	while (i >= 0) {
		ret += to_string(ans[i]);
		i--;
	}
	return ret;
}
void solve() {
	string x;
	int y;
	cin >> x >> y;
	string div = longDivision(x, y);
	if (multiply(div, to_string(y)) == x) {
		puts(x.c_str());
		return;
	}
	if (div.back() != '9')
		div.back()++;
	else {
		div.back() = '0';
		bool ok = false;
		for (int i = div.size() - 2; i >= 0; i--) {
			if (div[i] != '9') {
				div[i]++;
				ok = true;
				break;
			}
			else
				div[i] = '0';
		}
		if (!ok) {
			reverse(all(div));
			div += '1';
			reverse(all(div));
		}
	}
	cout << multiply(div, to_string(y)) << endl;
}
int t = 1;
int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	scanf("%d", &t);
	while (t--) solve();
}
Copy
AZOZ with an O (Needs a Carry) Malik
GNU G++17
723 ms
11.6 MB
Accepted