結果

問題 No.3 ビットすごろく
ユーザー asaka189asaka189
提出日時 2018-01-31 14:59:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 831 bytes
コンパイル時間 391 ms
コンパイル使用メモリ 10,856 KB
実行使用メモリ 8,624 KB
最終ジャッジ日時 2023-08-28 17:30:37
合計ジャッジ時間 2,338 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,292 KB
testcase_01 AC 17 ms
8,288 KB
testcase_02 AC 16 ms
8,260 KB
testcase_03 AC 21 ms
8,276 KB
testcase_04 AC 17 ms
8,284 KB
testcase_05 AC 26 ms
8,428 KB
testcase_06 AC 21 ms
8,268 KB
testcase_07 AC 20 ms
8,160 KB
testcase_08 AC 25 ms
8,252 KB
testcase_09 AC 30 ms
8,412 KB
testcase_10 AC 32 ms
8,528 KB
testcase_11 AC 29 ms
8,508 KB
testcase_12 AC 26 ms
8,276 KB
testcase_13 AC 20 ms
8,220 KB
testcase_14 AC 31 ms
8,452 KB
testcase_15 AC 35 ms
8,588 KB
testcase_16 AC 34 ms
8,408 KB
testcase_17 AC 36 ms
8,624 KB
testcase_18 AC 19 ms
8,224 KB
testcase_19 AC 36 ms
8,568 KB
testcase_20 AC 17 ms
8,200 KB
testcase_21 AC 16 ms
8,192 KB
testcase_22 AC 32 ms
8,444 KB
testcase_23 AC 36 ms
8,572 KB
testcase_24 AC 36 ms
8,520 KB
testcase_25 AC 36 ms
8,484 KB
testcase_26 RE -
testcase_27 AC 20 ms
8,184 KB
testcase_28 AC 34 ms
8,488 KB
testcase_29 AC 29 ms
8,376 KB
testcase_30 AC 16 ms
8,320 KB
testcase_31 AC 17 ms
8,340 KB
testcase_32 AC 27 ms
8,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def bitget(a):
	s = str(bin(a))
	n = len(s)
	counter = 0
	for i in range(n):
		if s[i] == '1':
			counter += 1
	return counter

n = int(input())
table = [0 for i in range(n+1)]
table[1] = 1
table[2] = 2
q = [2]
next = 0
while len(q) != 0:
	if len(q) == 1:
		next = bitget(q[0])
		if table[q[0]-next] == 0:
			table[q[0]-next] = table[q[0]]+1
			q.append(q[0]-next)
		if q[0]+next <= n and table[q[0]+next] == 0:
			table[q[0]+next] = table[q[0]]+1
			q.append(q[0]+next)
		q.pop(0)
	else:
		k = len(q)
		for i in range(k):
			next = bitget(q[i])
			if table[q[i]-next] == 0:
				table[q[i]-next] = table[q[i]]+1
				q.append(q[i]-next)
			if q[i]+next <= n and table[q[i]+next] == 0:
				table[q[i]+next] = table[q[i]]+1
				q.append(q[i]+next)
		for i in range(k):
			q.pop(0)
if table[n] != 0:
	print(table[n])
else:
	print('-1')
0