結果

問題 No.2255 Determinant Sum
ユーザー shobonvipshobonvip
提出日時 2023-03-12 01:20:25
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,140 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 81,544 KB
実行使用メモリ 76,960 KB
最終ジャッジ日時 2023-10-18 20:36:06
合計ジャッジ時間 3,255 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,452 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 94 ms
76,092 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 91 ms
76,248 KB
testcase_11 AC 76 ms
75,792 KB
testcase_12 WA -
testcase_13 AC 111 ms
76,216 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 88 ms
76,412 KB
testcase_19 AC 85 ms
76,224 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

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(x[i])
		if y[i] == 0:
			by.append(y[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