結果

問題 No.3 ビットすごろく
ユーザー tnodinotnodino
提出日時 2022-01-30 13:48:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 105 ms / 5,000 ms
コード長 559 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 87,244 KB
実行使用メモリ 77,760 KB
最終ジャッジ日時 2023-09-02 01:34:33
合計ジャッジ時間 5,661 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
71,696 KB
testcase_01 AC 86 ms
71,964 KB
testcase_02 AC 86 ms
71,640 KB
testcase_03 AC 101 ms
77,612 KB
testcase_04 AC 88 ms
71,556 KB
testcase_05 AC 101 ms
77,716 KB
testcase_06 AC 97 ms
77,492 KB
testcase_07 AC 97 ms
77,364 KB
testcase_08 AC 105 ms
77,436 KB
testcase_09 AC 105 ms
77,132 KB
testcase_10 AC 104 ms
77,616 KB
testcase_11 AC 99 ms
77,148 KB
testcase_12 AC 99 ms
77,692 KB
testcase_13 AC 93 ms
77,724 KB
testcase_14 AC 100 ms
77,696 KB
testcase_15 AC 103 ms
77,592 KB
testcase_16 AC 103 ms
77,628 KB
testcase_17 AC 103 ms
77,652 KB
testcase_18 AC 94 ms
77,608 KB
testcase_19 AC 103 ms
77,760 KB
testcase_20 AC 87 ms
71,668 KB
testcase_21 AC 86 ms
71,996 KB
testcase_22 AC 101 ms
77,728 KB
testcase_23 AC 104 ms
77,632 KB
testcase_24 AC 104 ms
77,676 KB
testcase_25 AC 103 ms
77,708 KB
testcase_26 AC 86 ms
71,560 KB
testcase_27 AC 95 ms
77,516 KB
testcase_28 AC 103 ms
77,200 KB
testcase_29 AC 100 ms
77,708 KB
testcase_30 AC 84 ms
71,936 KB
testcase_31 AC 85 ms
72,000 KB
testcase_32 AC 103 ms
77,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
def bfs(N, INF):
    Cost = [INF] * (N+1)
    Cost[1] = 1
    Queue = deque()
    Queue.append(1)
    while Queue:
        x = Queue.popleft()
        C = bin(x).count('1')
        if 1 <= x - C and Cost[x] + 1 < Cost[x-C]:
            Cost[x-C] = Cost[x] + 1
            Queue.append(x-C)
        if x + C <= N and Cost[x] + 1 < Cost[x+C]:
            Cost[x+C] = Cost[x] + 1
            Queue.append(x+C)
    return Cost

INF = 1<<32
N = int(input())
Cost = bfs(N, INF)
if Cost[N] == INF:
    print(-1)
else:
    print(Cost[N])
0