結果

問題 No.3 ビットすごろく
ユーザー pekempeypekempey
提出日時 2015-08-23 11:55:16
言語 Python2
(2.7.18)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 403 bytes
コンパイル時間 148 ms
コンパイル使用メモリ 6,640 KB
実行使用メモリ 6,888 KB
最終ジャッジ日時 2023-09-13 23:20:01
合計ジャッジ時間 2,588 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
6,612 KB
testcase_01 AC 14 ms
6,576 KB
testcase_02 AC 15 ms
6,608 KB
testcase_03 AC 24 ms
6,688 KB
testcase_04 AC 18 ms
6,572 KB
testcase_05 AC 36 ms
6,680 KB
testcase_06 AC 25 ms
6,524 KB
testcase_07 AC 20 ms
6,500 KB
testcase_08 AC 31 ms
6,760 KB
testcase_09 AC 42 ms
6,780 KB
testcase_10 AC 48 ms
6,676 KB
testcase_11 AC 40 ms
6,616 KB
testcase_12 AC 36 ms
6,744 KB
testcase_13 AC 23 ms
6,680 KB
testcase_14 AC 47 ms
6,832 KB
testcase_15 AC 54 ms
6,760 KB
testcase_16 AC 51 ms
6,716 KB
testcase_17 AC 53 ms
6,860 KB
testcase_18 AC 21 ms
6,540 KB
testcase_19 AC 54 ms
6,888 KB
testcase_20 AC 17 ms
6,540 KB
testcase_21 AC 15 ms
6,608 KB
testcase_22 AC 48 ms
6,708 KB
testcase_23 AC 55 ms
6,744 KB
testcase_24 AC 55 ms
6,724 KB
testcase_25 AC 54 ms
6,736 KB
testcase_26 AC 15 ms
6,640 KB
testcase_27 AC 24 ms
6,548 KB
testcase_28 AC 51 ms
6,748 KB
testcase_29 AC 41 ms
6,744 KB
testcase_30 AC 16 ms
6,536 KB
testcase_31 AC 15 ms
6,488 KB
testcase_32 AC 38 ms
6,608 KB
権限があれば一括ダウンロードができます

ソースコード

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