結果

問題 No.3 ビットすごろく
コンテスト
ユーザー tnodino
提出日時 2022-01-30 13:48:39
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 43 ms / 5,000 ms
コード長 559 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 123 ms
コンパイル使用メモリ 85,740 KB
実行使用メモリ 71,464 KB
最終ジャッジ日時 2026-03-04 10:20:57
合計ジャッジ時間 2,357 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
def bfs(N, INF):
    Cost = [INF] * (N+1)
    Cost[1] = 1
    Queue = deque()
    Queue.append(1)
    while Queue:
        x = Queue.popleft()
        C = bin(x).count('1')
        if 1 <= x - C and Cost[x] + 1 < Cost[x-C]:
            Cost[x-C] = Cost[x] + 1
            Queue.append(x-C)
        if x + C <= N and Cost[x] + 1 < Cost[x+C]:
            Cost[x+C] = Cost[x] + 1
            Queue.append(x+C)
    return Cost

INF = 1<<32
N = int(input())
Cost = bfs(N, INF)
if Cost[N] == INF:
    print(-1)
else:
    print(Cost[N])
0