結果

問題 No.3 ビットすごろく
ユーザー kaoru muratakaoru murata
提出日時 2021-08-26 11:56:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 31 ms / 5,000 ms
コード長 574 bytes
コンパイル時間 761 ms
コンパイル使用メモリ 10,852 KB
実行使用メモリ 8,872 KB
最終ジャッジ日時 2023-08-11 15:26:04
合計ジャッジ時間 3,525 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,480 KB
testcase_01 AC 18 ms
8,628 KB
testcase_02 AC 18 ms
8,548 KB
testcase_03 AC 21 ms
8,500 KB
testcase_04 AC 20 ms
8,624 KB
testcase_05 AC 25 ms
8,588 KB
testcase_06 AC 22 ms
8,600 KB
testcase_07 AC 20 ms
8,440 KB
testcase_08 AC 24 ms
8,668 KB
testcase_09 AC 27 ms
8,816 KB
testcase_10 AC 29 ms
8,796 KB
testcase_11 AC 26 ms
8,788 KB
testcase_12 AC 24 ms
8,632 KB
testcase_13 AC 21 ms
8,492 KB
testcase_14 AC 29 ms
8,816 KB
testcase_15 AC 31 ms
8,840 KB
testcase_16 AC 30 ms
8,780 KB
testcase_17 AC 30 ms
8,852 KB
testcase_18 AC 20 ms
8,472 KB
testcase_19 AC 31 ms
8,852 KB
testcase_20 AC 19 ms
8,572 KB
testcase_21 AC 19 ms
8,432 KB
testcase_22 AC 28 ms
8,800 KB
testcase_23 AC 31 ms
8,872 KB
testcase_24 AC 31 ms
8,816 KB
testcase_25 AC 30 ms
8,772 KB
testcase_26 AC 19 ms
8,572 KB
testcase_27 AC 21 ms
8,532 KB
testcase_28 AC 31 ms
8,800 KB
testcase_29 AC 27 ms
8,672 KB
testcase_30 AC 18 ms
8,552 KB
testcase_31 AC 19 ms
8,564 KB
testcase_32 AC 26 ms
8,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def read_int(): return int(input())
def read_int_list(): return list(map(int, input().split()))


N = read_int()
costs = [10000000] * (N + 1)
used = [False] * (N + 1)
costs[1] = 1
q = deque()
q.append(1)
while q:
    v = q.popleft()
    if used[v]:
        continue
    used[v] = True
    bits = bin(v).count('1')
    g = [v - bits, v + bits]
    for i in g:
        if i > N:
            continue
        if costs[i] > costs[v] + 1:
            costs[i] = costs[v] + 1
            q.append(i)
print(-1 if costs[N] == 10000000 else costs[N])
0