結果

問題 No.3 ビットすごろく
コンテスト
ユーザー にゃん
提出日時 2022-02-13 03:07:30
言語 Python3
(3.14.3 + numpy 2.4.2 + scipy 1.17.0)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 101 ms / 5,000 ms
コード長 506 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 527 ms
コンパイル使用メモリ 20,828 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2026-03-18 14:59:15
合計ジャッジ時間 4,681 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
def bit(x):
    x = bin(x)
    x = x.count('1')
    return x

def bit_fb(x):
    a = x - bit(x)
    b = x + bit(x)
    return a,b

N = int(input())
A = [0] * (N+1)
A[1] = 1
B = deque()
B.append(1)
while B:
    C = B.popleft()
    if C == N:
        print(A[C])
        break
    D,E = bit_fb(C)
    if 0 < D <= N and A[D] == 0:
        B.append(D)
        A[D] = A[C] + 1
    if 0 < E <= N and A[E] == 0:
        B.append(E)
        A[E] = A[C] + 1
if A[N] == 0:
    print(-1)
0