結果

問題 No.3 ビットすごろく
ユーザー yaoshimax
提出日時 2015-02-09 00:22:53
言語 Python2
(2.7.18)
結果
AC  
実行時間 35 ms / 5,000 ms
コード長 547 bytes
コンパイル時間 80 ms
コンパイル使用メモリ 6,948 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2024-07-01 07:12:22
合計ジャッジ時間 1,984 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
from heapq import heappush, heappop

N = int(raw_input())
heap = []
heappush(heap,(1,1))
arrived = [False for i in range(N+1)]
while len(heap)>0:
   dist, num = heappop(heap)
   if arrived[num]:
      continue
   if num == N:
      print dist
      exit()
   arrived[num]=True
   tmp = num
   cnt = 0
   while tmp != 0: 
      tmp &= tmp-1
      cnt+=1
   if num-cnt > 0 and arrived[num-cnt] ==False:
      heappush(heap,(dist+1,num-cnt))
   if num+cnt <= N and arrived[num+cnt] ==False:
      heappush(heap,(dist+1,num+cnt))
print -1
0