-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeterminant_mod.cpp
61 lines (54 loc) · 1.15 KB
/
determinant_mod.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <vector>
using namespace std;
long long powmod(long long a, long long b, long long m)
{
long long r = 1;
for (; b>0; b>>=1, a=a*a%m)
if (b&1)
r = r*a%m;
return r;
}
long long determinant_mod(vector<vector<long long>> A, long long M)
{
int n = (int)A.size();
long long d = 1;
for (int i=0; i<n; i++)
{
if (A[i][i]==0)
{
for (int j=i+1; j<n; j++)
if (A[j][i]!=0)
{
A[i].swap(A[j]);
d = -d;
break;
}
if (A[i][i]==0)
return 0;
}
for (int j=i+1; j<n; j++)
{
long long x = A[j][i]*powmod(A[i][i], M-2, M)%M;
for (int k=i; k<n; k++)
(A[j][k] += M-A[i][k]*x%M) %= M;
}
}
d = powmod(d+M, M-2, M);
for (int i=0; i<n; i++)
d = d*A[i][i]%M;
return d;
}
#include <iostream>
int main()
{
cout<<determinant_mod({
{2, 2},
{3, 6},
}, 1000000007)<<endl;
// 6
cout<<determinant_mod({
{2, 2},
{3, 6},
}, 5)<<endl;
// 1
}