結果

問題 No.3 ビットすごろく
ユーザー pasoconhoshiipasoconhoshii
提出日時 2019-02-09 13:16:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 659 bytes
コンパイル時間 76 ms
コンパイル使用メモリ 10,956 KB
実行使用メモリ 8,952 KB
最終ジャッジ日時 2023-09-14 11:25:15
合計ジャッジ時間 20,613 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,532 KB
testcase_01 AC 19 ms
8,640 KB
testcase_02 AC 18 ms
8,592 KB
testcase_03 AC 825 ms
8,744 KB
testcase_04 AC 72 ms
8,664 KB
testcase_05 AC 4,101 ms
8,740 KB
testcase_06 AC 977 ms
8,628 KB
testcase_07 AC 273 ms
8,580 KB
testcase_08 AC 1,937 ms
8,872 KB
testcase_09 TLE -
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
stack = deque([1])

dp[1] = 1
# stack = [1]
while stack:
    i = stack.pop()
    # cnt = bin(i).count("1")
    cnt = bcount[i]

    if i == N:
        continue

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

    if 1 <=  i-cnt  <= N:
        if dp[i]+1 < dp[i-cnt] :
            stack.append( i-cnt )
            dp[i-cnt] = dp[i]+1

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

0