結果

問題 No.3 ビットすごろく
ユーザー yomo3yomo3
提出日時 2020-02-28 15:40:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 62 ms / 5,000 ms
コード長 555 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 11,020 KB
実行使用メモリ 8,584 KB
最終ジャッジ日時 2023-09-14 01:38:17
合計ジャッジ時間 2,615 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,808 KB
testcase_01 AC 16 ms
7,840 KB
testcase_02 AC 16 ms
7,752 KB
testcase_03 AC 26 ms
7,828 KB
testcase_04 AC 19 ms
7,836 KB
testcase_05 AC 39 ms
8,308 KB
testcase_06 AC 26 ms
7,844 KB
testcase_07 AC 20 ms
7,828 KB
testcase_08 AC 34 ms
8,296 KB
testcase_09 AC 47 ms
8,496 KB
testcase_10 AC 52 ms
8,496 KB
testcase_11 AC 42 ms
8,412 KB
testcase_12 AC 39 ms
8,436 KB
testcase_13 AC 23 ms
7,840 KB
testcase_14 AC 50 ms
8,380 KB
testcase_15 AC 59 ms
8,460 KB
testcase_16 AC 59 ms
8,508 KB
testcase_17 AC 58 ms
8,532 KB
testcase_18 AC 21 ms
7,836 KB
testcase_19 AC 60 ms
8,464 KB
testcase_20 AC 17 ms
7,804 KB
testcase_21 AC 16 ms
7,840 KB
testcase_22 AC 51 ms
8,440 KB
testcase_23 AC 62 ms
8,468 KB
testcase_24 AC 58 ms
8,496 KB
testcase_25 AC 60 ms
8,584 KB
testcase_26 AC 15 ms
7,752 KB
testcase_27 AC 24 ms
7,836 KB
testcase_28 AC 56 ms
8,488 KB
testcase_29 AC 44 ms
8,432 KB
testcase_30 AC 16 ms
7,832 KB
testcase_31 AC 16 ms
7,748 KB
testcase_32 AC 42 ms
8,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

popcount = lambda n: bin(n).count('1')
INF = 1001001001
N = int(input())

pop = [popcount(x) for x in range(N)]
dp = [INF] * (N+1)
dp[1] = 1

while True:
    changed = False
    for i in range(1, N):
        if dp[i] == INF: continue
        j = i + pop[i]
        if j <= N and dp[i] + 1 < dp[j]:
            dp[j] = dp[i] + 1
            changed = True 
        j = i - pop[i]
        if i >= 0 and i != j and dp[i]+1 < dp[j]:
            dp[j] = dp[i]+1
            changed = True
    if not changed:
        break

print(-1 if dp[N] == INF else dp[N])
0