QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#300480 | #6300. Best Carry Player 2 | mendicillin2# | WA | 1ms | 3572kb | C++17 | 2.2kb | 2024-01-08 12:32:46 | 2024-01-08 12:32:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class F> struct ycr {
F f;
template <class T> explicit ycr(T&& f_) : f(forward<T>(f_)) {}
template <class... A> decltype(auto) operator()(A&&... as) {
return f(ref(*this), forward<A>(as)...);
}
};
template <class F> decltype(auto) yc(F&& f) {
return ycr<decay_t<F>>(forward<F>(f));
}
template <class T> void setmin(T& a, const T& b) {
if (b < a) a = b;
}
using ll = int64_t;
using i128 = __uint128_t;
string i128_to_string(i128 n) {
assert(n > 0);
string res;
while (n > 0) {
res += char(int(n % 10) + '0');
n /= 10;
}
reverse(res.begin(), res.end());
return res;
}
const int L = 37;
V<i128> p10(L);
string solve(ll X, int K) {
V<int> Xdigits(L);
{
auto t = X;
for (int l = 0; l < L; l++) {
Xdigits[l] = int(t % 10);
t /= 10;
}
assert(t == 0);
}
if (K == 0) {
int f = 0;
while (Xdigits[f] == 9) {
f++;
}
return string("1") + string(f, '0');
}
assert(K >= 1);
// dp(# of carries, carry) = (min of highest digit,
// state with the min of second highest digit)
const i128 INF = i128(-1);
VV<i128> dp(K+1, V<i128>(2, INF));
dp[0][0] = 0;
for (int l = 0; l+1 < L; l++) {
// TODO: Check feasibility of (l+1)-digit y
if (l > 0) {
i128 cnd = dp[K][0];
if (cnd < INF) {
return i128_to_string(cnd);
}
}
int vx = Xdigits[l];
VV<i128> ndp(K+1, V<i128>(2, INF));
for (int num_carries = 0; num_carries <= K; num_carries++) {
for (int carry : {0, 1}) {
if (dp[num_carries][carry] == INF) continue;
for (int vy = 0; vy < 10; vy++) {
int new_carry = (vx + vy + carry) / 10;
int new_num_carries = num_carries + new_carry;
if (new_num_carries <= K) {
setmin(ndp[new_num_carries][new_carry], dp[num_carries][carry] + vy * p10[l]);
}
}
}
}
swap(dp, ndp);
}
// ok
return string("-1");
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
cout << fixed << setprecision(20);
p10[0] = 1;
for (int l = 1; l < L; l++) {
p10[l] = p10[l-1] * i128(10);
}
int T;
cin >> T;
while (T--) {
ll X;
int K;
cin >> X >> K;
cout << solve(X, K) << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3560kb
input:
4 12345678 0 12345678 5 12345678 18 990099 5
output:
1 54322 999999999987654322 9910
result:
ok 4 lines
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3572kb
input:
21 999990000099999 0 999990000099999 1 999990000099999 2 999990000099999 3 999990000099999 4 999990000099999 5 999990000099999 6 999990000099999 7 999990000099999 8 999990000099999 9 999990000099999 10 999990000099999 11 999990000099999 12 999990000099999 13 999990000099999 14 999990000099999 15 999...
output:
100000 10000 1000 100 10 1 900001 9900001 99900001 999900001 10000000001 9999910000 9999901000 9999900100 9999900010 9999900001 9000009999900001 99000009999900001 999000009999900001 -1 1000000000000000000
result:
wrong answer 20th lines differ - expected: '99999999999999999900000000000000000', found: '-1'