传送门:http://202.197.224.59/OnlineJudge2/index.php/Contest/read_problem/cid/43/pid/1260
Determinant
Bobo learned the definition of determinant det(A) of matrix A in ICPCCamp. He also knew determinant can be computed in O(n3) using Gaussian Elimination.
Bobo has an (n−1)×n matrix B he would like to find det(Bj) modulo (109+7) for all j∈{1,2,…,n} where Bj is the matrix after removing the j -th column from B .
Input
The input contains zero or more test cases and is terminated by end-of-file. For each test case:
The first line contains an integer n . The i -th of following n lines contains n integers Bi,1,Bi,2,…,Bi,n .
- 2≤n≤200
- 0≤Bi,j<109+7
- The sum of n does not exceed 2000 .
Output
For each case, output n integers which denote the result.
Sample Input
2 2 0 3 1 2 0 6 3 1
Sample Output
0 2 2 1 999999998
Note
For the second sample,官方题解:随机n个数把矩阵补全成n×n的。
那么就是要算伴随矩阵的第一行,也就是逆矩阵的第一列,高斯消元即可。
然后以下是Q巨的写法:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=205;
const int Mod=1000000007;
int a[MAXN][MAXN],b[MAXN][MAXN];
int get_rand(int x)//[0,x)
{
int t=1;
while((1<<t)<x)t++;
int res=x;
while(res>=x)
{
res=0;
for(int i=0;i<t;i++)
res|=(rand()%2)<<i;
}
return res;
}
int fp(int a,int k)
{
int res=1;
while(k)
{
if(k&1)res=1LL*res*a%Mod;
a=1LL*a*a%Mod;
k>>=1;
}
return res;
}
void solve(int n)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
b[i][j]=(i==j);
int det=1;
for(int i=1;i<=n;i++)
{
int t=i;
for(int k=i;k<=n;k++)
if(a[k][i])t=k;
if(t!=i)det*=-1;
for(int j=1;j<=n;j++)
{
swap(a[i][j],a[t][j]);
swap(b[i][j],b[t][j]);
}
det=1LL*a[i][i]*det%Mod;
int inv=fp(a[i][i],Mod-2);
for(int j=1;j<=n;j++)
{
a[i][j]=1LL*inv*a[i][j]%Mod;
b[i][j]=1LL*inv*b[i][j]%Mod;
}
for(int k=1;k<=n;k++)
{
if(k==i)continue;
int tmp=a[k][i];
for(int j=1;j<=n;j++)
{
a[k][j]=(a[k][j]-1LL*a[i][j]*tmp%Mod+Mod)%Mod;
b[k][j]=(b[k][j]-1LL*b[i][j]*tmp%Mod+Mod)%Mod;
}
}
}
det=(det+Mod)%Mod;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
b[i][j]=1LL*det*b[i][j]%Mod;
}
int main()
{
srand(time(NULL));
int n;
while(scanf("%d",&n)!=EOF)
{
for(int j=1;j<=n;j++)
a[1][j]=2;
for(int i=2;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]);
solve(n);
for(int i=1;i<=n;i++)
printf("%d%c",(i&1 ? b[i][1] : (Mod-b[i][1])%Mod)," \n"[i==n]);
}
return 0;
}