Submission #3325611
Source Code Expand
void main() { import std.stdio, std.string, std.conv, std.algorithm; long a; int k; rd(a, k); long _a = a; int[] n; while (_a) { n = (_a % 10) ~ n; _a /= 10; } long[] pow = [1]; foreach (_; 0 .. 15) { pow ~= pow[$ - 1] * 10; } import core.bitop : popcnt; const long inf = 10L ^^ 18; auto memo = new long[][][][](n.length + 1, 2, 1 << 10, 2); auto memo2 = new long[][][][](n.length + 1, 2, 1 << 10, 2); foreach (i; 0 .. (n.length + 1)) foreach (j; 0 .. 2) foreach (x; 0 .. (1 << 10)) foreach (y; 0 .. 2) memo[i][j][x][y] = -inf, memo2[i][j][x][y] = -inf; long f(size_t i, bool less, int used, bool zero) { // a以下で最大 if (i == n.length) { return 0; } else { if (memo[i][less][used][zero] > -inf) return memo[i][less][used][zero]; long ret = -1; auto digit = less ? 9 : n[i]; for (int d = 0; d <= digit; d++) { auto l = less || (d < digit); auto z = zero || (d > 0); auto u = !z ? 0 : (used | (1 << d)); if (popcnt(u) <= k) { auto res = f(i + 1, l, u, z); if (res >= 0) { // res = -1 ならアレ ret = max(ret, d * pow[n.length - i - 1] + res); } } } return memo[i][less][used][zero] = ret; } } long f2(size_t i, bool greater, int used, bool zero) { // a以上で最小 if (i == n.length) { return 0; } else { if (memo2[i][greater][used][zero] >= 0) return memo2[i][greater][used][zero]; long ret = inf; auto digit = greater ? 0 : n[i]; for (int d = digit; d <= 9; d++) { auto g = greater || (d > digit); auto z = zero || (d > 0); auto u = !z ? 0 : (used | (1 << d)); if (popcnt(u) <= k) { ret = min(ret, d * pow[n.length - i - 1] + f2(i + 1, g, u, z)); } } return memo2[i][greater][used][zero] = ret; } } writeln(min(a - f(0, 0, 0, 0), f2(0, 0, 0, 0) - a)); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
Submission Info
Submission Time | |
---|---|
Task | D - 壊れた電卓 |
User | ikd |
Language | D (DMD64 v2.070.1) |
Score | 100 |
Code Size | 2319 Byte |
Status | AC |
Exec Time | 16 ms |
Memory | 7036 KB |
Judge Result
Set Name | sub | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 30 / 30 | 70 / 70 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
sub | test_01A.txt, test_02A.txt, test_04A.txt, test_05A.txt, test_07A.txt, test_09A.txt, test_10A.txt, test_11A.txt, test_12A.txt, test_13A.txt, test_15A.txt, test_17A.txt, test_18A.txt, test_19A.txt, test_21A.txt, test_22A.txt, test_23A.txt, test_25A.txt, test_27A.txt, test_28A.txt, test_29A.txt, test_31A.txt, test_33A.txt, test_34A.txt, test_35A.txt, test_37A.txt, test_38A.txt, test_40A.txt, test_42A.txt, test_44A.txt, test_46A.txt, test_48A.txt |
All | sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, test_01A.txt, test_02A.txt, test_03.txt, test_04A.txt, test_05A.txt, test_06.txt, test_07A.txt, test_08.txt, test_09A.txt, test_10A.txt, test_11A.txt, test_12A.txt, test_13A.txt, test_14.txt, test_15A.txt, test_16.txt, test_17A.txt, test_18A.txt, test_19A.txt, test_20.txt, test_21A.txt, test_22A.txt, test_23A.txt, test_24.txt, test_25A.txt, test_26.txt, test_27A.txt, test_28A.txt, test_29A.txt, test_30.txt, test_31A.txt, test_32.txt, test_33A.txt, test_34A.txt, test_35A.txt, test_36.txt, test_37A.txt, test_38A.txt, test_39.txt, test_40A.txt, test_41.txt, test_42A.txt, test_43.txt, test_44A.txt, test_45.txt, test_46A.txt, test_47.txt, test_48A.txt, test_49.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
sample_01.txt | AC | 4 ms | 1276 KB |
sample_02.txt | AC | 5 ms | 1788 KB |
sample_03.txt | AC | 7 ms | 3836 KB |
sample_04.txt | AC | 5 ms | 3324 KB |
test_01A.txt | AC | 2 ms | 636 KB |
test_02A.txt | AC | 5 ms | 1788 KB |
test_03.txt | AC | 12 ms | 5756 KB |
test_04A.txt | AC | 2 ms | 636 KB |
test_05A.txt | AC | 5 ms | 1788 KB |
test_06.txt | AC | 16 ms | 7036 KB |
test_07A.txt | AC | 4 ms | 1532 KB |
test_08.txt | AC | 10 ms | 3580 KB |
test_09A.txt | AC | 2 ms | 636 KB |
test_10A.txt | AC | 4 ms | 3324 KB |
test_11A.txt | AC | 4 ms | 1276 KB |
test_12A.txt | AC | 3 ms | 892 KB |
test_13A.txt | AC | 5 ms | 1532 KB |
test_14.txt | AC | 15 ms | 4860 KB |
test_15A.txt | AC | 3 ms | 892 KB |
test_16.txt | AC | 8 ms | 2428 KB |
test_17A.txt | AC | 4 ms | 1276 KB |
test_18A.txt | AC | 5 ms | 1532 KB |
test_19A.txt | AC | 5 ms | 1532 KB |
test_20.txt | AC | 11 ms | 5372 KB |
test_21A.txt | AC | 4 ms | 1276 KB |
test_22A.txt | AC | 5 ms | 3452 KB |
test_23A.txt | AC | 3 ms | 1148 KB |
test_24.txt | AC | 9 ms | 4604 KB |
test_25A.txt | AC | 4 ms | 1532 KB |
test_26.txt | AC | 12 ms | 4732 KB |
test_27A.txt | AC | 4 ms | 1276 KB |
test_28A.txt | AC | 4 ms | 3324 KB |
test_29A.txt | AC | 3 ms | 892 KB |
test_30.txt | AC | 10 ms | 5244 KB |
test_31A.txt | AC | 4 ms | 1276 KB |
test_32.txt | AC | 13 ms | 5244 KB |
test_33A.txt | AC | 2 ms | 636 KB |
test_34A.txt | AC | 5 ms | 1532 KB |
test_35A.txt | AC | 5 ms | 1532 KB |
test_36.txt | AC | 7 ms | 2428 KB |
test_37A.txt | AC | 2 ms | 636 KB |
test_38A.txt | AC | 5 ms | 1788 KB |
test_39.txt | AC | 12 ms | 5756 KB |
test_40A.txt | AC | 5 ms | 1532 KB |
test_41.txt | AC | 15 ms | 5628 KB |
test_42A.txt | AC | 5 ms | 3196 KB |
test_43.txt | AC | 14 ms | 4860 KB |
test_44A.txt | AC | 4 ms | 3196 KB |
test_45.txt | AC | 11 ms | 4988 KB |
test_46A.txt | AC | 4 ms | 1532 KB |
test_47.txt | AC | 12 ms | 5756 KB |
test_48A.txt | AC | 5 ms | 3452 KB |
test_49.txt | AC | 11 ms | 5244 KB |