結果

問題 No.3 ビットすごろく
ユーザー MarioMario
提出日時 2018-05-19 13:47:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 833 ms / 5,000 ms
コード長 749 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 10,928 KB
実行使用メモリ 8,936 KB
最終ジャッジ日時 2023-09-14 01:01:20
合計ジャッジ時間 12,410 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,624 KB
testcase_01 AC 18 ms
8,580 KB
testcase_02 AC 18 ms
8,576 KB
testcase_03 AC 69 ms
8,620 KB
testcase_04 AC 23 ms
8,580 KB
testcase_05 AC 251 ms
8,612 KB
testcase_06 AC 76 ms
8,512 KB
testcase_07 AC 39 ms
8,552 KB
testcase_08 AC 165 ms
8,728 KB
testcase_09 AC 399 ms
8,652 KB
testcase_10 AC 575 ms
8,884 KB
testcase_11 AC 342 ms
8,628 KB
testcase_12 AC 239 ms
8,580 KB
testcase_13 AC 55 ms
8,472 KB
testcase_14 AC 532 ms
8,836 KB
testcase_15 AC 800 ms
8,896 KB
testcase_16 AC 700 ms
8,792 KB
testcase_17 AC 789 ms
8,740 KB
testcase_18 AC 44 ms
8,548 KB
testcase_19 AC 833 ms
8,936 KB
testcase_20 AC 21 ms
8,568 KB
testcase_21 AC 18 ms
8,632 KB
testcase_22 AC 536 ms
8,876 KB
testcase_23 AC 822 ms
8,852 KB
testcase_24 AC 814 ms
8,800 KB
testcase_25 AC 810 ms
8,732 KB
testcase_26 AC 18 ms
8,504 KB
testcase_27 AC 60 ms
8,428 KB
testcase_28 AC 677 ms
8,864 KB
testcase_29 AC 348 ms
8,620 KB
testcase_30 AC 18 ms
8,508 KB
testcase_31 AC 19 ms
8,596 KB
testcase_32 AC 300 ms
8,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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