結果

問題 No.3 ビットすごろく
ユーザー htkbhtkb
提出日時 2019-02-09 12:24:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 546 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 10,952 KB
実行使用メモリ 8,916 KB
最終ジャッジ日時 2023-09-14 01:11:49
合計ジャッジ時間 2,123 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,392 KB
testcase_01 AC 18 ms
8,548 KB
testcase_02 AC 18 ms
8,448 KB
testcase_03 AC 21 ms
8,608 KB
testcase_04 AC 19 ms
8,460 KB
testcase_05 AC 26 ms
8,836 KB
testcase_06 AC 21 ms
8,624 KB
testcase_07 AC 20 ms
8,520 KB
testcase_08 AC 24 ms
8,772 KB
testcase_09 AC 26 ms
8,748 KB
testcase_10 AC 28 ms
8,900 KB
testcase_11 AC 27 ms
8,900 KB
testcase_12 AC 25 ms
8,784 KB
testcase_13 AC 21 ms
8,628 KB
testcase_14 AC 28 ms
8,736 KB
testcase_15 AC 32 ms
8,912 KB
testcase_16 AC 30 ms
8,916 KB
testcase_17 AC 31 ms
8,688 KB
testcase_18 AC 21 ms
8,536 KB
testcase_19 AC 31 ms
8,908 KB
testcase_20 AC 18 ms
8,436 KB
testcase_21 AC 18 ms
8,448 KB
testcase_22 AC 28 ms
8,896 KB
testcase_23 AC 30 ms
8,848 KB
testcase_24 AC 30 ms
8,688 KB
testcase_25 AC 31 ms
8,844 KB
testcase_26 AC 19 ms
8,500 KB
testcase_27 AC 21 ms
8,604 KB
testcase_28 AC 29 ms
8,748 KB
testcase_29 AC 27 ms
8,792 KB
testcase_30 AC 18 ms
8,632 KB
testcase_31 AC 19 ms
8,636 KB
testcase_32 AC 25 ms
8,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
N = int(input())
a = list((bin(i).count("1") for i in range(N+1)))
inf = float("inf")
dp = [inf, 1] + [inf]*(N+100)

dq = deque([(1, 1)])
popleft, append = dq.popleft, dq.append

while dq:
    v, steps = popleft()
    if v == N:
        break
    back, forward = v-a[v], v+a[v]
    steps += 1
    if dp[back] > steps:
        dp[back] = steps
        append((back, steps))
    if N >= forward and dp[forward] > steps:
        dp[forward] = steps
        append((forward, steps))


print(dp[N] if dp[N] < inf else -1)
0