結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,740 KB
testcase_01 AC 16 ms
7,864 KB
testcase_02 AC 17 ms
7,776 KB
testcase_03 AC 25 ms
7,916 KB
testcase_04 AC 18 ms
7,884 KB
testcase_05 AC 38 ms
8,352 KB
testcase_06 AC 26 ms
7,812 KB
testcase_07 AC 20 ms
7,812 KB
testcase_08 AC 34 ms
8,256 KB
testcase_09 AC 46 ms
8,368 KB
testcase_10 AC 51 ms
8,428 KB
testcase_11 AC 44 ms
8,412 KB
testcase_12 AC 39 ms
8,384 KB
testcase_13 AC 22 ms
7,780 KB
testcase_14 AC 50 ms
8,476 KB
testcase_15 AC 59 ms
8,504 KB
testcase_16 AC 55 ms
8,496 KB
testcase_17 AC 58 ms
8,396 KB
testcase_18 AC 21 ms
7,976 KB
testcase_19 AC 60 ms
8,452 KB
testcase_20 AC 17 ms
7,744 KB
testcase_21 AC 16 ms
7,792 KB
testcase_22 AC 51 ms
8,584 KB
testcase_23 AC 62 ms
8,512 KB
testcase_24 AC 59 ms
8,528 KB
testcase_25 AC 60 ms
8,508 KB
testcase_26 AC 17 ms
7,880 KB
testcase_27 AC 25 ms
7,864 KB
testcase_28 AC 54 ms
8,492 KB
testcase_29 AC 43 ms
8,348 KB
testcase_30 AC 16 ms
7,876 KB
testcase_31 AC 17 ms
7,812 KB
testcase_32 AC 43 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 j >= 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