結果

問題 No.2958 Placing Many L-s
ユーザー hiro1729hiro1729
提出日時 2024-11-08 22:09:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 3,284 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 79,360 KB
最終ジャッジ日時 2024-11-08 22:09:55
合計ジャッジ時間 5,320 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

for _ in range(int(input())):
	N, M = map(int, input().split())
	if N >= 3 and N % 2 == 1 and M % 8 == 0:
		print(N * M // 4)
		ans = [[0] * M for _ in range(N)]
		c = 1
		for i in range(0, N - 3, 2):
			for j in range(0, M, 4):
				ans[i][j] = c
				ans[i][j + 1] = c
				ans[i][j + 2] = c
				ans[i][j + 3] = c + 1
				ans[i + 1][j] = c
				ans[i + 1][j + 1] = c + 1
				ans[i + 1][j + 2] = c + 1
				ans[i + 1][j + 3] = c + 1
				c += 2
		for j in range(0, M, 8):
			ans[N - 3][j] = c
			ans[N - 3][j + 1] = c + 1
			ans[N - 3][j + 2] = c + 1
			ans[N - 3][j + 3] = c + 1
			ans[N - 3][j + 4] = c + 2
			ans[N - 3][j + 5] = c + 2
			ans[N - 3][j + 6] = c + 2
			ans[N - 3][j + 7] = c + 3
			ans[N - 2][j] = c
			ans[N - 2][j + 1] = c + 1
			ans[N - 2][j + 2] = c + 4
			ans[N - 2][j + 3] = c + 5
			ans[N - 2][j + 4] = c + 5
			ans[N - 2][j + 5] = c + 5
			ans[N - 2][j + 6] = c + 2
			ans[N - 2][j + 7] = c + 3
			ans[N - 1][j] = c
			ans[N - 1][j + 1] = c
			ans[N - 1][j + 2] = c + 4
			ans[N - 1][j + 3] = c + 4
			ans[N - 1][j + 4] = c + 4
			ans[N - 1][j + 5] = c + 5
			ans[N - 1][j + 6] = c + 3
			ans[N - 1][j + 7] = c + 3
			c += 6
		for i in range(N):
			print(*ans[i])
		continue
	if M >= 3 and M % 2 == 1 and N % 8 == 0:
		print(N * M // 4)
		ans = [[0] * M for _ in range(N)]
		c = 1
		for i in range(0, N, 4):
			for j in range(0, M - 3, 2):
				ans[i][j] = c
				ans[i][j + 1] = c
				ans[i + 1][j] = c
				ans[i + 1][j + 1] = c + 1
				ans[i + 2][j] = c
				ans[i + 2][j + 1] = c + 1
				ans[i + 3][j] = c + 1
				ans[i + 3][j + 1] = c + 1
				c += 2
		for i in range(0, N, 8):
			ans[i][M - 3] = c
			ans[i][M - 2] = c
			ans[i][M - 1] = c
			ans[i + 1][M - 3] = c
			ans[i + 1][M - 2] = c + 1
			ans[i + 1][M - 1] = c + 1
			ans[i + 2][M - 3] = c + 4
			ans[i + 2][M - 2] = c + 4
			ans[i + 2][M - 1] = c + 1
			ans[i + 3][M - 3] = c + 4
			ans[i + 3][M - 2] = c + 5
			ans[i + 3][M - 1] = c + 1
			ans[i + 4][M - 3] = c + 4
			ans[i + 4][M - 2] = c + 5
			ans[i + 4][M - 1] = c + 2
			ans[i + 5][M - 3] = c + 5
			ans[i + 5][M - 2] = c + 5
			ans[i + 5][M - 1] = c + 2
			ans[i + 6][M - 3] = c + 3
			ans[i + 6][M - 2] = c + 2
			ans[i + 6][M - 1] = c + 2
			ans[i + 7][M - 3] = c + 3
			ans[i + 7][M - 2] = c + 3
			ans[i + 7][M - 1] = c + 3
			c += 6
		for i in range(N):
			print(*ans[i])
		continue
	if N % 4 == 0 and M % 2 == 0:
		print(N * M // 4)
		ans = [[0] * M for _ in range(N)]
		c = 1
		for i in range(N // 4):
			for j in range(M // 2):
				ans[4 * i][2 * j] = c
				ans[4 * i][2 * j + 1] = c
				ans[4 * i + 1][2 * j] = c
				ans[4 * i + 1][2 * j + 1] = c + 1
				ans[4 * i + 2][2 * j] = c
				ans[4 * i + 2][2 * j + 1] = c + 1
				ans[4 * i + 3][2 * j] = c + 1
				ans[4 * i + 3][2 * j + 1] = c + 1
				c += 2
		for i in range(N):
			print(*ans[i])
		continue
	elif N % 2 == 0 and M % 4 == 0:
		print(N * M // 4)
		ans = [[0] * M for _ in range(N)]
		c = 1
		for i in range(N // 2):
			for j in range(M // 4):
				ans[2 * i][4 * j] = c
				ans[2 * i + 1][4 * j] = c
				ans[2 * i][4 * j + 1] = c
				ans[2 * i + 1][4 * j + 1] = c + 1
				ans[2 * i][4 * j + 2] = c
				ans[2 * i + 1][4 * j + 2] = c + 1
				ans[2 * i][4 * j + 3] = c + 1
				ans[2 * i + 1][4 * j + 3] = c + 1
				c += 2
		for i in range(N):
			print(*ans[i])
		continue
	print(-1)
0