結果

問題 No.3 ビットすごろく
ユーザー Koh YamashitaKoh Yamashita
提出日時 2017-06-07 07:36:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 909 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 10,744 KB
実行使用メモリ 9,464 KB
最終ジャッジ日時 2023-09-14 00:44:50
合計ジャッジ時間 2,418 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,496 KB
testcase_01 AC 20 ms
8,548 KB
testcase_02 AC 19 ms
8,624 KB
testcase_03 AC 24 ms
8,580 KB
testcase_04 AC 20 ms
8,676 KB
testcase_05 AC 30 ms
8,884 KB
testcase_06 AC 25 ms
8,608 KB
testcase_07 AC 21 ms
8,748 KB
testcase_08 AC 28 ms
8,740 KB
testcase_09 AC 35 ms
9,412 KB
testcase_10 AC 38 ms
9,336 KB
testcase_11 AC 35 ms
8,868 KB
testcase_12 AC 31 ms
8,752 KB
testcase_13 AC 25 ms
8,588 KB
testcase_14 AC 41 ms
9,380 KB
testcase_15 AC 45 ms
9,268 KB
testcase_16 AC 43 ms
9,412 KB
testcase_17 AC 44 ms
9,464 KB
testcase_18 AC 24 ms
8,616 KB
testcase_19 AC 45 ms
9,432 KB
testcase_20 AC 19 ms
8,528 KB
testcase_21 AC 18 ms
8,508 KB
testcase_22 AC 36 ms
9,396 KB
testcase_23 AC 41 ms
9,432 KB
testcase_24 AC 42 ms
9,344 KB
testcase_25 AC 41 ms
9,280 KB
testcase_26 AC 18 ms
8,500 KB
testcase_27 AC 22 ms
8,588 KB
testcase_28 AC 39 ms
9,344 KB
testcase_29 AC 32 ms
9,348 KB
testcase_30 AC 17 ms
8,520 KB
testcase_31 AC 18 ms
8,584 KB
testcase_32 AC 32 ms
8,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter
import heapq


def bsf(my_number):
    if my_number == 1:
        return 1
    my_heap = list()
    seen = set()
    heapq.heappush(my_heap, (1, 1))
    while len(my_heap) > 0:
        count, node_id = heapq.heappop(my_heap)
        if node_id in seen:
            continue
        seen.add(node_id)
        crt_ones = get_num_ones(node_id)
        if node_id + crt_ones == my_number:
            return count + 1
        if (0 < node_id + crt_ones < my_number) and ((node_id + crt_ones) not in seen):
            heapq.heappush(my_heap, (count + 1, node_id + crt_ones))
        if (0 < node_id - crt_ones < my_number) and ((node_id - crt_ones) not in seen):
            heapq.heappush(my_heap, (count + 1, node_id - crt_ones))
    return -1


def get_num_ones(my_int):
    return Counter(str(bin(my_int))[2:]).get('1')

if __name__ == '__main__':
    print(bsf(int(input())))
0