結果

問題 No.3 ビットすごろく
コンテスト
ユーザー tnodino
提出日時 2022-05-26 18:08:21
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 94 ms / 5,000 ms
+ 304µs
コード長 556 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 279 ms
コンパイル使用メモリ 21,336 KB
実行使用メモリ 15,664 KB
最終ジャッジ日時 2026-08-31 20:32:47
合計ジャッジ時間 5,182 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
def bfs():
    INF = 1<<32
    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()
if cost[N] == INF:
    cost[N] = -1
print(cost[N])
0