結果

問題 No.3 ビットすごろく
ユーザー pajannatpajannat
提出日時 2021-04-11 17:56:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 161 ms / 5,000 ms
コード長 975 bytes
コンパイル時間 762 ms
コンパイル使用メモリ 10,820 KB
実行使用メモリ 12,536 KB
最終ジャッジ日時 2023-09-09 20:04:01
合計ジャッジ時間 4,698 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,564 KB
testcase_01 AC 19 ms
8,512 KB
testcase_02 AC 18 ms
8,576 KB
testcase_03 AC 47 ms
9,356 KB
testcase_04 AC 26 ms
8,864 KB
testcase_05 AC 89 ms
10,720 KB
testcase_06 AC 51 ms
9,588 KB
testcase_07 AC 36 ms
9,076 KB
testcase_08 AC 73 ms
10,272 KB
testcase_09 AC 113 ms
11,200 KB
testcase_10 AC 132 ms
11,624 KB
testcase_11 AC 104 ms
11,100 KB
testcase_12 AC 87 ms
10,640 KB
testcase_13 AC 43 ms
9,364 KB
testcase_14 AC 128 ms
11,668 KB
testcase_15 AC 157 ms
12,508 KB
testcase_16 AC 147 ms
12,264 KB
testcase_17 AC 157 ms
12,344 KB
testcase_18 AC 39 ms
9,312 KB
testcase_19 AC 160 ms
12,388 KB
testcase_20 AC 24 ms
8,824 KB
testcase_21 AC 18 ms
8,500 KB
testcase_22 AC 129 ms
11,552 KB
testcase_23 AC 160 ms
12,528 KB
testcase_24 AC 161 ms
12,536 KB
testcase_25 AC 158 ms
12,328 KB
testcase_26 AC 18 ms
8,696 KB
testcase_27 AC 45 ms
9,492 KB
testcase_28 AC 144 ms
12,192 KB
testcase_29 AC 105 ms
10,992 KB
testcase_30 AC 19 ms
8,520 KB
testcase_31 AC 20 ms
8,672 KB
testcase_32 AC 98 ms
10,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
	N = int(input())

	# 10進数を2進数に変換
	def BitTrans(n):
		i = 0
		bit = []
		while n > 0:
			bit.insert(0, n % 2)
			n = int(n / 2)
			i += 1
		return bit

	# 2進数表記に含まれる1の数を数える
	def BitCount(bit):
		cnt = 0
		for i in bit:
			cnt += i
		return cnt

	list = []
	cnt = 0
	for i in range(1,N):
		if (i - BitCount(BitTrans(i))) > 0:
			list.append([i,(i - BitCount(BitTrans(i)))])
			cnt += 1
		if (i + BitCount(BitTrans(i))) < N + 1:
			list.append([i, (i + BitCount(BitTrans(i)))])
			cnt += 1


	from collections import deque

	n, m = N, cnt

	graph = [[] for _ in range(n+1)]

	for j in range(m):
		a, b =  list[j][0], list[j][1]
		graph[a].append(b)

	dist = [-1] * (n+1)
	dist[0] = 0
	dist[1] = 1

	d = deque()
	d.append(1)

	while d:
		v = d.popleft()
		for i in graph[v]:
			if dist[i] != -1:
				continue
			dist[i] = dist[v] + 1
			d.append(i)

	ans = dist[1:]
	print(ans[-1])

if __name__ == '__main__':
    main()
0