結果

問題 No.3 ビットすごろく
コンテスト
ユーザー nanae
提出日時 2017-02-22 19:25:35
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 102 ms / 5,000 ms
コード長 651 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 473 ms
コンパイル使用メモリ 20,828 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2026-03-22 04:56:16
合計ジャッジ時間 4,518 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque

N = int(input())
d = [-1] * N
checked = [False] * N
p = [None] * N
nxts = deque([0])
checked[0] = True

while nxts:
    pos = nxts.popleft()

    if pos == 0:
        d[pos] = 1
    else:
        d[pos] = d[p[pos]] + 1

    if pos == N - 1:
        print(d[N - 1])
        quit()

    step = bin(pos + 1).count('1')

    if pos - step >= 0 and not checked[pos - step]:
        nxts.append(pos - step)
        checked[pos - step] = True
        p[pos - step] = pos
    if pos + step < N and not checked[pos + step]:
        nxts.append(pos + step)
        checked[pos + step] = True
        p[pos + step] = pos

print(-1)
0