結果

問題 No.3 ビットすごろく
ユーザー aqua_tenhouaqua_tenhou
提出日時 2020-09-15 22:42:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 120 ms / 5,000 ms
コード長 442 bytes
コンパイル時間 1,122 ms
コンパイル使用メモリ 86,984 KB
実行使用メモリ 77,728 KB
最終ジャッジ日時 2023-09-14 01:51:21
合計ジャッジ時間 6,129 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,768 KB
testcase_01 AC 90 ms
71,580 KB
testcase_02 AC 94 ms
71,764 KB
testcase_03 AC 109 ms
77,468 KB
testcase_04 AC 95 ms
71,680 KB
testcase_05 AC 112 ms
77,656 KB
testcase_06 AC 106 ms
77,436 KB
testcase_07 AC 106 ms
77,020 KB
testcase_08 AC 112 ms
77,240 KB
testcase_09 AC 111 ms
77,476 KB
testcase_10 AC 109 ms
77,568 KB
testcase_11 AC 109 ms
77,516 KB
testcase_12 AC 108 ms
77,652 KB
testcase_13 AC 101 ms
77,444 KB
testcase_14 AC 119 ms
77,432 KB
testcase_15 AC 116 ms
77,180 KB
testcase_16 AC 113 ms
77,100 KB
testcase_17 AC 113 ms
77,552 KB
testcase_18 AC 101 ms
77,492 KB
testcase_19 AC 120 ms
77,728 KB
testcase_20 AC 99 ms
71,780 KB
testcase_21 AC 95 ms
71,624 KB
testcase_22 AC 109 ms
77,436 KB
testcase_23 AC 108 ms
77,580 KB
testcase_24 AC 112 ms
77,316 KB
testcase_25 AC 110 ms
77,576 KB
testcase_26 AC 94 ms
71,884 KB
testcase_27 AC 109 ms
77,548 KB
testcase_28 AC 117 ms
77,308 KB
testcase_29 AC 109 ms
77,064 KB
testcase_30 AC 93 ms
71,336 KB
testcase_31 AC 86 ms
71,552 KB
testcase_32 AC 106 ms
77,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n = int(input())
reach = [float("Inf")]*(n+1)
reach[1] = 1
q = deque([])
q.append(1)

while q:
    x = q.popleft()
    c = bin(x).count("1")
    if 1 <= x-c and reach[x-c] > reach[x] + 1:
        q.append(x-c)
        reach[x-c] = reach[x] + 1
    if x+c <= n and reach[x+c] > reach[x] + 1:
        q.append(x+c)
        reach[x+c] = reach[x] + 1
if reach[-1] >= 10**18:
    print(-1)
else:
    print(reach[-1])
0