結果

問題 No.3 ビットすごろく
ユーザー nidosinonidosino
提出日時 2017-06-02 04:35:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 959 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 10,760 KB
実行使用メモリ 8,596 KB
最終ジャッジ日時 2023-09-14 00:44:23
合計ジャッジ時間 2,212 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,840 KB
testcase_01 AC 16 ms
7,816 KB
testcase_02 AC 16 ms
7,924 KB
testcase_03 AC 19 ms
7,784 KB
testcase_04 AC 17 ms
7,808 KB
testcase_05 AC 23 ms
8,392 KB
testcase_06 AC 20 ms
7,888 KB
testcase_07 AC 18 ms
7,888 KB
testcase_08 AC 22 ms
8,296 KB
testcase_09 AC 27 ms
8,376 KB
testcase_10 AC 28 ms
8,424 KB
testcase_11 AC 26 ms
8,520 KB
testcase_12 AC 24 ms
8,236 KB
testcase_13 AC 19 ms
7,864 KB
testcase_14 AC 28 ms
8,396 KB
testcase_15 AC 32 ms
8,440 KB
testcase_16 AC 29 ms
8,512 KB
testcase_17 AC 30 ms
8,536 KB
testcase_18 AC 18 ms
7,976 KB
testcase_19 AC 31 ms
8,464 KB
testcase_20 AC 17 ms
7,848 KB
testcase_21 AC 16 ms
7,888 KB
testcase_22 AC 27 ms
8,476 KB
testcase_23 AC 31 ms
8,596 KB
testcase_24 AC 32 ms
8,520 KB
testcase_25 AC 31 ms
8,580 KB
testcase_26 AC 17 ms
7,784 KB
testcase_27 AC 19 ms
7,860 KB
testcase_28 AC 30 ms
8,472 KB
testcase_29 AC 27 ms
8,444 KB
testcase_30 AC 17 ms
7,804 KB
testcase_31 AC 16 ms
7,892 KB
testcase_32 AC 25 ms
8,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MAX_STEP = 100000


def my_bin(x):
    return bin(x).count("1")

if __name__ == '__main__':
    A = int(input())
    bit_one_list = list(map(my_bin, range(1, A + 1)))
    din_list = [MAX_STEP] * A
    din_list[0] = 0
    now_step = 0
    hohaba = 0
    step_count = 0
    step_queue = [now_step]
    while len(step_queue) > 0:
        now_step = step_queue.pop(0)
        hohaba = bit_one_list[now_step]
        step_count = din_list[now_step]
        if now_step + hohaba + 1 <= A and step_count + 1 < din_list[now_step + hohaba]:
            step_queue.append(now_step + hohaba)
            din_list[now_step + hohaba] = step_count + 1
        if now_step - hohaba + 1 > 0 and step_count + 1 < din_list[now_step - hohaba]:
            step_queue.append(now_step - hohaba)
            din_list[now_step - hohaba] = step_count + 1
    if din_list[A - 1] != MAX_STEP:
        print(din_list[A - 1] + 1)
    else:
        print(-1)
0