結果

問題 No.3 ビットすごろく
ユーザー htkbhtkb
提出日時 2019-02-09 12:40:34
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 534 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 11,012 KB
実行使用メモリ 9,084 KB
最終ジャッジ日時 2023-09-14 11:08:20
合計ジャッジ時間 2,271 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,652 KB
testcase_01 AC 19 ms
8,524 KB
testcase_02 AC 18 ms
8,576 KB
testcase_03 AC 25 ms
8,600 KB
testcase_04 AC 19 ms
8,580 KB
testcase_05 AC 37 ms
8,844 KB
testcase_06 AC 25 ms
8,776 KB
testcase_07 AC 19 ms
8,536 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 36 ms
8,736 KB
testcase_13 AC 23 ms
8,716 KB
testcase_14 AC 50 ms
8,864 KB
testcase_15 AC 59 ms
8,944 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 19 ms
8,564 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 19 ms
8,460 KB
testcase_22 AC 50 ms
8,928 KB
testcase_23 AC 60 ms
9,084 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 18 ms
8,460 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 18 ms
8,640 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

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)])
pop, append = dq.pop, dq.append

while dq:
    v, steps = pop()
    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