結果

問題 No.3 ビットすごろく
ユーザー caleicalei
提出日時 2019-12-28 19:42:34
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 673 bytes
コンパイル時間 89 ms
コンパイル使用メモリ 10,768 KB
実行使用メモリ 11,724 KB
最終ジャッジ日時 2023-09-14 01:33:42
合計ジャッジ時間 2,392 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,296 KB
testcase_01 AC 17 ms
8,228 KB
testcase_02 AC 17 ms
8,228 KB
testcase_03 AC 24 ms
9,052 KB
testcase_04 AC 19 ms
8,404 KB
testcase_05 AC 32 ms
9,840 KB
testcase_06 AC 24 ms
9,088 KB
testcase_07 AC 22 ms
8,692 KB
testcase_08 AC 29 ms
9,584 KB
testcase_09 AC 36 ms
10,620 KB
testcase_10 AC 40 ms
11,188 KB
testcase_11 AC 34 ms
10,356 KB
testcase_12 AC 32 ms
9,792 KB
testcase_13 AC 23 ms
8,904 KB
testcase_14 AC 39 ms
10,924 KB
testcase_15 AC 44 ms
11,724 KB
testcase_16 AC 42 ms
11,496 KB
testcase_17 AC 43 ms
11,560 KB
testcase_18 AC 21 ms
8,740 KB
testcase_19 AC 44 ms
11,632 KB
testcase_20 AC 18 ms
8,412 KB
testcase_21 AC 17 ms
8,176 KB
testcase_22 AC 39 ms
10,988 KB
testcase_23 AC 44 ms
11,616 KB
testcase_24 AC 44 ms
11,720 KB
testcase_25 AC 45 ms
11,700 KB
testcase_26 AC 17 ms
8,212 KB
testcase_27 AC 23 ms
8,988 KB
testcase_28 AC 42 ms
11,192 KB
testcase_29 AC 35 ms
10,304 KB
testcase_30 AC 17 ms
8,352 KB
testcase_31 AC 18 ms
8,384 KB
testcase_32 AC 33 ms
10,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 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