結果

問題 No.3 ビットすごろく
ユーザー irumo8202irumo8202
提出日時 2022-01-21 15:22:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 542 bytes
コンパイル時間 528 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 8,956 KB
最終ジャッジ日時 2023-08-16 20:46:36
合計ジャッジ時間 2,251 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,512 KB
testcase_01 AC 16 ms
8,636 KB
testcase_02 AC 17 ms
8,504 KB
testcase_03 AC 19 ms
8,696 KB
testcase_04 AC 16 ms
8,600 KB
testcase_05 AC 21 ms
8,784 KB
testcase_06 AC 19 ms
8,696 KB
testcase_07 AC 18 ms
8,544 KB
testcase_08 AC 22 ms
8,692 KB
testcase_09 AC 24 ms
8,780 KB
testcase_10 AC 25 ms
8,896 KB
testcase_11 AC 22 ms
8,884 KB
testcase_12 AC 20 ms
8,724 KB
testcase_13 AC 17 ms
8,580 KB
testcase_14 AC 24 ms
8,836 KB
testcase_15 AC 25 ms
8,832 KB
testcase_16 AC 26 ms
8,856 KB
testcase_17 AC 26 ms
8,868 KB
testcase_18 AC 16 ms
8,548 KB
testcase_19 AC 25 ms
8,956 KB
testcase_20 AC 16 ms
8,656 KB
testcase_21 AC 15 ms
8,528 KB
testcase_22 AC 23 ms
8,844 KB
testcase_23 AC 25 ms
8,880 KB
testcase_24 AC 27 ms
8,932 KB
testcase_25 AC 25 ms
8,800 KB
testcase_26 AC 15 ms
8,588 KB
testcase_27 AC 17 ms
8,688 KB
testcase_28 AC 25 ms
8,912 KB
testcase_29 AC 21 ms
8,712 KB
testcase_30 AC 15 ms
8,492 KB
testcase_31 AC 17 ms
8,500 KB
testcase_32 AC 23 ms
8,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n = int(input())

visited = [False] * (n + 1)
visited[:1] = [True, True]
cost = [-1] * (n + 1)
cost[1] = 1
que = deque([1])

while que:
    v = que.pop()

    m = bin(v).count("1")

    if v - m >= 1:
        if not visited[v - m]:
            visited[v - m] = True
            cost[v - m] = cost[v] + 1
            que.appendleft(v - m)

    if v + m <= n:
        if not visited[v + m]:
            visited[v + m] = True
            cost[v + m] = cost[v] + 1
            que.appendleft(v + m)

print(cost[n])
0