結果
| 問題 |
No.362 門松ナンバー
|
| コンテスト | |
| ユーザー |
ikd
|
| 提出日時 | 2018-10-05 06:25:16 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,032 bytes |
| コンパイル時間 | 884 ms |
| コンパイル使用メモリ | 98,944 KB |
| 実行使用メモリ | 13,440 KB |
| 最終ジャッジ日時 | 2024-06-13 01:46:57 |
| 合計ジャッジ時間 | 22,476 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 10 WA * 9 |
ソースコード
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
import std.array;
int t;
rd(t);
while (t--) {
long k;
rd(k);
long kadocount(long _n) {
int[] n;
do {
n = _n % 10 ~ n;
_n /= 10;
}
while (_n);
auto memo = new long[][][][](n.length + 1, 2, 1000, 4);
afill(memo, -1L);
long f(size_t i, bool less, int last3, int trailing) {
if (i == n.length) {
return trailing >= 3 && kadoma2(last3);
} else {
if (memo[i][less][last3][trailing] >= 0) {
return memo[i][less][last3][trailing];
}
long ret = 0;
auto digit = less ? 9 : n[i];
for (int d = 0; d <= digit; d++) {
auto l = less || (d < digit);
auto t = min(3, trailing + (trailing > 0 || d > 0));
if (i < 2 || t < 3 || kadoma2((last3 * 10 + d) % 1000)) {
ret += f(i + 1, l, (last3 * 10 + d) % 1000, t);
}
}
return memo[i][less][last3][trailing] = ret;
}
}
return f(0, 0, 0, 0);
}
bool enough(long n) {
auto kk = kadocount(n);
return kk >= k;
}
long ng = 100, ok = 10L ^^ 12;
while (ok - ng > 1) {
auto m = (ng + ok) / 2;
(enough(m) ? ok : ng) = m;
}
writeln(ok);
}
}
bool kadoma2(int n) {
int x = n / 100, y = n / 10 % 10, z = n % 10;
if (x == y || y == z || z == x) {
return false;
} else if (x < y && y > z) {
return true;
} else if (x > y && y < z) {
return true;
} else {
return false;
}
}
void afill(Range, Type)(Range r, Type value) {
static if (is(typeof(r) == Type[])) {
foreach (ref elem; r)
elem = value;
} else {
foreach (ref arr; r)
afill(arr, value);
}
}
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));
}
ikd