結果

問題 No.3 ビットすごろく
ユーザー 👑 rin204rin204
提出日時 2022-01-20 21:28:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 119 ms / 5,000 ms
コード長 448 bytes
コンパイル時間 700 ms
コンパイル使用メモリ 87,284 KB
実行使用メモリ 77,632 KB
最終ジャッジ日時 2023-08-15 21:31:19
合計ジャッジ時間 5,557 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,476 KB
testcase_01 AC 97 ms
71,668 KB
testcase_02 AC 93 ms
71,768 KB
testcase_03 AC 109 ms
76,948 KB
testcase_04 AC 98 ms
71,684 KB
testcase_05 AC 113 ms
77,208 KB
testcase_06 AC 109 ms
76,996 KB
testcase_07 AC 106 ms
77,132 KB
testcase_08 AC 112 ms
77,464 KB
testcase_09 AC 116 ms
77,392 KB
testcase_10 AC 115 ms
77,508 KB
testcase_11 AC 113 ms
77,364 KB
testcase_12 AC 114 ms
77,468 KB
testcase_13 AC 106 ms
77,148 KB
testcase_14 AC 116 ms
77,264 KB
testcase_15 AC 117 ms
77,316 KB
testcase_16 AC 115 ms
77,632 KB
testcase_17 AC 117 ms
77,396 KB
testcase_18 AC 106 ms
77,476 KB
testcase_19 AC 117 ms
77,288 KB
testcase_20 AC 99 ms
71,768 KB
testcase_21 AC 100 ms
71,680 KB
testcase_22 AC 119 ms
77,456 KB
testcase_23 AC 118 ms
77,136 KB
testcase_24 AC 117 ms
77,148 KB
testcase_25 AC 116 ms
77,516 KB
testcase_26 AC 98 ms
71,712 KB
testcase_27 AC 110 ms
77,140 KB
testcase_28 AC 117 ms
77,348 KB
testcase_29 AC 114 ms
77,140 KB
testcase_30 AC 97 ms
71,688 KB
testcase_31 AC 99 ms
71,684 KB
testcase_32 AC 116 ms
76,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n = int(input())

dist = [-1] * (n + 1)
queue = deque()
queue.append(1)
dist[1] = 1

while queue:
    pos = queue.popleft()
    d = bin(pos).count("1")
    
    npos = pos + d
    if npos <= n and dist[npos] == -1:
        dist[npos] = dist[pos] + 1
        queue.append(npos)
    
    npos = pos - d
    if npos > 0 and dist[npos] == -1:
        dist[npos] = dist[pos] + 1
        queue.append(npos)

print(dist[n])
0