結果
問題 | No.362 門松ナンバー |
ユーザー |
|
提出日時 | 2017-04-04 14:18:38 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 118 ms / 3,000 ms |
コード長 | 2,595 bytes |
コンパイル時間 | 694 ms |
コンパイル使用メモリ | 115,164 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-12 18:35:30 |
合計ジャッジ時間 | 3,117 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
import std.stdio, std.array, std.string, std.conv, std.algorithm;import std.typecons, std.range, std.random, std.math, std.container;import std.numeric, std.bigint, core.bitop;long dp(long n) {auto s = n.to!string;auto m = s.length.to!int;auto dp = new long[][][][][](m+1, 2, 2, 10, 10);foreach (a; 0..m+1)foreach (b; 0..2)foreach (c; 0..2)foreach (d; 0..10)foreach (e; 0..10)dp[a][b][c][d][e] = 0;dp[0][0][0][0][0] = 1;foreach (a; 0..m) {foreach (b; 0..2) {foreach (c; 0..2) {foreach (d; 0..10) {foreach (e; 0..10) {auto new_c = c || (d > 0);if (c && d == e) continue;int start = 0;int end = b ? 9 : s[a] - '0';if (new_c && d < e) end = min(end, e-1);if (new_c && d > e) start = max(start, e+1);foreach (digit; start..end+1) {if (c && digit == d) continue;//if (new_c && digit == d) continue;if (!c && digit == d && d > 0) continue;if (!c && digit == e && d > 0) continue;if (!c && digit == e && e > 0) continue;auto new_b = b || (digit < s[a]-'0');auto new_d = e;auto new_e = digit;//if(dp[a][b][c][d][e]) {// writeln(a, " ", b, " ", c, " ", d, " ", e ," ", digit);// writeln(a+1, " ", new_b, " ", new_c, " ", new_d, " ", new_e);//}dp[a+1][new_b][new_c][new_d][new_e] +=dp[a][b][c][d][e];}}}}}}long ret = 0;foreach (b; 0..2) {foreach (d; 0..10) {foreach (e; 0..10) {ret += dp[m][b][1][d][e];}}}return ret;}void main() {auto N = readln.chomp.to!int;foreach (i; 0..N) {auto T = readln.chomp.to!long;long hi = 37294859064823;long lo = -1;while (hi - lo > 1) {auto mid = (hi + lo) / 2;if (dp(mid) >= T) hi = mid;else lo = mid;}hi.writeln;}}