結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
7,788 KB
testcase_01 AC 17 ms
7,796 KB
testcase_02 AC 16 ms
7,776 KB
testcase_03 AC 36 ms
7,788 KB
testcase_04 AC 18 ms
7,764 KB
testcase_05 AC 114 ms
8,216 KB
testcase_06 AC 39 ms
7,852 KB
testcase_07 AC 24 ms
7,868 KB
testcase_08 AC 84 ms
8,232 KB
testcase_09 AC 181 ms
8,408 KB
testcase_10 AC 276 ms
8,124 KB
testcase_11 AC 157 ms
8,312 KB
testcase_12 AC 110 ms
8,176 KB
testcase_13 AC 29 ms
7,908 KB
testcase_14 AC 261 ms
8,180 KB
testcase_15 AC 351 ms
8,444 KB
testcase_16 AC 313 ms
8,252 KB
testcase_17 AC 347 ms
8,436 KB
testcase_18 AC 26 ms
7,856 KB
testcase_19 AC 367 ms
8,436 KB
testcase_20 AC 18 ms
7,776 KB
testcase_21 AC 16 ms
7,808 KB
testcase_22 AC 266 ms
8,268 KB
testcase_23 AC 361 ms
8,388 KB
testcase_24 AC 363 ms
8,524 KB
testcase_25 AC 354 ms
8,492 KB
testcase_26 AC 16 ms
7,852 KB
testcase_27 AC 33 ms
7,852 KB
testcase_28 AC 302 ms
8,132 KB
testcase_29 AC 160 ms
8,292 KB
testcase_30 AC 16 ms
7,780 KB
testcase_31 AC 17 ms
7,828 KB
testcase_32 AC 137 ms
8,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

dp[1] = 1
stack = [1]
while stack:
    i = stack.pop()
    cnt = bin(i).count("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
    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