結果

問題 No.3 ビットすごろく
コンテスト
ユーザー Mario
提出日時 2018-05-19 13:47:32
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 665 ms / 5,000 ms
コード長 749 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 395 ms
コンパイル使用メモリ 20,696 KB
実行使用メモリ 15,484 KB
最終ジャッジ日時 2026-03-22 06:01:34
合計ジャッジ時間 11,428 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque

def count_1(n):
    return list((bin(n)))[2:].count("1")

class BiSugoroku():
    def __init__(self, n, step):
        self.n = n
        st = count_1(n)
        self.plus = self.n + st
        self.minus = self.n - st
        self.step = step


n = int(input())

q = deque([BiSugoroku(1, 1)])
visited = [1]
step = -1

while len(q) > 0:
    b = q.popleft()

    if  b.n == n:
        step = b.step
        break
    else:
        if b.plus <= n and not (b.plus in visited):
            q.append(BiSugoroku(b.plus, b.step + 1))
            visited.append(b.plus)

        if 0 < b.minus and not (b.minus in visited):
            q.append(BiSugoroku(b.minus, b.step + 1))
            visited.append(b.minus)

print(step)
0