QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#557035 | #2835. Number Theory | ucup-team1198 | WA | 126ms | 7960kb | C++20 | 5.4kb | 2024-09-11 00:26:42 | 2024-09-11 00:26:44 |
Judging History
answer
#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
const int INF = 1e9;
const int MAXN = 5e3 + 100;
const int MAXD = 30;
int pre_dp[2][2 * 6 * MAXN][2 * MAXD + 1];
int dp[2][2 * 6 * MAXN][3];
int lst(int x) {
int res = x % 10;
if (res < 0) res += 10;
return res;
}
int rest(int x) {
return (x - lst(x)) / 10;
}
int rest9(int x) {
int r = x % 9;
if (r < 0) r += 9;
return (x - r) / 9;
}
const int KK = 10;
int solve(string ns) {
reverse(ns.begin(), ns.end());
ns.push_back('0');
ns.push_back('0');
ns.push_back('0');
vector<int> n;
for (char ch : ns) {
n.push_back(ch - '0');
}
int l = n.size();
int maxc = 5 * l + 30;
int maxd = (maxc + 89) / 90;
for (int i = 0; i <= 1; ++i) {
for (int j = -maxc; j <= maxc; ++j) {
fill(dp[i][j + maxc], dp[i][j + maxc] + 2 * MAXD + 1, INF);
}
}
for (int c = -maxc; c <= maxc; ++c) {
fill(pre_dp[0][c + maxc], pre_dp[0][c + maxc] + 2 * MAXD + 1, INF);
}
for (int c = -maxc; c <= maxc; ++c) {
if (lst(c) != n[0])
continue;
int carry = rest(c);
int val = 0;
int rem = lst(n[1] - carry);
int start = rest(c - rem) * 10 + rem;
for (int c1 = start; c1 <= c + 6; c1 += 10) {
int val1 = val + abs(c - c1);
int carry1 = rest(carry + c1);
int delta1 = carry1 - rest9(c1);
//cerr << "to " << 2 << ' ' << c1 << ' ' << delta1 << ' ' << val1 << '\n';
pre_dp[0][maxc + c1][MAXD + delta1] = min(pre_dp[0][maxc + c1][MAXD + delta1], val1);
}
}
maxd = (maxd + 10) / 10;
for (int i = 2; i < min(KK, l); ++i) {
for (int c = -maxc; c <= maxc; ++c) {
fill(pre_dp[(i + 1) & 1][c + maxc] + MAXD - maxd - 1, pre_dp[(i + 1) & 1][c + maxc] + MAXD + maxd + 1, INF);
}
int cur_maxc = (l - i) * 5 + 10;
for (int c = -cur_maxc; c <= cur_maxc; ++c) {
for (int delta = -maxd; delta <= maxd; ++delta) {
int val = pre_dp[i & 1][c + maxc][delta + MAXD];
if (val == INF) continue;
//cerr << i << ' ' << c << ' ' << delta << ' ' << val << '\n';
int carry = rest9(c) + delta;
int rem = lst(n[i] - carry);
int start = rest(c - rem) * 10 + rem;
for (int c1 = start; c1 <= c + 6; c1 += 10) {
int val1 = val + i * abs(c - c1);
int carry1 = rest(carry + c1);
int delta1 = carry1 - rest9(c1);
//cerr << " to " << i + 1 << ' ' << c1 << ' ' << delta1 << ' ' << val1 << '\n';
pre_dp[(i + 1) & 1][maxc + c1][MAXD + delta1] = min(pre_dp[(i + 1) & 1][maxc + c1][MAXD + delta1], val1);
}
}
}
maxd = (maxd + 10) / 10;
}
for (int c = -maxc; c <= maxc; ++c) {
for (int d = -1; d <= 1; ++d)
dp[min(KK, l) & 1][c + maxc][1 + d] = pre_dp[min(KK, l) & 1][c + maxc][MAXD + d];
}
for (int i = KK; i < l; ++i) {
int cur_maxc = (l - i) * 5 + 10;
for (int c = -cur_maxc; c <= cur_maxc; ++c) {
fill(dp[(i + 1) & 1][c + maxc], dp[(i + 1) & 1][c + maxc] + 3, INF);
}
int min_d = -1, max_d = 1;
for (int c = -cur_maxc; c <= cur_maxc; ++c) {
for (int delta = min_d; delta <= max_d; ++delta) {
int val = dp[i & 1][c + maxc][delta + 1];
if (val == INF) continue;
min_d = delta;
max_d = delta;
int carry = rest9(c) + delta;
int rem = lst(n[i] - carry);
int start = rest(c - rem) * 10 + rem;
for (int c1 = start; c1 <= c + 5; c1 += 10) {
int val1 = val + i * abs(c - c1);
int carry1 = rest(carry + c1);
int delta1 = carry1 - rest9(c1);
dp[(i + 1) & 1][maxc + c1][1 + delta1] = min(dp[(i + 1) & 1][maxc + c1][1 + delta1], val1);
}
}
}
}
int ans = INF;
for (int i = -maxc; i <= maxc; ++i) {
for (int delta = -1; delta <= 1; ++delta) {
int carry = rest9(i) + delta;
if (carry == 0) {
ans = min(ans, dp[l & 1][i + maxc][1 + delta]);
}
}
}
return ans;
}
//#define STRESS
//#define RANDOM
#ifdef STRESS
const int LEN = 7;
const int K = 500000;
int precalc[K];
void gen(int i, int cur, int cost, int num) {
if (i == LEN) {
if (0 < cur && cur < K) {
precalc[cur] = min(precalc[cur], cost);
}
return;
}
for (int j = -7; j <= 7; ++j) {
gen(i + 1, cur + j * num, cost + abs(j) * i, num * 10 + 1);
}
}
int dumb(string n) {
int x = stoi(n);
return precalc[x];
}
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
#if defined(STRESS)
fill(precalc, precalc + K, INF);
gen(1, 0, 0, 1);
for (int n = 1; n < K; ++n) {
string ns = to_string(n);
int ans1 = solve(ns);
int ans2 = dumb(ns);
if (ans1 != ans2) {
cerr << "BUG!!\n";
cerr << ns << '\n';
cerr << "wrong: " << ans1 << " right: " << ans2 << "\n\n\n";
break;
}
}
#elif defined(RANDOM)
const int L = 5000;
string n;
for (int i = 0; i < L; ++i)
n += char('0' + rand() % 10);
cout << solve(n) << '\n';
#else
string n;
while (cin >> n) {
cout << solve(n) << '\n';
}
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7896kb
input:
12 100 998244353
output:
3 5 76
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 109ms
memory: 7960kb
input:
4296 5370 8014 9532 6311 4070 2541 4782 5630 1487 2916 454 2020 5650 1851 5885 3556 6533 5044 1780 5746 5605 7860 4416 4495 8081 2416 3534 6045 3348 4536 8725 3505 1074 1531 937 7954 4451 7052 9586 3468 2679 6085 9908 3630 8046 6282 2883 9021 1436 5201 8166 8986 2167 7780 4156 101 3753 5732 5173 118...
output:
29 36 28 27 35 36 22 30 32 22 33 15 20 30 28 32 19 29 40 22 34 34 30 26 30 35 24 23 42 18 26 28 31 14 22 22 33 22 43 30 21 24 42 19 30 33 43 30 21 18 34 36 21 18 19 34 6 30 35 42 14 27 36 32 23 25 30 35 28 22 32 27 41 39 26 37 30 28 39 35 22 24 32 44 35 36 35 42 22 22 31 35 25 32 32 15 17 33 38 31 2...
result:
ok 9999 lines
Test #3:
score: 0
Accepted
time: 120ms
memory: 7760kb
input:
16528 11452 14198 11877 11309 14176 10807 19189 12063 11011 11618 18292 19862 13144 16834 16652 14120 17091 19836 16498 17655 19144 14088 13370 12709 17115 14482 10958 12306 17891 17979 10705 10036 17976 12035 18042 17495 14498 10163 15926 12209 18908 13033 19875 11699 14361 14785 14253 12647 14726 ...
output:
37 19 29 21 17 33 24 37 24 10 32 47 30 25 40 30 30 44 33 40 32 34 31 23 30 42 28 18 21 31 37 28 18 38 23 42 48 28 25 41 13 29 28 26 26 30 31 31 28 38 26 20 30 36 43 31 40 40 38 31 27 26 28 35 33 39 19 24 36 36 20 20 27 32 43 23 20 28 29 40 31 35 25 20 31 32 40 41 40 41 33 39 39 42 37 12 21 10 30 39 ...
result:
ok 10000 lines
Test #4:
score: 0
Accepted
time: 124ms
memory: 7660kb
input:
25172 29204 28892 23128 21312 28850 23340 27350 21632 29705 29655 24700 27992 23570 28822 21683 26260 26497 28092 22514 28528 23098 26446 27220 22261 28727 28177 23884 28830 24460 26841 29986 23893 28374 24608 22374 22731 26925 22753 24241 20202 24668 23072 21788 27823 23938 23414 22339 20247 20546 ...
output:
45 48 34 28 25 43 20 50 33 47 39 31 42 26 42 32 49 42 47 30 50 24 34 42 22 48 50 33 45 25 43 32 34 55 33 24 35 42 31 31 30 26 32 25 40 43 26 19 31 32 36 42 50 35 51 36 39 22 27 34 47 48 42 40 46 31 37 43 29 37 49 48 33 26 13 40 46 43 34 40 38 47 35 30 24 26 33 49 27 46 29 28 46 33 34 37 53 42 42 42 ...
result:
ok 10000 lines
Test #5:
score: 0
Accepted
time: 126ms
memory: 5652kb
input:
35458 30814 33590 33177 30718 35952 39315 37797 38544 32595 34013 36806 38978 37445 32117 35080 36177 36811 38996 33557 35833 36013 31989 33626 30095 30732 32497 32972 39513 35802 38572 36304 37335 39638 30390 30443 30181 37677 30341 33589 34504 37365 38395 38755 32061 37743 36823 34880 31171 38806 ...
output:
31 41 29 27 47 46 55 37 46 40 35 40 43 41 28 43 48 37 41 23 40 40 27 36 37 42 37 34 57 36 52 46 45 55 48 40 44 36 41 28 34 50 60 42 36 38 40 36 38 48 32 46 37 49 33 35 48 34 41 41 32 37 49 29 32 40 53 48 42 26 30 40 35 37 30 43 56 32 37 32 32 24 33 39 48 37 39 35 46 52 53 57 35 24 33 40 34 45 53 33 ...
result:
ok 10000 lines
Test #6:
score: 0
Accepted
time: 121ms
memory: 5728kb
input:
47583 45587 40557 41413 48126 43227 41361 49755 42400 48511 46835 42685 42425 44853 41732 45421 44157 41929 47256 42185 49905 47237 45004 40732 41670 44069 40082 42147 49510 40690 45655 41481 48052 40609 43269 48612 46900 46706 40691 48979 49898 46602 43411 47434 42544 40816 49880 40217 45017 47779 ...
output:
48 31 48 49 48 32 48 50 42 53 44 47 41 40 50 32 39 49 51 38 51 50 40 51 46 39 49 40 61 49 29 51 50 57 38 53 37 40 50 45 46 42 33 44 39 52 48 47 43 34 45 32 42 43 38 41 44 53 31 48 51 45 41 55 29 41 42 36 42 47 38 54 37 35 40 34 45 45 35 42 41 42 33 37 44 56 41 54 29 50 37 51 47 50 39 47 40 39 55 40 ...
result:
ok 10000 lines
Test #7:
score: 0
Accepted
time: 126ms
memory: 7724kb
input:
55562 56846 58105 54937 52692 50178 54319 54915 50557 52656 56355 57363 54038 52812 53176 59197 57326 59159 59531 57845 56859 57711 55867 50764 56134 52439 55360 52162 57155 52264 53214 59034 55078 59088 57901 56623 55475 56947 56875 59270 57531 56644 55171 54900 53866 59246 55120 50452 57034 58555 ...
output:
31 45 47 54 57 55 37 50 57 52 42 54 52 55 46 52 51 56 59 45 45 45 39 55 46 48 40 52 50 47 41 48 43 47 40 38 36 49 39 59 45 33 48 44 49 53 41 56 46 46 46 53 49 44 46 45 40 45 62 40 55 56 50 45 40 47 44 52 49 59 49 50 53 41 32 43 54 47 50 44 44 43 49 40 52 55 42 31 41 55 49 42 54 41 58 45 53 46 50 51 ...
result:
ok 10000 lines
Test #8:
score: 0
Accepted
time: 126ms
memory: 5656kb
input:
68733 60810 67393 64496 65059 60979 63486 67951 63442 63390 62976 67541 63288 61770 63615 62708 63675 64462 60143 67574 65757 62877 69434 60667 68109 65801 61749 60594 62448 61977 60014 69238 63419 63440 60449 64529 65693 65650 69566 62932 64440 64388 62977 67095 69177 65421 67034 63159 67234 64999 ...
output:
46 60 52 48 54 53 52 49 44 49 54 42 48 58 58 65 52 42 50 44 41 54 52 58 41 41 63 68 51 54 47 50 49 46 59 46 41 41 53 60 39 46 53 45 52 39 42 55 44 47 47 44 51 54 40 61 41 48 41 52 49 50 42 41 35 54 49 42 48 54 56 42 63 43 50 41 51 58 57 57 43 41 54 59 63 66 56 45 43 63 65 41 43 38 57 54 47 43 46 51 ...
result:
ok 10000 lines
Test #9:
score: 0
Accepted
time: 126ms
memory: 7724kb
input:
70451 77409 73192 73579 75086 78154 78238 75957 78550 75617 78564 73870 74725 70590 73332 74045 70687 76389 79692 74760 73538 74386 77646 76853 75878 79897 76404 71876 76587 73889 76008 73574 77829 72673 76321 74783 75211 70235 70292 74476 74216 71549 78231 77353 79686 71120 71752 78548 77730 70493 ...
output:
56 39 48 48 49 39 40 50 40 45 39 54 52 61 39 53 60 42 45 50 51 47 29 40 40 37 42 55 36 49 46 51 35 59 38 47 41 48 58 41 45 60 37 40 45 46 62 39 33 63 48 30 53 47 42 44 44 49 47 48 41 57 50 40 63 45 48 44 47 54 51 51 48 43 59 49 33 42 46 39 53 42 32 38 44 43 64 50 56 46 56 41 46 33 48 34 57 42 58 53 ...
result:
ok 10000 lines
Test #10:
score: 0
Accepted
time: 121ms
memory: 5604kb
input:
86492 82042 82395 85456 82379 83405 84464 80074 82438 88208 81516 82815 87181 88276 81288 89085 80748 82539 84837 88160 85818 83695 89219 89825 81747 88241 84428 87307 83517 81371 80557 89236 80342 83252 82503 89808 86584 82599 80539 86491 84521 81155 86352 80720 86012 87141 86667 83075 80697 80963 ...
output:
41 53 56 33 50 51 39 42 49 37 57 60 45 43 48 30 51 53 55 42 46 55 28 37 57 39 40 41 54 50 46 30 42 49 57 35 37 54 51 40 43 41 41 55 43 45 24 52 54 46 32 42 44 34 46 36 43 53 44 27 42 47 45 52 41 39 61 57 35 25 52 43 38 50 41 54 37 47 26 44 35 30 39 28 49 42 49 26 51 28 53 35 45 43 37 44 52 53 54 32 ...
result:
ok 10000 lines
Test #11:
score: 0
Accepted
time: 126ms
memory: 7692kb
input:
98444 95686 99650 91810 92829 90145 94267 90093 95597 92642 94279 97359 91044 99840 92439 97572 99845 92132 92676 96111 91222 97743 96688 99396 95545 95983 98381 98418 92309 92481 98112 92293 94426 90088 99648 92689 93574 96073 94906 98190 97349 91640 98879 92966 94198 94040 98573 98144 91707 93616 ...
output:
28 37 28 47 56 30 45 31 38 46 44 38 35 27 39 32 22 36 43 39 27 27 28 39 29 47 41 36 37 44 29 43 38 28 27 45 45 50 51 38 37 47 18 48 44 55 32 34 50 54 30 30 51 41 45 32 47 32 41 19 49 46 42 30 27 54 37 37 33 43 29 36 48 35 52 47 28 28 29 30 38 46 45 34 35 45 41 45 41 51 45 38 40 51 23 27 35 34 54 51 ...
result:
ok 10000 lines
Test #12:
score: -100
Wrong Answer
time: 55ms
memory: 7732kb
input:
99647655441310714404533248973861508920 2073756854781 97786932 697203586561561 220 92603944 857 4913 37162832688008526170899875515887671731826673 1480102698509 402012655841707502050341636832 75136137088485508854715245558132922867818030309 4114635151757 770 75 82646730706547052 496224488355898625 2477...
output:
1987 213 59 406 8 122 17 35 2713 194 1267 2802 247 18 12 460 488 1402 13 378 108 3241 111 2261 119 4118 58 2410 785 3003 65 26 1582 2007 57 153 445 64 247 876 3412 78 1392 518 390 1031 1708 215 706 3254 1407 1640 1827 2237 108 655 8 1669 1823 57 536 44 1798 1800 1391 22 600 11 585 138 1465 544 3427 ...
result:
wrong answer 1st lines differ - expected: '1421', found: '1987'