Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

计算行列式的值模板

用于快速求行列式的值。

用类似高斯消元的方法。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int det ()
{
int res = 1;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
{
while (A[i][i])
{
int t = A[j][i] / A[i][i];
for (int k = i; k <= n; k++)
(A[j][k] -= (LL)t * A[i][k] % mod) %= mod;
swap(A[i], A[j]);
res *= -1;
}
swap(A[i], A[j]);
res *= -1;
}
for (int i = 1; i <= n; i++)
res = (LL)res * A[i][i] % mod;
return res;
}