結果

問題 No.362 門松ナンバー
ユーザー nebukuro09nebukuro09
提出日時 2017-04-04 14:18:38
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 129 ms / 3,000 ms
コード長 2,595 bytes
コンパイル時間 632 ms
コンパイル使用メモリ 105,188 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 12:40:44
合計ジャッジ時間 3,731 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
4,376 KB
testcase_01 AC 32 ms
4,380 KB
testcase_02 AC 28 ms
4,380 KB
testcase_03 AC 15 ms
4,380 KB
testcase_04 AC 9 ms
4,380 KB
testcase_05 AC 45 ms
4,376 KB
testcase_06 AC 77 ms
4,376 KB
testcase_07 AC 44 ms
4,376 KB
testcase_08 AC 59 ms
4,380 KB
testcase_09 AC 96 ms
4,376 KB
testcase_10 AC 125 ms
4,380 KB
testcase_11 AC 120 ms
4,376 KB
testcase_12 AC 120 ms
4,376 KB
testcase_13 AC 119 ms
4,380 KB
testcase_14 AC 120 ms
4,376 KB
testcase_15 AC 123 ms
4,376 KB
testcase_16 AC 118 ms
4,380 KB
testcase_17 AC 101 ms
4,376 KB
testcase_18 AC 129 ms
4,380 KB
testcase_19 AC 67 ms
4,376 KB
testcase_20 AC 129 ms
4,376 KB
testcase_21 AC 15 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
    }
}
0