結果

問題 No.3 ビットすごろく
ユーザー eiji1000eiji1000
提出日時 2019-03-14 19:39:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 362 ms / 5,000 ms
コード長 611 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 8,636 KB
最終ジャッジ日時 2023-09-14 01:13:21
合計ジャッジ時間 6,299 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
7,852 KB
testcase_01 AC 16 ms
7,852 KB
testcase_02 AC 17 ms
7,908 KB
testcase_03 AC 35 ms
7,848 KB
testcase_04 AC 18 ms
7,776 KB
testcase_05 AC 115 ms
8,332 KB
testcase_06 AC 39 ms
7,972 KB
testcase_07 AC 24 ms
7,856 KB
testcase_08 AC 84 ms
8,376 KB
testcase_09 AC 181 ms
8,448 KB
testcase_10 AC 280 ms
8,396 KB
testcase_11 AC 157 ms
8,392 KB
testcase_12 AC 111 ms
8,264 KB
testcase_13 AC 29 ms
7,836 KB
testcase_14 AC 262 ms
8,376 KB
testcase_15 AC 355 ms
8,604 KB
testcase_16 AC 315 ms
8,260 KB
testcase_17 AC 346 ms
8,508 KB
testcase_18 AC 26 ms
7,872 KB
testcase_19 AC 362 ms
8,636 KB
testcase_20 AC 18 ms
7,784 KB
testcase_21 AC 16 ms
7,956 KB
testcase_22 AC 268 ms
8,512 KB
testcase_23 AC 362 ms
8,448 KB
testcase_24 AC 362 ms
8,460 KB
testcase_25 AC 356 ms
8,596 KB
testcase_26 AC 16 ms
7,856 KB
testcase_27 AC 33 ms
7,780 KB
testcase_28 AC 308 ms
8,288 KB
testcase_29 AC 162 ms
8,316 KB
testcase_30 AC 17 ms
7,816 KB
testcase_31 AC 16 ms
7,892 KB
testcase_32 AC 138 ms
8,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
dp = [10**9 for _ in range(N+1)]

dp[1] = 1 
stack = [1]
while stack:
    i = stack.pop() # current location
    cnt = bin(i).count("1") # distance for current location
    if 1 <=  i-cnt  <= N: # 現在地から後方に動いた場合
        if dp[i]+1 < dp[i-cnt] :
            if not (i-cnt) in stack:
                stack.append( i-cnt )
            dp[i-cnt] = dp[i]+1
            
    if 1 <= i+cnt  <= N:
        if dp[i]+1 < dp[i+cnt] :
            if not (i+cnt) in stack:
                stack.append( i+cnt )
            dp[i+cnt] = dp[i]+1

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