結果

問題 No.3 ビットすごろく
ユーザー dashi_dragonsdashi_dragons
提出日時 2017-02-13 21:48:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 35 ms / 5,000 ms
コード長 548 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 11,012 KB
実行使用メモリ 8,880 KB
最終ジャッジ日時 2023-09-14 00:20:27
合計ジャッジ時間 2,252 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,784 KB
testcase_01 AC 16 ms
7,752 KB
testcase_02 AC 16 ms
7,796 KB
testcase_03 AC 20 ms
8,336 KB
testcase_04 AC 17 ms
7,824 KB
testcase_05 AC 26 ms
8,480 KB
testcase_06 AC 21 ms
8,252 KB
testcase_07 AC 19 ms
8,188 KB
testcase_08 AC 24 ms
8,504 KB
testcase_09 AC 28 ms
8,588 KB
testcase_10 AC 31 ms
8,872 KB
testcase_11 AC 27 ms
8,584 KB
testcase_12 AC 26 ms
8,544 KB
testcase_13 AC 19 ms
8,328 KB
testcase_14 AC 31 ms
8,820 KB
testcase_15 AC 34 ms
8,880 KB
testcase_16 AC 33 ms
8,832 KB
testcase_17 AC 34 ms
8,776 KB
testcase_18 AC 19 ms
8,196 KB
testcase_19 AC 35 ms
8,776 KB
testcase_20 AC 17 ms
7,752 KB
testcase_21 AC 16 ms
7,856 KB
testcase_22 AC 31 ms
8,724 KB
testcase_23 AC 35 ms
8,772 KB
testcase_24 AC 35 ms
8,876 KB
testcase_25 AC 34 ms
8,880 KB
testcase_26 AC 16 ms
7,808 KB
testcase_27 AC 19 ms
8,372 KB
testcase_28 AC 33 ms
8,876 KB
testcase_29 AC 28 ms
8,564 KB
testcase_30 AC 16 ms
7,796 KB
testcase_31 AC 17 ms
7,784 KB
testcase_32 AC 27 ms
8,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def bitcnt(N):
	cnt = 0

	while N != 0:
		if N % 2 == 1:
			cnt += 1
		N = N // 2
	return cnt

N = int(input())

loc = 1
queue = [(1,1)]
list = {1:1}

while True:

	loc = queue[0][0]
	cnt = queue[0][1] + 1

	mov = bitcnt(loc)

	if loc + mov <= N:
		if loc + mov not in list:
			list[loc + mov] = cnt
			queue.append((loc + mov,cnt))

	if loc - mov >= 1:
		if loc - mov not in list:
			list[loc - mov] = cnt
			queue.append((loc - mov,cnt))
	queue.pop(0)

	if queue == []:
		break
#print(list)

if N not in list:
	print(-1)
else:
	print(list[N])		
0