結果

問題 No.3 ビットすごろく
ユーザー matsu7874matsu7874
提出日時 2015-08-14 16:47:47
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 508 bytes
コンパイル時間 111 ms
コンパイル使用メモリ 10,908 KB
実行使用メモリ 9,184 KB
最終ジャッジ日時 2023-09-13 23:18:20
合計ジャッジ時間 2,892 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
8,924 KB
testcase_01 AC 22 ms
8,960 KB
testcase_02 AC 22 ms
9,056 KB
testcase_03 AC 30 ms
9,040 KB
testcase_04 AC 25 ms
9,048 KB
testcase_05 AC 40 ms
8,972 KB
testcase_06 AC 31 ms
8,980 KB
testcase_07 AC 27 ms
8,972 KB
testcase_08 AC 36 ms
9,000 KB
testcase_09 AC 45 ms
8,948 KB
testcase_10 AC 50 ms
8,992 KB
testcase_11 AC 43 ms
9,040 KB
testcase_12 AC 39 ms
8,928 KB
testcase_13 AC 29 ms
8,984 KB
testcase_14 AC 49 ms
9,028 KB
testcase_15 AC 55 ms
9,048 KB
testcase_16 AC 51 ms
9,064 KB
testcase_17 AC 53 ms
9,184 KB
testcase_18 AC 27 ms
9,068 KB
testcase_19 AC 54 ms
9,128 KB
testcase_20 AC 23 ms
8,968 KB
testcase_21 AC 22 ms
8,888 KB
testcase_22 AC 50 ms
9,116 KB
testcase_23 AC 55 ms
9,128 KB
testcase_24 AC 56 ms
9,068 KB
testcase_25 AC 55 ms
9,012 KB
testcase_26 AC 23 ms
8,956 KB
testcase_27 AC 29 ms
9,100 KB
testcase_28 AC 52 ms
9,036 KB
testcase_29 AC 44 ms
8,992 KB
testcase_30 AC 23 ms
9,016 KB
testcase_31 AC 23 ms
9,048 KB
testcase_32 AC 41 ms
9,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import queue

def bit_count(n):
    return bin(n).count('1')

N = int(input())
visited = [False for i in range(N+1)]
q = queue.Queue()
q.put((1,1))
while not q.empty():
    t = q.get()
    if visited[t[0]]:
        continue
    # print(t)
    visited[t[0]] = True
    if t[0] == N:
        print(t[1])
        exit()
    bc = bit_count(t[0])
    if t[0]-bc > 0 and not visited[t[0]-bc]:
        q.put((t[0]-bc,t[1]+1))
    if t[0]+bc <= N and not visited[t[0]+bc]:
        q.put((t[0]+bc, t[1]+1))
print(-1)
0