QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#625863 | #8932. Bingo | supepapupu# | WA | 0ms | 3716kb | C++20 | 4.3kb | 2024-10-09 21:29:03 | 2024-10-09 21:29:05 |
Judging History
answer
#include <bits/stdc++.h>
#define x first
#define y second
#define el '\n'
#define debug(x) cerr << #x << ": " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 3e5 + 10, INF = 0x3f3f3f3f, mod = 998244353;
vector<int> read() {
vector<int> k;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') {
k.emplace_back(c - '0');
c = getchar();
}
reverse(k.begin(), k.end());
return k;
}
void print(vector<int> &a) {
for (int i = a.size() - 1; i >= 0; --i) putchar('0' + a[i]);
puts("");
}
int cmp(const vector<int> &a, const vector<int> &b) {
if (a.size() > b.size()) return 1;
if (a.size() < b.size()) return -1;
for (int i = a.size() - 1; i >= 0; --i) {
if (a[i] > b[i]) return 1;
if (b[i] > a[i]) return -1;
}
return 0;
}
vector<int> mul(const vector<int> &a, ll k) {
vector<int> res(a.size());
ll t = 0;
for (int i = 0; i < a.size(); ++i) {
t += a[i] * k;
res[i] = t % 10;
t /= 10;
}
while (t) {
res.emplace_back(t % 10);
t /= 10;
}
return res;
}
vector<int> add(const vector<int> &a, ll k) {
ll t = k;
vector<int> res(a.size());
for (int i = 0; i < a.size(); ++i) {
t += a[i];
res[i] = t % 10;
t /= 10;
}
while (t) {
res.emplace_back(t % 10);
t /= 10;
}
return res;
}
void shrink(vector<int> &a) {
while (a.size() > 1 && !a.back()) a.pop_back();
}
vector<int> del(const vector<int> &a, ll k) {
ll t = -k;
vector<int> res(a.size());
for (int i = 0; i < a.size(); ++i) {
t += a[i];
res[i] = t % 10;
t /= 10;
if (res[i] < 0) res[i] += 10, --t;
}
assert(!t);
// while (t) {
// res.emplace_back(t % 10);
// t /= 10;
// }
shrink(res);
return res;
}
vector<int> div(const vector<int> &a, ll k) {
vector<int> res(a.size());
ll t = 0;
for (int i = a.size() - 1; i >= 0; --i) {
t += a[i];
res[i] = t / k;
t %= k;
t *= 10;
}
shrink(res);
return res;
}
void solve() {
vector<int> n = read();
ll m; scanf("%lld", &m);
vector<int> ans = mul(div(n, m), m);
ans = add(ans, m);
n = add(n, 1);
for (int i = 0; i < n.size(); ++i) {
int t = m;
int p = i, ok = 1;
while (t) {
if (p >= n.size() || n[p] != t % 10) {
ok = 0;
break;
}
t /= 10;
++p;
}
if (ok) return print(n);
}
ll pw = 1;
int p = 0;
vector<int> tt = n;
while (pw <= m) {
pw *= 10;
if (p < tt.size()) tt[p++] = 0;
}
shrink(tt);
ll mm = m;
while (pw <= 1e12) {
auto t = add(tt, mm);
if (cmp(t, n) < 0) t = add(t, pw);
if (cmp(t, ans) < 0) ans = move(t);
if (p < tt.size()) tt[p++] = 0, shrink(tt);
pw *= 10;
mm *= 10;
}
ll sum = 0;
ll mn = 0;
tt = n;
for (int i = 0; i < 20; ++i) tt.emplace_back(0);
vector<ll> pow(18);
pow[0] = 1;
for (int i = 1; i <= 17; ++i) pow[i] = pow[i - 1] * 10;
for (int i = 0; i < tt.size() - 20; ++i) {
int t = m;
int p = i, ok = 1;
ll res = 0;
while (t) {
if (t % 10 != tt[p]) {
if (p > 10) {
ok = 0;
break;
}
res += (t % 10 - tt[p]) * pow[p];
}
t /= 10;
++p;
}
if (res < 0) continue;
mn = min(mn, res + sum);
if (tt[i] && i > 10) break;
sum += tt[i] * pow[i];
tt[i] = 0;
++tt[i + 1];
p = i + 1;
while (tt[p] >= 10) ++tt[p + 1], tt[p] -= 10, ++p;
if (!ok) break;
}
auto ans2 = add(n, mn);
if (cmp(ans2, ans) < 0) ans = move(ans2);
print(ans);
}
int main() {
// ios::sync_with_stdio(0); cin.tie(0);
int tcase = 1;
cin >> tcase;
while (tcase--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3716kb
input:
6 7 3 12 3 9 10 249 51 1369 37 2 1
output:
8 13 10 250 1370 3
result:
wrong answer 1st lines differ - expected: '9', found: '8'