結果

問題 No.3 ビットすごろく
ユーザー mottodoramottodora
提出日時 2016-10-21 16:45:12
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 704 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,776 KB
最終ジャッジ日時 2024-07-01 08:07:20
合計ジャッジ時間 2,470 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
memo = {1:1}

def sumbinary(n):
    if n == 0:
        return 0
    elif n % 2 == 0:
        return sumbinary(n//2)
    else:
        return sumbinary(n//2) + 1

target = int(input())

Q = [(1, 1)]

while True:
    if len(Q) == 0:
        result = -1
        break
    else:
        now, walk = Q.pop(0)
        if now == target:
            result = walk
            break
        d = sumbinary(now)
        if 0 < now-d <= target and not(now-d in memo):
            memo[now-d] = walk+1
            Q.append((now-d, walk+1))

        if 0 < now+d <= target and not(now+d in memo):
            memo[now+d] = walk+1
            Q.append((now+d, walk+1))

print(result)
0