結果

問題 No.3 ビットすごろく
ユーザー warashi
提出日時 2015-02-09 14:39:11
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 57 ms / 5,000 ms
コード長 641 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 13,312 KB
最終ジャッジ日時 2024-07-01 07:12:49
合計ジャッジ時間 2,619 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
from collections import deque


def memoize(f):
    table = {}

    def func(*args):
        if not args in table:
            table[args] = f(*args)
        return table[args]
    return func


@memoize
def nexts(node):
    r = format(node, "b").count('1')
    return [i + node for i in (-r, r) if 0 < i + node <= N]


N = int(input())
node = 1
pend = deque()
hop = {1: 1}
while node < N:
    nxts = [n for n in nexts(node) if n not in hop.keys()]
    pend.extend(nxts)
    for n in nxts:
        hop[n] = hop[node] + 1
    if len(pend) == 0:
        print(-1)
        exit()
    node = pend.popleft()

print(hop[N])
0