結果

問題 No.362 門松ナンバー
ユーザー maspymaspy
提出日時 2020-03-12 23:10:47
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,964 ms / 3,000 ms
コード長 1,151 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 82,148 KB
最終ジャッジ日時 2024-11-28 08:20:54
合計ジャッジ時間 20,323 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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