結果

問題 No.3 ビットすごろく
ユーザー tachyon777
提出日時 2020-02-13 13:33:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 194 ms / 5,000 ms
コード長 775 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,536 KB
実行使用メモリ 79,756 KB
最終ジャッジ日時 2024-07-01 09:40:45
合計ジャッジ時間 6,470 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

n = int(input())

lst1 = []
for i in range(1,100001):
    dig = len(bin(i))-2
    res = 0
    for j in range(dig):
        if i >> j & 1:
            res += 1
    lst1.append(res)

visited = [0]*n
visited[0] = 1
queue = [(1,0)] #res,now
ans = -1

heapq.heapify(queue)

while queue:
    res,now = heapq.heappop(queue)
    if now == n-1: #最小から取り出してるので、初めに行き着いたやつが答え
        ans = res
        break
    if 0 <= now-lst1[now] and visited[now-lst1[now]] == 0:
        visited[now-lst1[now]] = 1
        heapq.heappush(queue,(res+1,now-lst1[now]))
    if now+lst1[now] < n and visited[now+lst1[now]] == 0:
        visited[now+lst1[now]] = 1
        heapq.heappush(queue,(res+1,now+lst1[now]))

print(ans)
    
    
0