結果

問題 No.3 ビットすごろく
ユーザー kokoa1046kokoa1046
提出日時 2018-02-01 16:56:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 864 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 11,084 KB
実行使用メモリ 8,688 KB
最終ジャッジ日時 2023-09-14 00:54:02
合計ジャッジ時間 2,161 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,308 KB
testcase_01 AC 18 ms
8,360 KB
testcase_02 AC 18 ms
8,388 KB
testcase_03 AC 20 ms
8,260 KB
testcase_04 AC 18 ms
8,192 KB
testcase_05 AC 24 ms
8,356 KB
testcase_06 AC 20 ms
8,248 KB
testcase_07 AC 18 ms
8,236 KB
testcase_08 AC 22 ms
8,332 KB
testcase_09 AC 26 ms
8,540 KB
testcase_10 AC 29 ms
8,568 KB
testcase_11 AC 26 ms
8,388 KB
testcase_12 AC 24 ms
8,400 KB
testcase_13 AC 20 ms
8,224 KB
testcase_14 AC 28 ms
8,424 KB
testcase_15 AC 31 ms
8,624 KB
testcase_16 AC 30 ms
8,488 KB
testcase_17 AC 32 ms
8,644 KB
testcase_18 AC 19 ms
8,220 KB
testcase_19 AC 31 ms
8,652 KB
testcase_20 AC 17 ms
7,972 KB
testcase_21 AC 16 ms
8,368 KB
testcase_22 AC 28 ms
8,596 KB
testcase_23 AC 32 ms
8,616 KB
testcase_24 AC 32 ms
8,512 KB
testcase_25 AC 31 ms
8,580 KB
testcase_26 AC 16 ms
8,360 KB
testcase_27 AC 19 ms
8,304 KB
testcase_28 AC 30 ms
8,688 KB
testcase_29 AC 25 ms
8,472 KB
testcase_30 AC 18 ms
8,212 KB
testcase_31 AC 16 ms
8,336 KB
testcase_32 AC 25 ms
8,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
# Your code here!
""" 
2進数でたってる1の数
""" 
def bitget(x):
    ret = 0
    while (x):
        x = x ^ (-x & x)
        ret += 1
    return ret

n = int(input())
table = [0 for i in range(n+1)]
table[1] = 1
q = [1]
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