QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#210116 | #6300. Best Carry Player 2 | therehello | TL | 2010ms | 3904kb | C++20 | 2.0kb | 2023-10-11 01:55:01 | 2023-10-11 01:55:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using LL = __int128_t;
using i64 = long long;
LL pw10[36];
bool _ = []() {
pw10[0] = 1;
for (int i = 1; i <= 35; i++) pw10[i] = pw10[i - 1] * 10;
return 1;
}();
auto to_string(LL x) {
string s;
if (x == 0) s += "0";
while (x) {
s += x % 10 + '0';
x /= 10;
}
reverse(s.begin(), s.end());
return s;
}
struct cmp {
ll operator()(const array<ll, 2>& a) const { return (a[0] + a[1]) << 5 + (a[0] + a[1]) >> 7; }
};
void solve() {
map<array<ll, 2>, LL> mp;
auto dfs = [&](auto self, ll num, int k) -> LL {
if (k == 0) return 0;
if (mp.count({num, k})) return mp[{num, k}];
string s = to_string(num);
reverse(s.begin(), s.end());
vector<int> r(s.size(), s.size());
int last = s.size();
for (int i = s.size() - 1; i >= 0; --i) {
r[i] = last;
if (s[i] != '9') {
last = i;
}
}
LL ans = (LL(1) << 127) - 1;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '0') continue;
int x = 10 - (s[i] - '0');
auto pos = r[i];
int cnt = pos - i;
if (k >= cnt) {
ll now = num / pw10[pos];
now++;
ans = min((self(self, now, k - cnt) * pw10[cnt] + x) * pw10[i], ans);
}
}
return mp[{num, k}] = ans;
};
i64 x, k;
cin >> x >> k;
if (k == 0) {
string num = to_string(x);
reverse(num.begin(), num.end());
auto pos = find_if(num.begin(), num.end(), [](char ch) { return ch != '9'; }) - num.begin();
cout << (ll)pw10[pos] << "\n";
return;
}
cout << to_string(dfs(dfs, x, k)) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.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: 100
Accepted
time: 0ms
memory: 3656kb
input:
4 12345678 0 12345678 5 12345678 18 990099 5
output:
1 54322 999999999987654322 9910
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3888kb
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 99999999999999999900000000000000000 1000000000000000000
result:
ok 21 lines
Test #3:
score: 0
Accepted
time: 2010ms
memory: 3668kb
input:
100000 119111011091190000 10 1911011191011999 16 110099199000119 0 19009911191091011 13 199090909919000900 17 19009010011919110 5 90910190019900091 18 10911100000101111 1 110090011101119990 4 100909999119090000 12 90901119109011100 2 111010119991090101 4 900991019109199009 5 100919919990991119 8 911...
output:
88988908810000 8088988808988001 10 88808908989 9800909090080999100 80890 909089809980099909 9 80010 9090000880910000 8900 9909 991 9008900 8880880090 8080090801 8009900808909899 80880898981 909 8800909 99988889901 89908888089 980908890980099000 100 9889801 81 908890008099900891 880990801 9998099 890...
result:
ok 100000 lines
Test #4:
score: 0
Accepted
time: 2005ms
memory: 3904kb
input:
100000 900019901991919191 0 119199000910909911 18 190001990019910009 12 110091191100109001 6 10191091999110091 18 990011110190190090 5 900900000199919019 14 119100019910100099 17 909911111001111901 3 901100909911099 10 919009910101990019 7 911011009199199100 11 919111110190110999 0 91010111901190900...
output:
1 880800999089090089 9980089991 890999 989808908000889909 809910 99999800080990 80899980089899901 99 9090088901 8009981 8990800800900 1000 99089898880988091000 99101000 8988889980809 99008880080090009 10900 9998889909 889009889909901 808909990100 9090008889889089010 8099888899000 990898099098891000 ...
result:
ok 100000 lines
Test #5:
score: 0
Accepted
time: 1990ms
memory: 3684kb
input:
100000 90109000009190190 17 100091999119191900 13 119911991111100009 10 191199111011010091 12 99199110109099919 17 900011911110190199 7 990109199990010100 8 90191099199119101 13 101009019910001111 2 999199010010119119 1 91100991091900090 12 901100110901100900 4 10910910019101109 13 99011019901999010...
output:
909890999990809810 908000880808100 8888900991 888988990009 800889890900081 9809801 9990900 8900800880909 89 1 9008908099910 899100 9089980898891 80009899 1009 101 9899888080810 8808900888888981 80801 89890988080889809 88998009000900 90989809809890 889999100 988890810 98991 9900 9990008880980009100 8...
result:
ok 100000 lines
Test #6:
score: 0
Accepted
time: 2000ms
memory: 3848kb
input:
100000 190090090190009901 8 110911911110919010 16 101101009991110 1 901011000199900 5 900119091100999099 11 191011011019019001 2 101090009999910019 3 999100919001019919 9 9910090910011001 14 909091191100090191 18 101190999191199909 16 9190199191991 11 19909100909900001 9 10001910011001019 18 1900000...
output:
9990099 89088088889080990 90 9800100 8899000901 99 981 998980090 89909089989009 90908808899909809 8809000808800091 9800808009 90099999 989998089988998981 1 98089 8898980988900100 898890989900 989090 9 989890008909999 100 9099 9 100 89909900091 10 9880899008810 999880098900880991000 9809088998810089 ...
result:
ok 100000 lines
Test #7:
score: 0
Accepted
time: 1986ms
memory: 3612kb
input:
100000 900101011999010011 17 109019011901009119 6 100001099109091110 12 909900011111999190 17 9191909111910001 17 99119900909191199 15 919999901110090900 11 910099099001091991 5 91019109990991191 13 911019000990111110 1 999099011119100011 1 99199110111911199 11 190111991190901100 4 91011190910911090...
output:
99898988000989990 990881 8900890908890 90099988888000810 90808090888089999 880099090808900 98890010100 8009 890009008809 90 9 89888088801 98900 890889100 988998880990989 98908090 910 899009998809889 990099080809100 9 8980998909898099 800090910 910 9 900 809888909898888810 10 99091 9909998909081 8989...
result:
ok 100000 lines
Test #8:
score: -100
Time Limit Exceeded
input:
100000 556678926267652258 2 929929657746014640 13 110808054835665500 13 603411220842314620 18 236920943881957481 13 228392045048443659 0 800016407284399949 4 891810444049711534 10 673038060913134012 8 497925775526719327 2 320546054458668782 7 22937449060095980 7 592739113092913372 15 378621488315118...
output:
42 70342253986000 191945164334500 9396588779157685380 9056118042519 10 60 5950288466 86865990 73 1331218 39904020 260886907086628 21378511684881483 27 74912313591831403 202572281666000677 10 2445141061145 1057747266722529 93751641 633355727 6592761984728 5909230 346570 397717429 97513000559636020 27...