結果

問題 No.3 ビットすごろく
ユーザー pasoconhoshiipasoconhoshii
提出日時 2019-02-09 13:40:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 759 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 10,808 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2023-09-14 01:12:17
合計ジャッジ時間 2,533 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,460 KB
testcase_01 AC 18 ms
8,544 KB
testcase_02 AC 18 ms
8,560 KB
testcase_03 AC 24 ms
8,608 KB
testcase_04 AC 19 ms
8,508 KB
testcase_05 AC 36 ms
8,836 KB
testcase_06 AC 25 ms
8,592 KB
testcase_07 AC 20 ms
8,460 KB
testcase_08 AC 32 ms
8,748 KB
testcase_09 AC 44 ms
8,748 KB
testcase_10 AC 51 ms
8,784 KB
testcase_11 AC 41 ms
8,736 KB
testcase_12 AC 34 ms
8,696 KB
testcase_13 AC 22 ms
8,632 KB
testcase_14 AC 49 ms
8,804 KB
testcase_15 AC 56 ms
8,828 KB
testcase_16 AC 55 ms
8,856 KB
testcase_17 AC 57 ms
8,820 KB
testcase_18 AC 21 ms
8,524 KB
testcase_19 AC 58 ms
8,824 KB
testcase_20 AC 18 ms
8,504 KB
testcase_21 AC 18 ms
8,456 KB
testcase_22 AC 49 ms
8,904 KB
testcase_23 AC 56 ms
8,816 KB
testcase_24 AC 59 ms
8,960 KB
testcase_25 AC 59 ms
8,924 KB
testcase_26 AC 19 ms
8,504 KB
testcase_27 AC 24 ms
8,492 KB
testcase_28 AC 55 ms
8,892 KB
testcase_29 AC 40 ms
8,804 KB
testcase_30 AC 18 ms
8,580 KB
testcase_31 AC 19 ms
8,588 KB
testcase_32 AC 39 ms
8,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/mnt/c/Users/moiki/bash/env/bin/python
# N,M = map(int, input().split())
from collections import deque
N = int(input())
inf = float("inf")
# dp = [inf for _ in range(N+1)]
dp = [inf, 1] + [inf]*(N+100)
# bcount  = [ bin(i).count("1") for i in range(N+1)]
bcount = list((bin(i).count("1") for i in range(N+1)))

# dp[1] = 1
stack = deque([(1,1)])
pop, append = stack.pop, stack.append

while stack:

    i,steps = pop()
    if i == N:
        continue

    steps = steps+1
    back, forward = i-bcount[i], i+bcount[i]


    if steps < dp[back] :
            append((back, steps))
            dp[back] = steps

    if forward  <= N and steps < dp[forward]:
        append( (forward, steps))
        dp[forward] = steps


print(dp[N] if dp[N] != inf else -1)

0