結果

問題 No.3 ビットすごろく
ユーザー pekempey
提出日時 2015-08-23 11:55:16
言語 Python2
(2.7.18)
結果
AC  
実行時間 58 ms / 5,000 ms
コード長 403 bytes
コンパイル時間 81 ms
コンパイル使用メモリ 6,912 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2024-07-01 07:29:34
合計ジャッジ時間 2,604 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import Queue

N = int(raw_input())
INF = float('inf')
dist = [INF] * (N + 1)

q = Queue.Queue()
q.put(1)
dist[1] = 1

while not q.empty():
	p = q.get()
	c = bin(p).count('1')
	if p - c >= 1 and dist[p - c] > dist[p] + 1:
		dist[p - c] = dist[p] + 1
		q.put(p - c)
	if p + c <= N and dist[p + c] > dist[p] + 1:
		dist[p + c] = dist[p] + 1
		q.put(p + c)

ans = dist[N]
if ans == INF: ans = -1;
print ans
0