行列式求值
当模数不为质数的时候,可以用辗转相减来消元。
每个数的大小在消元的过程都会变小,以这个作为势能,不难分析出复杂度为 \(\mathcal{O}(n^2(\log p+n))\).
代码实现参考 qyc 的板子,常数小而且好写,qyc nb!
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n';
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n';
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
r=0;bool w=0;char ch=getchar();
while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x, T2& ...y){ read(x); read(y...); }
int mod;
inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
int qpow(int x,int y){
int s=1;
while(y){
if(y&1)s=1ll*s*x%mod;
x=1ll*x*x%mod;
y>>=1;
}
return s;
}
const int N=610;
int n,m,t;
int A[N][N];
int det(){
int ans=1,tag=0;
for(int k=1;k<=n&&ans;ans=1ll*ans*A[k][k]%mod,k++){
for(int i=k+1,c;i<=n;i++){
while(A[k][k]){
c=A[i][k]/A[k][k];
for(int j=k;j<=n;j++)
cdel(A[i][j],1ll*A[k][j]*c%mod);
swap(A[i],A[k]),tag^=1;
}
swap(A[k],A[i]),tag^=1;
}
}
return tag?del(0,ans):ans;
}
signed main(){
read(n,mod);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
read(A[i][j]),A[i][j]%=mod;
cout << det() << '\n';
return 0;
}