結果
問題 | No.362 門松ナンバー |
ユーザー | ikd |
提出日時 | 2018-10-05 07:25:30 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 455 ms / 3,000 ms |
コード長 | 1,852 bytes |
コンパイル時間 | 2,778 ms |
コンパイル使用メモリ | 98,944 KB |
実行使用メモリ | 7,424 KB |
最終ジャッジ日時 | 2024-11-08 18:53:42 |
合計ジャッジ時間 | 10,801 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
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[][][][](20, 2, 100, 4); afill(memo, -1L); long f(size_t i, bool less, int last2, int trailing) { if (i == n.length) { return trailing >= 3; } else { if (memo[i][less][last2][trailing] >= 0) { return memo[i][less][last2][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(last2 * 10 + d)) { ret += f(i + 1, l, (last2 * 10 + d) % 100, t); } } return memo[i][less][last2][trailing] = ret; } } return f(0, 0, 0, 0); } bool enough(long n) { auto kk = kadocount(n); return kk >= k; } long ng = 100, ok = 10L ^^ 18; 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; return (y - x) * (y - z) > 0 && x != z; } 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)); }