結果

問題 No.2255 Determinant Sum
ユーザー shobonvipshobonvip
提出日時 2023-03-20 05:39:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 344 ms / 2,000 ms
コード長 1,134 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 81,548 KB
実行使用メモリ 77,088 KB
最終ジャッジ日時 2023-10-18 20:36:17
合計ジャッジ時間 3,856 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,452 KB
testcase_01 AC 42 ms
53,452 KB
testcase_02 AC 54 ms
63,652 KB
testcase_03 AC 56 ms
63,936 KB
testcase_04 AC 59 ms
66,260 KB
testcase_05 AC 42 ms
55,500 KB
testcase_06 AC 98 ms
76,092 KB
testcase_07 AC 257 ms
77,088 KB
testcase_08 AC 156 ms
76,540 KB
testcase_09 AC 186 ms
76,760 KB
testcase_10 AC 96 ms
76,252 KB
testcase_11 AC 81 ms
75,800 KB
testcase_12 AC 126 ms
76,960 KB
testcase_13 AC 109 ms
76,216 KB
testcase_14 AC 344 ms
76,584 KB
testcase_15 AC 149 ms
76,428 KB
testcase_16 AC 91 ms
76,168 KB
testcase_17 AC 155 ms
76,432 KB
testcase_18 AC 92 ms
76,420 KB
testcase_19 AC 88 ms
75,908 KB
testcase_20 AC 101 ms
76,120 KB
testcase_21 AC 137 ms
76,612 KB
testcase_22 AC 93 ms
76,504 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
	if mode == 0:
		print(0)
		continue
	
	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