結果

問題 No.3 ビットすごろく
ユーザー nidosino
提出日時 2017-06-02 04:35:07
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 959 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-07-01 08:44:08
合計ジャッジ時間 2,324 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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