結果

問題 No.3 ビットすごろく
ユーザー halship
提出日時 2021-06-02 20:47:38
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 566 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-11-15 16:16:26
合計ジャッジ時間 2,527 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def bitcount(x):
    return bin(x).count('1')


n = int(input())
states = deque([(1, 1)])
searched = {1}

while len(states) > 0:
    pos, turn = states.popleft()

    left = pos - bitcount(pos)
    right = pos + bitcount(pos)
    if left == n or right == n:
        print(turn + 1)
        exit(0)

    if left not in searched and left > 0:
        searched.add(left)
        states.append((left, turn + 1))
    if right not in searched and right < n:
        searched.add(right)
        states.append((right, turn + 1))

print('-1')
0