結果

問題 No.3 ビットすごろく
ユーザー ryo_nryo_n
提出日時 2020-06-22 16:16:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 945 ms / 5,000 ms
コード長 576 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 8,420 KB
最終ジャッジ日時 2023-09-14 01:47:35
合計ジャッジ時間 16,271 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
7,776 KB
testcase_01 AC 18 ms
7,788 KB
testcase_02 AC 17 ms
7,836 KB
testcase_03 AC 225 ms
7,776 KB
testcase_04 AC 73 ms
7,836 KB
testcase_05 AC 514 ms
8,248 KB
testcase_06 AC 255 ms
7,796 KB
testcase_07 AC 147 ms
7,844 KB
testcase_08 AC 394 ms
7,944 KB
testcase_09 AC 662 ms
8,300 KB
testcase_10 AC 766 ms
8,416 KB
testcase_11 AC 584 ms
8,380 KB
testcase_12 AC 483 ms
8,220 KB
testcase_13 AC 193 ms
7,840 KB
testcase_14 AC 762 ms
8,328 KB
testcase_15 AC 945 ms
8,396 KB
testcase_16 AC 879 ms
8,384 KB
testcase_17 AC 901 ms
8,388 KB
testcase_18 AC 164 ms
7,832 KB
testcase_19 AC 929 ms
8,412 KB
testcase_20 AC 56 ms
7,840 KB
testcase_21 AC 16 ms
7,768 KB
testcase_22 AC 749 ms
8,280 KB
testcase_23 AC 932 ms
8,420 KB
testcase_24 AC 928 ms
8,388 KB
testcase_25 AC 942 ms
8,336 KB
testcase_26 AC 17 ms
7,844 KB
testcase_27 AC 210 ms
7,792 KB
testcase_28 AC 837 ms
8,380 KB
testcase_29 AC 598 ms
8,220 KB
testcase_30 AC 25 ms
7,844 KB
testcase_31 AC 28 ms
7,892 KB
testcase_32 AC 570 ms
8,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    dp = [float("inf")] * (N + 1)

    dp[1] = 1

    for _ in range(100):
        for i in range(1, N + 1):
            bcount = bin(i).count("1")
            if i + bcount <= N:
                dp[i + bcount] = min(dp[i + bcount], dp[i] + 1)

            if i - bcount >= 0:
                dp[i - bcount] = min(dp[i - bcount], dp[i] + 1)

    if dp[-1] == float("inf"):
        print(-1)
    else:
        print(dp[-1])


    

if __name__ == '__main__':
    main()

0