結果
| 問題 |
No.3 ビットすごろく
|
| ユーザー |
|
| 提出日時 | 2017-06-07 07:36:24 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 61 ms / 5,000 ms |
| コード長 | 909 bytes |
| コンパイル時間 | 110 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 11,776 KB |
| 最終ジャッジ日時 | 2024-07-01 08:44:30 |
| 合計ジャッジ時間 | 2,639 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
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())))