結果

問題 No.2255 Determinant Sum
ユーザー shobonvipshobonvip
提出日時 2023-03-20 05:39:27
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,097 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 81,632 KB
実行使用メモリ 77,516 KB
最終ジャッジ日時 2023-10-18 20:36:26
合計ジャッジ時間 3,500 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,512 KB
testcase_01 AC 37 ms
53,512 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 97 ms
76,124 KB
testcase_07 AC 252 ms
77,124 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 92 ms
76,244 KB
testcase_11 WA -
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 336 ms
76,612 KB
testcase_15 RE -
testcase_16 AC 89 ms
76,304 KB
testcase_17 AC 150 ms
76,464 KB
testcase_18 AC 87 ms
76,444 KB
testcase_19 RE -
testcase_20 RE -
testcase_21 AC 129 ms
76,644 KB
testcase_22 AC 92 ms
76,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def det(M, mod):
	M = [row[:] for row in M]
	N, sign, prev = len(M), 1, 1
	for i in range(N-1):
		#print(*M, sep="\n")
		#print()
		if M[i][i] == 0:
			swapto = next((j for j in range(i+1, N) if M[j][i] != 0), None)
			if swapto is None:
				return 0
			M[i], M[swapto], sign = M[swapto], M[i], -sign
		previnv = pow(prev, mod-2, mod)
		for j in range(i+1, N):
			for k in range(i+1, N):
				M[j][k] = (M[j][k]*M[i][i]%mod-M[j][i]*M[i][k]%mod)*previnv%mod
		prev = M[i][i]
	return sign*M[-1][-1]%mod


T = int(input())
for _ in range(T):
	n, p = map(int,input().split())
	a = [list(map(int,input().split())) for i in range(n)]
	if p > 2:
		print(0)
		continue
	# p = 2
	x = [0] * n
	y = [0] * n
	for i in range(n):
		for j in range(n):
			if a[i][j] == -1:
				x[i] += 1
				y[j] += 1
	
	mode = 1
	for i in range(n):
		if x[i] > 1 or y[i] > 1:
			mode = 0
			break
	
	bx = []
	by = []
	for i in range(n):
		if x[i] == 0:
			bx.append(i)
		if y[i] == 0:
			by.append(i)

	m = len(bx)
	b = [[a[bx[i]][by[j]] for j in range(m)]for i in range(m)]
	
	if len(b) > 0:
		print(det(b, 2))
	else:
		print(1)
0