結果

問題 No.3 ビットすごろく
コンテスト
ユーザー calei
提出日時 2019-12-28 19:42:34
言語 Python3
(3.14.3 + numpy 2.4.2 + scipy 1.17.0)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 114 ms / 5,000 ms
コード長 673 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 375 ms
コンパイル使用メモリ 20,956 KB
実行使用メモリ 17,880 KB
最終ジャッジ日時 2026-03-22 07:07:45
合計ジャッジ時間 4,732 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# 2019/12/28

from heapq import heappop,heappush

n=int(input())

edge=[[] for _ in range(n+1)]
for i in range(n+1):
    u,v=i,i+bin(i).count('1')
    if v<=n:
        edge[u].append((u,v))
    u,v=i,i-bin(i).count('1')
    if v>0:
        edge[u].append((u,v))


def dijkstra(p,s):
    d=[float('inf')]*(n+1)
    d[s]=1
    que=[]
    for e in edge[s]:
        heappush(que,e)
    while que:
        p,v=heappop(que)
        if d[v]<=d[p]+1:continue
        d[v]=d[p]+1
        for pp,vtx in edge[v]:
            if d[vtx]<=d[pp]+1:continue
            hq=(pp,vtx)
            heappush(que,hq)
    return d


res=dijkstra(0,1)
print(res[n] if res[n]!=float('inf') else -1)
0