結果

問題 No.3 ビットすごろく
ユーザー kjnhokjnho
提出日時 2017-02-19 13:52:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 35 ms / 5,000 ms
コード長 769 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 10,948 KB
実行使用メモリ 9,124 KB
最終ジャッジ日時 2023-09-14 00:20:32
合計ジャッジ時間 2,180 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,704 KB
testcase_01 AC 21 ms
8,788 KB
testcase_02 AC 20 ms
8,708 KB
testcase_03 AC 24 ms
8,888 KB
testcase_04 AC 23 ms
8,804 KB
testcase_05 AC 28 ms
9,008 KB
testcase_06 AC 24 ms
8,720 KB
testcase_07 AC 22 ms
8,716 KB
testcase_08 AC 26 ms
8,920 KB
testcase_09 AC 29 ms
8,892 KB
testcase_10 AC 31 ms
9,000 KB
testcase_11 AC 30 ms
9,044 KB
testcase_12 AC 28 ms
8,992 KB
testcase_13 AC 24 ms
8,728 KB
testcase_14 AC 31 ms
9,004 KB
testcase_15 AC 35 ms
8,948 KB
testcase_16 AC 33 ms
8,972 KB
testcase_17 AC 33 ms
8,976 KB
testcase_18 AC 22 ms
8,652 KB
testcase_19 AC 33 ms
8,916 KB
testcase_20 AC 21 ms
8,868 KB
testcase_21 AC 19 ms
8,696 KB
testcase_22 AC 30 ms
8,916 KB
testcase_23 AC 33 ms
8,976 KB
testcase_24 AC 32 ms
9,060 KB
testcase_25 AC 33 ms
8,956 KB
testcase_26 AC 20 ms
8,696 KB
testcase_27 AC 22 ms
8,708 KB
testcase_28 AC 30 ms
9,124 KB
testcase_29 AC 26 ms
9,040 KB
testcase_30 AC 20 ms
8,716 KB
testcase_31 AC 20 ms
8,864 KB
testcase_32 AC 29 ms
8,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
from heapq import heappop, heappush, heapify
from copy import deepcopy as dcopy

N = int(input())
# N = 5000

stack = deque()
stack.append((1, 0)) # count, position, arrived, root
D = [1] + [float("inf")]*(N-1) # saitankyorimap

flag = False
while len(stack) != 0:
    count, position = stack.popleft()
    number = position + 1

    if number == N:
        print(count)
        flag = True
        break

    move_size = bin(number).count("1")
    for move in (move_size, - move_size):
        new_position = position + move
        new_count = count + 1
        if (0 <= new_position <= N-1) and D[new_position] > count:
            D[new_position] = count
            stack.append((new_count, new_position))

if not flag:
    print(-1)
0