题目链接:点击这里
题目大意:
给定一个
n
n
n 阶行列式
A
A
A,求
∣
A
∣
|A|
∣A∣ 。结果对
p
p
p (
p
p
p 不一定是质数)取模
题目分析:
高斯消元,类似于求解线性方程组,消成三角矩阵,然后求三角矩阵的行列式即可
注意交换行列要对结果乘
−
1
-1
−1
具体细节见代码:
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
//#define int ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
int res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 1e3+5;
const int mod = 998244353;
const double pi = acos(-1);
const double eps = 1e-8;
int n,p,cnt = 1;
int a[maxn][maxn];
int qpow(int a,int b)
{
int res = 1;
while(b)
{
if(b&1) res = res*a%p;
a = a*a%p;
b >>= 1;
}
return res;
}
int Gauss(int a[][maxn],int n) // 求行列式
{
int res = 1;
for(int i = 1;i <= n;i++)
{
int pos = i;
for(int j = i+1;j <= n;j++)
if(a[j][i] > a[pos][i]) pos = j;
if(!a[pos][i]) return 0;
if(i^pos) swap(a[i],a[pos]),res *= -1;
for(int j = i+1;j <= n;j++)
{
if(a[j][i] > a[i][i]) swap(a[j],a[i]),res *= -1;
while(a[j][i])
{
int tmp = a[i][i]/a[j][i];
for(int k = i;k <= n;k++)
a[i][k] = (a[i][k]+(ll)(p-tmp)*a[j][k])%p;
swap(a[j],a[i]),res *= -1;
}
}
}
for(int i = 1;i <= n;i++) res = (ll)res*a[i][i]%p;
return (res+p)%p;
}
int main()
{
n = read(),p = read();
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++) scanf("%d",&a[i][j]),a[i][j] %= p;
cout<<Gauss(a,n)<<endl;
return 0;
}