QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#611007 | #8932. Bingo | ucup-team4975 | WA | 0ms | 3612kb | C++23 | 3.0kb | 2024-10-04 18:46:17 | 2024-10-04 18:46:18 |
Judging History
answer
// #define LOCAL
#include <bits/stdc++.h>
#define FINISH cout << "FINISH" << endl;
#define fir first
#define sec second
#define el '\n'
using namespace std;
using PII = pair<int, int>;
using ll = long long;
const int N = 1000010;
void solve()
{
string s1;
ll m, res = 0;
cin >> s1 >> m;
for (int i = s1.size() - 1; i >= 0; i--) {
if (s1[i] != '9') {
// cout << "! " << i << endl;
s1[i] = char(s1[i] + 1);
break;
}
else if (i != 0) {
s1[i] = '0';
}
else {
s1[i] = '0';
s1 = '1' + s1;
}
}
string s2 = to_string(m);
for (int i = 0; i < s1.size(); i++) {
res = res * 10 + (s1[i] - '0');
res = res % m;
}
ll ans = m - res;
if (ans == m)
ans = 0;
// cout << s1 << " " << s2 << " " << ans << endl;
for (int i = 0; i <= s1.size(); i++) {
if (i + s2.size() - 1 >= s1.size())
break;
int flag = 1;
for (int j = 0, k = i; j < s2.size(); j++, k++) {
if (s1[k] != s2[j]) {
flag = 0;
break;
}
}
if (flag == 1) {
cout << s1 << el;
return;
}
}
// FINISH
// cout << s2.size() << endl;
// cout << s1 << " " << s2 << endl;
for (int i = min(17, (int)s1.size() - 1); i >= (int)s2.size() - 1; i--) {
// cout << i << endl;
int st = s1.size() - 1 - i + 1;
ll sum1 = m, sum3 = m;
for (int j = i - s2.size(); j >= 0; j--) {
sum1 = sum1 * 10 + 9;
sum3 = sum3 * 10 + (s1[s1.size() - 1 - j] - '0');
/*cout << j << " " << sum1 << " " << sum3 << " " << s1.size() - 1 -
j
<< " " << s1[s1.size() - 1 - j] << endl;*/
}
// cout << endl;
ll sum2 = 0, _10 = 1;
// cout << int(s1.size() - 1 - i) << " " << int(s1.size() - 1) << endl;
for (int j = s1.size() - 1 - i; j <= s1.size() - 1; j++) {
// cout << " ! " << j << endl;
sum2 = sum2 * 10 + (s1[j] - '0');
_10 = _10 * 10;
}
if (sum2 < sum1) {
ans = min(ans, sum1 - sum2);
}
else
ans = min(ans, sum2 + _10 - sum1);
/*cout << i << " " << sum1 << " " << sum2 << " " << sum3 << " " << _10
<< endl;*/
}
cout << ans << endl;
s2 = to_string(ans);
s1 = "0000000000" + s1;
if (s1.size() < s2.size()) {
swap(s1, s2);
}
// cout << s1 << " " << s2 << endl;
for (int i = s1.size() - 1, j = s2.size() - 1; i >= 0 && j >= 0; i--, j--) {
s1[i] = ((s1[i] - '0') + (s2[j] - '0'));
if (s1[i] >= 10) {
s1[i] = char(s1[i] - 10 + '0');
s1[i - 1]++;
}
else
s1[i] = char(s1[i] + '0');
}
// cout << s1 << endl;
for (int i = s1.size() - 1; i > 0; i--) {
if (s1[i] - '0' >= 10) {
s1[i] = char(s1[i] - 10);
s1[i - 1]++;
}
}
int st = 0;
for (int i = 0; i <= s1.size(); i++) {
if (s1[i] == '0')
continue;
else {
st = i;
break;
}
}
// cout << st << endl;
for (int i = st; i < s1.size(); i++)
cout << s1[i];
cout << el;
// FINISH
// cout << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3612kb
input:
6 7 3 12 3 9 10 249 51 1369 37 2 1
output:
1 9 13 10 1 251 1370 0 3
result:
wrong answer 1st lines differ - expected: '9', found: '1'