結果

問題 No.3 ビットすごろく
ユーザー pasoconhoshiipasoconhoshii
提出日時 2019-02-09 15:24:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 360 ms / 5,000 ms
コード長 593 bytes
コンパイル時間 155 ms
コンパイル使用メモリ 10,792 KB
実行使用メモリ 8,496 KB
最終ジャッジ日時 2023-09-14 01:12:31
合計ジャッジ時間 6,078 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,796 KB
testcase_01 AC 15 ms
7,852 KB
testcase_02 AC 15 ms
7,792 KB
testcase_03 AC 34 ms
7,848 KB
testcase_04 AC 18 ms
7,768 KB
testcase_05 AC 113 ms
8,200 KB
testcase_06 AC 38 ms
7,840 KB
testcase_07 AC 23 ms
7,852 KB
testcase_08 AC 84 ms
7,812 KB
testcase_09 AC 181 ms
8,124 KB
testcase_10 AC 276 ms
8,248 KB
testcase_11 AC 152 ms
8,188 KB
testcase_12 AC 105 ms
8,244 KB
testcase_13 AC 28 ms
7,872 KB
testcase_14 AC 254 ms
8,140 KB
testcase_15 AC 344 ms
8,448 KB
testcase_16 AC 308 ms
8,124 KB
testcase_17 AC 341 ms
8,488 KB
testcase_18 AC 24 ms
7,912 KB
testcase_19 AC 358 ms
8,496 KB
testcase_20 AC 18 ms
7,796 KB
testcase_21 AC 16 ms
7,712 KB
testcase_22 AC 257 ms
8,124 KB
testcase_23 AC 349 ms
8,400 KB
testcase_24 AC 360 ms
8,492 KB
testcase_25 AC 350 ms
8,412 KB
testcase_26 AC 15 ms
7,860 KB
testcase_27 AC 32 ms
7,804 KB
testcase_28 AC 297 ms
8,132 KB
testcase_29 AC 159 ms
8,200 KB
testcase_30 AC 16 ms
7,768 KB
testcase_31 AC 15 ms
7,792 KB
testcase_32 AC 136 ms
8,288 KB
権限があれば一括ダウンロードができます

ソースコード

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)]

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