結果

問題 No.3 ビットすごろく
ユーザー pasoconhoshiipasoconhoshii
提出日時 2019-02-09 11:22:19
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 91 ms / 5,000 ms
コード長 755 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 10,944 KB
実行使用メモリ 8,512 KB
最終ジャッジ日時 2023-09-14 01:11:40
合計ジャッジ時間 3,169 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,976 KB
testcase_01 AC 18 ms
7,820 KB
testcase_02 AC 17 ms
7,820 KB
testcase_03 AC 31 ms
7,776 KB
testcase_04 AC 20 ms
7,816 KB
testcase_05 AC 56 ms
8,228 KB
testcase_06 AC 32 ms
7,748 KB
testcase_07 AC 24 ms
7,836 KB
testcase_08 AC 48 ms
7,792 KB
testcase_09 AC 69 ms
8,312 KB
testcase_10 AC 78 ms
8,376 KB
testcase_11 AC 62 ms
8,308 KB
testcase_12 AC 56 ms
8,268 KB
testcase_13 AC 27 ms
7,784 KB
testcase_14 AC 74 ms
8,280 KB
testcase_15 AC 89 ms
8,512 KB
testcase_16 AC 84 ms
8,360 KB
testcase_17 AC 87 ms
8,396 KB
testcase_18 AC 25 ms
7,824 KB
testcase_19 AC 91 ms
8,396 KB
testcase_20 AC 19 ms
7,848 KB
testcase_21 AC 18 ms
7,832 KB
testcase_22 AC 76 ms
8,236 KB
testcase_23 AC 90 ms
8,500 KB
testcase_24 AC 90 ms
8,484 KB
testcase_25 AC 90 ms
8,456 KB
testcase_26 AC 16 ms
7,844 KB
testcase_27 AC 31 ms
7,832 KB
testcase_28 AC 83 ms
8,492 KB
testcase_29 AC 63 ms
8,328 KB
testcase_30 AC 17 ms
7,944 KB
testcase_31 AC 17 ms
7,824 KB
testcase_32 AC 59 ms
8,348 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]
change = True
while change:
    change = False
    # i = stack.pop()

    for i in range(1, N+1):
        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
                change=True

        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
                change=True
print(dp[N] if dp[N] != 10**9 else -1)
0