結果

問題 No.362 門松ナンバー
ユーザー maspymaspy
提出日時 2020-03-12 23:12:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 993 ms / 3,000 ms
コード長 1,151 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 82,440 KB
実行使用メモリ 247,148 KB
最終ジャッジ日時 2024-04-30 13:25:54
合計ジャッジ時間 11,845 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
77,736 KB
testcase_01 AC 141 ms
78,736 KB
testcase_02 AC 123 ms
78,524 KB
testcase_03 AC 125 ms
77,988 KB
testcase_04 AC 122 ms
78,100 KB
testcase_05 AC 229 ms
90,556 KB
testcase_06 AC 361 ms
117,268 KB
testcase_07 AC 352 ms
111,288 KB
testcase_08 AC 427 ms
130,748 KB
testcase_09 AC 655 ms
180,640 KB
testcase_10 AC 931 ms
247,148 KB
testcase_11 AC 910 ms
231,908 KB
testcase_12 AC 993 ms
245,256 KB
testcase_13 AC 901 ms
244,944 KB
testcase_14 AC 902 ms
231,560 KB
testcase_15 AC 909 ms
246,380 KB
testcase_16 AC 545 ms
155,552 KB
testcase_17 AC 427 ms
129,736 KB
testcase_18 AC 557 ms
158,024 KB
testcase_19 AC 118 ms
78,780 KB
testcase_20 AC 553 ms
156,116 KB
testcase_21 AC 216 ms
87,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3.8
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from functools import lru_cache


# %%
def is_kadomatsu(a, b, c):
    if a == c:
        return False
    return (a < b > c) or (a > b < c)


# %%
@lru_cache(None)
def count_kadomatsu(N, x):
    """counting kadomatsu number K such that:
    1 <= K <= N and K = x mod 100"""
    if N < 100:
        return 0
    ret = 0
    q, r = divmod(x, 10)
    for i in range(10):
        if 100 * i + x <= N and is_kadomatsu(i, q, r):
            if i:
                ret += 1
            ret += count_kadomatsu((N - r) // 10, 10 * i + q)
    return ret


def count_kadomatsu_all(N):
    return sum(count_kadomatsu(N, x) for x in range(100))


# %%
def solve(K):
    left = 0
    right = K
    while count_kadomatsu_all(right) < K:
        right *= 10
    while left + 1 < right:
        mid = (left + right) // 2
        if count_kadomatsu_all(mid) >= K:
            right = mid
        else:
            left = mid
    return right


# %%
T, *K = map(int, read().split())
print('\n'.join(map(str, map(solve, K))))
0