結果

問題 No.639 An Ordinary Sequence
ユーザー lam6er
提出日時 2025-03-20 21:09:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 40 ms / 1,000 ms
コード長 680 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 82,308 KB
実行使用メモリ 55,440 KB
最終ジャッジ日時 2025-03-20 21:10:14
合計ジャッジ時間 1,534 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

def get_required_nodes(n):
    required = set()
    if n == 0:
        return [0]
    required.add(n)
    from collections import deque
    queue = deque([n])
    while queue:
        current = queue.popleft()
        for child in [current // 3, current // 5]:
            if child not in required and child > 0:
                required.add(child)
                queue.append(child)
    required.add(0)
    return sorted(required)

n = int(input())
if n == 0:
    print(1)
else:
    required_nodes = get_required_nodes(n)
    memo = {0: 1}
    for i in required_nodes[1:]:  # Skip 0 since it's already initialized
        memo[i] = memo[i // 3] + memo[i // 5]
    print(memo[n])
0