結果

問題 No.3 ビットすごろく
ユーザー FromBooskaFromBooska
提出日時 2023-09-12 20:06:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 155 ms / 5,000 ms
コード長 1,043 bytes
コンパイル時間 904 ms
コンパイル使用メモリ 86,616 KB
実行使用メモリ 79,760 KB
最終ジャッジ日時 2023-09-12 20:06:35
合計ジャッジ時間 6,134 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
71,224 KB
testcase_01 AC 75 ms
70,964 KB
testcase_02 AC 76 ms
71,004 KB
testcase_03 AC 120 ms
77,764 KB
testcase_04 AC 86 ms
75,512 KB
testcase_05 AC 136 ms
78,436 KB
testcase_06 AC 116 ms
78,196 KB
testcase_07 AC 107 ms
77,400 KB
testcase_08 AC 132 ms
77,856 KB
testcase_09 AC 143 ms
79,360 KB
testcase_10 AC 145 ms
79,268 KB
testcase_11 AC 140 ms
78,972 KB
testcase_12 AC 136 ms
78,440 KB
testcase_13 AC 112 ms
77,792 KB
testcase_14 AC 147 ms
79,412 KB
testcase_15 AC 154 ms
79,408 KB
testcase_16 AC 152 ms
79,444 KB
testcase_17 AC 154 ms
79,760 KB
testcase_18 AC 109 ms
77,880 KB
testcase_19 AC 155 ms
79,412 KB
testcase_20 AC 77 ms
71,052 KB
testcase_21 AC 75 ms
71,196 KB
testcase_22 AC 148 ms
79,412 KB
testcase_23 AC 153 ms
79,572 KB
testcase_24 AC 152 ms
79,400 KB
testcase_25 AC 154 ms
79,448 KB
testcase_26 AC 76 ms
70,828 KB
testcase_27 AC 114 ms
77,640 KB
testcase_28 AC 151 ms
79,256 KB
testcase_29 AC 141 ms
79,092 KB
testcase_30 AC 77 ms
71,040 KB
testcase_31 AC 76 ms
71,000 KB
testcase_32 AC 141 ms
79,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# うーん、愚直にダイクストラで間に合うのか

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

from heapq import heappush, heappop
INF = 10 ** 18
def dijkstra(s, n, connect): #(始点, ノード数)
    distance = [INF] * n
    que = [(0, s)] #(distance, node)
    distance[s] = 0
    confirmed = [False] * n # ノードが確定済みかどうか
    while que:
        w,v = heappop(que)
        if distance[v]<w:
            continue
        confirmed[v] = True
        for to, cost in connect[v]: # ノード v に隣接しているノードに対して
            if confirmed[to] == False and distance[v] + cost < distance[to]:
                distance[to] = distance[v] + cost
                heappush(que, (distance[to], to))
    return distance
 
distance = dijkstra(1, N+1, edges)
ans = distance[N]+1
if ans >= INF:
    print(-1)
else:
    print(ans)
0