結果

問題 No.3 ビットすごろく
ユーザー Koh YamashitaKoh Yamashita
提出日時 2017-06-07 06:27:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 843 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 11,928 KB
実行使用メモリ 10,372 KB
最終ジャッジ日時 2023-10-23 18:26:40
合計ジャッジ時間 3,074 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,160 KB
testcase_01 AC 32 ms
10,160 KB
testcase_02 AC 32 ms
10,160 KB
testcase_03 AC 33 ms
10,196 KB
testcase_04 AC 33 ms
10,160 KB
testcase_05 AC 34 ms
10,208 KB
testcase_06 AC 34 ms
10,196 KB
testcase_07 AC 34 ms
10,188 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 34 ms
10,208 KB
testcase_13 AC 34 ms
10,192 KB
testcase_14 AC 35 ms
10,280 KB
testcase_15 AC 38 ms
10,288 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 33 ms
10,192 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 32 ms
10,160 KB
testcase_22 AC 37 ms
10,280 KB
testcase_23 AC 39 ms
10,288 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 33 ms
10,160 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 33 ms
10,160 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter
import heapq


def bsf(my_number):
    if my_number == 1:
        return 1
    my_heap = list()
    seen = set([1])
    heapq.heappush(my_heap, (1, 1))
    while len(my_heap) > 0:
        count, node_id = heapq.heappop(my_heap)
        seen.add(node_id)
        crt_ones = get_num_ones(node_id)
        if node_id + crt_ones == my_number:
            return count + 1
        elif node_id + crt_ones < my_number and (node_id + crt_ones) not in seen:
            heapq.heappush(my_heap, (count + 1, node_id + crt_ones))
        elif node_id - crt_ones > 0 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