結果

問題 No.3 ビットすごろく
ユーザー FromBooskaFromBooska
提出日時 2023-02-14 22:13:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 121 ms / 5,000 ms
コード長 718 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 87,148 KB
実行使用メモリ 79,332 KB
最終ジャッジ日時 2023-09-24 09:20:44
合計ジャッジ時間 5,411 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,680 KB
testcase_01 AC 88 ms
71,648 KB
testcase_02 AC 87 ms
71,828 KB
testcase_03 AC 121 ms
77,920 KB
testcase_04 AC 95 ms
76,740 KB
testcase_05 AC 105 ms
78,436 KB
testcase_06 AC 106 ms
77,652 KB
testcase_07 AC 103 ms
77,880 KB
testcase_08 AC 109 ms
78,376 KB
testcase_09 AC 112 ms
78,464 KB
testcase_10 AC 110 ms
79,204 KB
testcase_11 AC 111 ms
78,528 KB
testcase_12 AC 110 ms
78,312 KB
testcase_13 AC 104 ms
77,912 KB
testcase_14 AC 112 ms
79,220 KB
testcase_15 AC 114 ms
79,180 KB
testcase_16 AC 113 ms
79,228 KB
testcase_17 AC 111 ms
79,176 KB
testcase_18 AC 105 ms
77,580 KB
testcase_19 AC 113 ms
79,056 KB
testcase_20 AC 93 ms
71,884 KB
testcase_21 AC 89 ms
71,404 KB
testcase_22 AC 114 ms
78,972 KB
testcase_23 AC 113 ms
78,980 KB
testcase_24 AC 114 ms
79,132 KB
testcase_25 AC 113 ms
79,020 KB
testcase_26 AC 89 ms
71,480 KB
testcase_27 AC 106 ms
77,860 KB
testcase_28 AC 114 ms
79,332 KB
testcase_29 AC 111 ms
78,288 KB
testcase_30 AC 91 ms
71,568 KB
testcase_31 AC 91 ms
71,660 KB
testcase_32 AC 110 ms
78,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 辺を張ってBFS or ダイクストラ法で間に合うか

N = int(input())
edges = [[] for i in range(N+1)]
for i in range(1, N):
    one_count = bin(i).count('1')
    if i+one_count <= N:
        edges[i].append(i+one_count)
    if i-one_count >= 1:
        edges[i].append(i-one_count)

from collections import deque
que = deque()
que.append(1)
INF = 10**10
distance = [INF]*(N+1)
distance[1] = 1
while que:
    current = que.popleft()
    for nxt in edges[current]:
        #print('current', current, 'nxt', nxt)
        if distance[nxt] > distance[current]+1:
            distance[nxt] = distance[current]+1
            que.append(nxt)
if distance[N] == INF:
    print(-1)
else:
    print(distance[N])





0