結果

問題 No.3 ビットすごろく
ユーザー ckawatakckawatak
提出日時 2017-07-06 23:40:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 52 ms / 5,000 ms
コード長 587 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 10,972 KB
実行使用メモリ 9,380 KB
最終ジャッジ日時 2023-09-14 00:46:45
合計ジャッジ時間 2,807 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
8,948 KB
testcase_01 AC 22 ms
8,964 KB
testcase_02 AC 23 ms
8,968 KB
testcase_03 AC 28 ms
9,108 KB
testcase_04 AC 23 ms
9,012 KB
testcase_05 AC 37 ms
9,056 KB
testcase_06 AC 28 ms
9,124 KB
testcase_07 AC 26 ms
9,024 KB
testcase_08 AC 34 ms
8,992 KB
testcase_09 AC 42 ms
9,264 KB
testcase_10 AC 45 ms
9,112 KB
testcase_11 AC 39 ms
9,104 KB
testcase_12 AC 36 ms
9,088 KB
testcase_13 AC 28 ms
9,112 KB
testcase_14 AC 44 ms
9,212 KB
testcase_15 AC 49 ms
9,240 KB
testcase_16 AC 47 ms
9,340 KB
testcase_17 AC 48 ms
9,356 KB
testcase_18 AC 26 ms
8,940 KB
testcase_19 AC 49 ms
9,364 KB
testcase_20 AC 22 ms
8,928 KB
testcase_21 AC 20 ms
8,872 KB
testcase_22 AC 47 ms
9,196 KB
testcase_23 AC 51 ms
9,368 KB
testcase_24 AC 51 ms
9,380 KB
testcase_25 AC 52 ms
9,372 KB
testcase_26 AC 23 ms
9,108 KB
testcase_27 AC 29 ms
9,128 KB
testcase_28 AC 49 ms
9,328 KB
testcase_29 AC 42 ms
9,108 KB
testcase_30 AC 23 ms
9,072 KB
testcase_31 AC 22 ms
9,076 KB
testcase_32 AC 39 ms
9,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import queue

N = int(input())

def nbit(n):
    b = 0
    while 0 < n:
        b += n & 1
        n >>= 1
    return b

def bfs():
    d = []
    for i in range(N+1):
        d.append(None)

    q = queue.Queue()
    q.put(1)
    d[1] = 1

    while 0 < q.qsize():
        c = q.get()

        if c == N:
            break

        nb = nbit(c)
        bs = [-nb, nb]
        for b in bs:
            n = c + b
            if 1 <= n <= N and d[n] == None:
                q.put(n)
                d[n] = d[c] + 1

    return d[N]

result = bfs()
print(result if result != None else -1)
0