結果

問題 No.3 ビットすごろく
ユーザー pasoconhoshiipasoconhoshii
提出日時 2019-02-09 13:27:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 713 bytes
コンパイル時間 123 ms
コンパイル使用メモリ 10,804 KB
実行使用メモリ 13,324 KB
最終ジャッジ日時 2023-09-14 11:41:50
合計ジャッジ時間 18,475 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,512 KB
testcase_01 AC 19 ms
8,488 KB
testcase_02 AC 19 ms
8,528 KB
testcase_03 AC 710 ms
8,540 KB
testcase_04 AC 64 ms
8,536 KB
testcase_05 AC 3,393 ms
8,732 KB
testcase_06 AC 899 ms
8,612 KB
testcase_07 AC 235 ms
8,528 KB
testcase_08 AC 1,674 ms
8,788 KB
testcase_09 AC 4,481 ms
8,732 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/mnt/c/Users/moiki/bash/env/bin/python
# N,M = map(int, input().split())
N = int(input())
dp = [10**9 for _ in range(N+1)]
bcount  = [ bin(i).count("1") for i in range(N+1)]

from collections import deque
dp[1] = 1
stack = deque([(1,1)])

while stack:
    i,steps = stack.pop()
    cnt = bcount[i]

    if i == N:
        continue

    steps = steps+1
    back, forward = i-cnt, i+cnt

    if forward  <= N:
        # if dp[i]+1 < dp[i+cnt] :
        if steps < dp[forward]:
            stack.append( (forward, steps))
            dp[forward] = steps

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

print(dp[N] if dp[N] != 10**9 else -1)

0