結果

問題 No.3 ビットすごろく
ユーザー hr20khr20k
提出日時 2017-12-05 11:39:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 531 bytes
コンパイル時間 109 ms
コンパイル使用メモリ 10,876 KB
実行使用メモリ 8,936 KB
最終ジャッジ日時 2023-09-14 00:51:56
合計ジャッジ時間 2,121 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,568 KB
testcase_01 AC 18 ms
8,568 KB
testcase_02 AC 18 ms
8,468 KB
testcase_03 AC 23 ms
8,500 KB
testcase_04 AC 19 ms
8,452 KB
testcase_05 AC 25 ms
8,720 KB
testcase_06 AC 21 ms
8,536 KB
testcase_07 AC 20 ms
8,468 KB
testcase_08 AC 24 ms
8,668 KB
testcase_09 AC 27 ms
8,644 KB
testcase_10 AC 29 ms
8,760 KB
testcase_11 AC 26 ms
8,684 KB
testcase_12 AC 25 ms
8,704 KB
testcase_13 AC 21 ms
8,628 KB
testcase_14 AC 28 ms
8,888 KB
testcase_15 AC 31 ms
8,880 KB
testcase_16 AC 31 ms
8,872 KB
testcase_17 AC 33 ms
8,776 KB
testcase_18 AC 19 ms
8,504 KB
testcase_19 AC 32 ms
8,780 KB
testcase_20 AC 18 ms
8,416 KB
testcase_21 AC 17 ms
8,488 KB
testcase_22 AC 28 ms
8,880 KB
testcase_23 AC 31 ms
8,936 KB
testcase_24 AC 31 ms
8,936 KB
testcase_25 AC 31 ms
8,928 KB
testcase_26 AC 17 ms
8,416 KB
testcase_27 AC 21 ms
8,448 KB
testcase_28 AC 31 ms
8,848 KB
testcase_29 AC 28 ms
8,664 KB
testcase_30 AC 18 ms
8,488 KB
testcase_31 AC 18 ms
8,556 KB
testcase_32 AC 27 ms
8,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys

N = int(input())
q = deque([1])
d_min = [1] + [sys.maxsize] * (N - 1)

while(len(q) != 0):
	now = q.popleft()
	backword = now - bin(now).count('1')
	forword = now + bin(now).count('1')
	if backword > 0 and d_min[backword - 1] > d_min[now - 1] + 1:
		d_min[backword - 1] = d_min[now - 1] + 1
		q.append(backword)
	if forword <= N and d_min[forword - 1] > d_min[now - 1] + 1:
		d_min[forword - 1] = d_min[now - 1] + 1
		q.append(forword)

print(d_min[-1] if d_min[-1] != sys.maxsize else -1)
0