QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#449675#8473. Matrices and Determinants275307894aAC ✓84ms4088kbC++143.3kb2024-06-21 15:54:142024-06-21 15:54:15

Judging History

你现在查看的是最新测评结果

  • [2024-06-21 15:54:15]
  • 评测
  • 测评结果:AC
  • 用时:84ms
  • 内存:4088kb
  • [2024-06-21 15:54:14]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=10+5,M=N*4+5,K=1000+5,mod=1e9+7,Mod=mod-1;const db eps=1e-9;const int INF=2e9+7;mt19937 rnd(time(0));
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#ifdef LOCAL
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
	#else 
	#define gdb(...) void()
	#endif
}using namespace Debug;
int n,m,k;
using mat=array<array<ll,N>,N>;
mat A,B,C,D,I;
void clr(mat &A){A=I;}
ll Gauss(mat &A){
	for(int i=1;i<=n;i++) A[i][i+n]=1;
	ll mul=1;
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++) while(A[j][i]){
			int x=A[i][i]/A[j][i];
			for(int h=i;h<=2*n;h++) A[i][h]-=x*A[j][h];
			swap(A[i],A[j]);
			mul*=-1;
		}
	}
	for(int i=1;i<=n;i++) mul=mul*A[i][i];
	return mul;
}
void inve(mat &A){
	for(int i=1;i<=n;i++) A[i][i+n]=1;
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++) if(A[j][i]){swap(A[j],A[i]);break;}
		for(int j=i+1;j<=n;j++)while(A[j][i]){
			ll x=A[i][i]/A[j][i];
			for(int h=i;h<=2*n;h++) A[i][h]-=x*A[j][h];
			swap(A[i],A[j]);
		}
	}
	for(int i=n;i;i--){
		ll x=A[i][i];
		for(int j=i;j<=2*n;j++) A[i][j]/=x;
		for(int j=1;j<i;j++){
			ll x=A[j][i];
			for(int h=i;h<=2*n;h++) A[j][h]-=x*A[i][h];
		}
	}
	for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) A[i][j]=A[i][j+n];
}
mat operator *(const mat &A,const mat &B){
	mat C=I;
	for(int h=1;h<=n;h++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) C[i][j]+=A[i][h]*B[h][j];
	return C;
}
void exgcd(ll x,ll y,ll &a,ll &b){
	if(!y){a=x;b=0;return;}
	exgcd(y,x%y,b,a);b-=x/y*a;
}
void construct(int n,ll k){
	ll g=__gcd(k,A[n][n]);
	B[n][n]=g;C[n][n]=A[n][n]/g;
	if(n>1) construct(n-1,k/g);
	for(int i=n-1;i;i--){
		ll w=A[i][n];
		for(int j=i+1;j<n;j++) w-=B[i][j]*C[j][n];
		exgcd(B[i][i],C[n][n],C[i][n],B[i][n]);
		C[i][n]*=w;B[i][n]*=w;
	}
}
void Solve(){
	int i,j;scanf("%d",&n);
	clr(A);
	for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%lld",&A[i][j]);
	ll mul=Gauss(A);
	if(mul<=0){puts("No");return;}
	ll k=sqrtl(mul);
	if(k*k!=mul){puts("No");return;}
	puts("Yes");
	clr(D);for(i=1;i<=n;i++) for(j=1;j<=n;j++) D[i][j]=A[i][j+n];
	inve(D);
	clr(B);clr(C);
	construct(n,k);
	for(int i=1;i<=n;i++) for(j=1;j<=n;j++) cerr<<A[i][j]<<" \n"[j==n];cerr<<'\n';
	for(int i=1;i<=n;i++) for(j=1;j<=n;j++) cerr<<B[i][j]<<" \n"[j==n];cerr<<'\n';
	for(int i=1;i<=n;i++) for(j=1;j<=n;j++) cerr<<C[i][j]<<" \n"[j==n];cerr<<'\n';
	B=D*B;
	for(i=1;i<=n;i++) for(j=1;j<=n;j++) printf("%lld%c",B[i][j]," \n"[j==n]);
	for(i=1;i<=n;i++) for(j=1;j<=n;j++) printf("%lld%c",C[i][j]," \n"[j==n]);
	B=B*C;
	for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cerr<<B[i][j]<<" \n"[j==n];
}
int main(){
	int t=1;
	scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 4048kb

input:

3
2
2 0
0 2
2
2 1
1 2
1
1

output:

Yes
1 0
0 2
2 0
0 1
No
Yes
1
1

result:

ok OK, Accepted. (3 test cases)

Test #2:

score: 0
Accepted
time: 63ms
memory: 4036kb

input:

10000
3
10 0 1
0 0 4
2 10 0
3
2 -2 5
-6 9 0
2 1 10
4
-7 0 7 7
0 0 -10 0
0 0 -10 -10
8 -7 3 9
3
-3 3 3
5 -7 -9
-8 -6 -2
4
9 -8 4 -3
-3 -4 0 0
0 7 0 0
10 -4 7 0
4
3 -10 1 -3
5 5 -3 9
0 -5 -5 -10
3 0 4 -8
3
10 9 0
0 10 0
-5 -1 4
1
-10
4
0 7 0 0
0 -2 7 0
1 -2 7 5
5 -10 -1 0
4
3 1 4 0
-2 3 2 -4
-1 2 -10 ...

output:

No
No
Yes
-7 7 -154 7
0 0 -1 0
0 0 -1 -10
8 -7 154 9
1 -7 10 0
0 -7 231 0
0 0 10 0
0 0 0 1
No
No
No
Yes
-2 9 40
0 10 60
1 -1 0
-5 0 28
0 1 24
0 0 -4
No
No
No
Yes
0 9 18
0 5 9
1 -3 0
-3 0 -20
0 1 -6
0 0 3
No
Yes
-5 10 40
-7 15 60
-4 8 30
1 -4 -12
0 -2 14
0 0 -5
No
No
No
Yes
-1 7
0 -10
10 0
0 1
No
Yes...

result:

ok OK, Accepted. (10000 test cases)

Test #3:

score: 0
Accepted
time: 43ms
memory: 4028kb

input:

10000
3
0 -3 5
5 0 0
2 1 0
4
2 0 -2 0
-7 4 6 4
-2 8 -3 -10
3 -8 3 -7
4
-7 -4 5 2
5 0 0 4
0 0 0 7
-8 7 0 1
2
4 6
-8 -6
4
0 7 2 -3
0 0 0 -9
0 0 -9 0
7 8 9 2
4
-7 8 -10 -8
6 8 -1 6
8 -10 -7 3
2 7 7 7
3
0 0 -10
0 4 -2
-10 -2 5
3
-4 2 2
7 1 10
10 9 -1
2
9 -1
0 9
4
-7 2 -10 0
-5 1 -3 9
-10 -6 -3 3
8 -10 -...

output:

Yes
0 -3 -5
5 0 20
2 1 10
1 0 -20
0 1 -10
0 0 5
No
No
No
Yes
0 7 -6 -3
0 0 0 -9
0 0 -1 0
1 8 0 2
7 0 -55 0
0 1 8 0
0 0 9 0
0 0 0 1
No
No
No
Yes
1 -1
0 9
9 0
0 1
No
No
No
No
No
Yes
7 6 24
3 3 12
4 4 12
1 -6 12
0 6 -6
0 0 -2
No
Yes
0 0 0 1
3 0 105 4
-1 -4 0 -3
0 -7 63 1
3 0 -245 0
0 1 63 0
0 0 7 0
0 0...

result:

ok OK, Accepted. (10000 test cases)

Test #4:

score: 0
Accepted
time: 40ms
memory: 4056kb

input:

10000
3
10 3 10
0 10 0
9 7 0
4
-2 0 5 1
8 -8 -1 0
2 -9 -1 2
3 8 -5 -4
3
-7 0 7
6 7 -9
1 0 0
4
9 -10 1 -5
9 9 1 10
0 5 -8 -9
-8 -7 -6 -10
2
-10 -3
0 -10
4
5 2 4 2
4 -5 -7 8
-1 8 -5 -8
-9 6 0 6
3
9 0 0
1 0 3
6 3 2
2
8 -3
8 -5
4
6 0 0 6
0 -6 0 -6
0 0 0 10
8 -9 -10 -4
4
-9 -10 4 -1
0 -3 -4 -7
8 -3 -3 7
...

output:

No
No
No
No
Yes
-1 -3
0 -10
10 0
0 1
No
No
No
No
No
Yes
1 -7
0 3
3 0
0 1
No
Yes
1 8
0 10
10 0
0 1
No
Yes
3 7
0 1
3 0
0 1
No
No
No
Yes
8 8
7 8
1 -8
0 8
No
Yes
1 0 -2
0 0 5
-5 2 -2
-2 2 0
0 5 0
0 0 1
No
No
No
No
No
Yes
2 6
3 6
3 6
0 -2
No
No
No
Yes
0 0 -6 12
0 5 -9 114
1 -2 -9 0
0 10 5 180
-10 0 0 -11...

result:

ok OK, Accepted. (10000 test cases)

Test #5:

score: 0
Accepted
time: 60ms
memory: 4048kb

input:

10000
3
1 -1 0
0 4 0
1 3 9
3
5 6 6
10 -2 -5
10 -4 -7
3
-6 0 1
7 9 -8
0 0 -6
4
-10 4 9 -4
4 -2 3 -7
0 -8 0 -2
-1 -2 3 -4
4
0 0 0 -4
9 -10 0 7
1 0 0 3
-8 7 10 -5
4
-10 8 7 -1
5 4 7 10
-1 -7 0 1
-1 8 7 -4
3
7 0 0
10 7 -1
1 0 1
3
-5 0 -6
6 0 -1
1 7 1
3
-10 1 0
0 -8 -4
0 0 5
4
-9 4 -6 -5
-2 -10 2 4
4 7 -...

output:

Yes
1 -2 -9
0 2 6
1 0 0
1 3 9
0 2 -9
0 0 3
No
Yes
-6 -3 1
7 3 -8
0 0 -6
1 9 0
0 -18 0
0 0 1
No
Yes
0 0 0 -4
9 -10 -15 7
1 0 0 3
-8 7 10 -5
1 0 0 0
0 1 30 0
0 0 -20 0
0 0 0 1
No
Yes
7 0 0
10 -1 -1
1 0 1
1 0 0
0 -7 0
0 0 1
No
Yes
1 0 0
0 4 -4
0 0 5
-10 1 0
0 -2 0
0 0 1
No
Yes
4 -7 0
-3 7 10
5 -9 0
2 9...

result:

ok OK, Accepted. (10000 test cases)

Test #6:

score: 0
Accepted
time: 48ms
memory: 3972kb

input:

10000
3
6 0 0
-10 4 -7
8 0 6
2
8 -2
5 7
4
1 0 0 10
10 0 -10 -10
-6 -10 1 6
10 0 0 0
4
4 3 3 -3
-6 -8 -9 1
-9 -4 6 -6
9 0 -1 0
4
0 7 0 0
-2 7 -10 -1
-10 7 0 -1
0 2 0 7
4
2 -10 6 -2
5 -1 -7 3
3 8 -9 0
9 -6 5 -8
1
9
4
-5 7 -2 -2
-6 -2 -10 4
-1 -10 5 10
-1 -7 2 10
4
1 0 -3 -7
10 -10 4 -3
0 0 1 0
0 0 -3 ...

output:

Yes
3 6 33
-5 -12 -66
4 8 46
2 4 -7
0 -2 -13
0 0 3
No
Yes
1 0 0 10
10 0 -1 -10
-6 1 0 6
10 0 0 0
1 0 0 0
0 -10 1 0
0 0 10 0
0 0 0 1
No
Yes
0 7 0 21
1 7 0 0
5 7 10 -72
0 2 0 7
-2 0 -10 146
0 1 0 -21
0 0 5 -8
0 0 0 7
No
Yes
3
3
No
Yes
1 0 -3 -7
10 -1 4 -3
0 0 1 0
0 0 -3 -10
1 0 0 0
0 10 0 0
0 0 1 0
0 ...

result:

ok OK, Accepted. (10000 test cases)

Test #7:

score: 0
Accepted
time: 63ms
memory: 4044kb

input:

10000
3
7 0 0
10 4 5
8 0 7
1
6
3
0 1 0
7 -3 0
5 -8 7
3
-2 -10 2
9 -3 2
-10 5 5
3
1 0 0
0 -2 0
4 9 -8
4
-2 -2 -4 -7
-1 -8 -4 7
4 10 -2 8
-1 4 -6 2
3
3 4 5
0 -2 0
-10 -6 0
4
10 0 10 -6
1 -2 9 10
-6 -7 -3 9
-5 9 -6 6
4
3 7 3 1
0 0 3 0
0 -10 3 0
0 1 3 -10
4
-1 -7 -10 1
5 2 1 -3
1 -4 5 4
6 -2 6 6
4
0 0 0...

output:

Yes
7 28 -182
10 42 -273
8 32 -207
1 -8 -10
0 2 48
0 0 7
No
No
No
Yes
1 0 0
0 -2 4
4 9 -16
1 0 0
0 1 -8
0 0 -4
No
No
No
No
No
Yes
0 0 0 10
-1 2 0 -1
0 0 1 0
0 -7 8 -7
7 0 33 0
0 1 12 0
0 0 10 0
0 0 0 1
No
Yes
3
3
No
Yes
1 5
0 4
4 0
0 1
No
No
No
Yes
-3 10 -39 8
2 1 -4 -9
9 0 0 -7
1 0 0 0
1 0 0 0
0 1 ...

result:

ok OK, Accepted. (10000 test cases)

Test #8:

score: 0
Accepted
time: 79ms
memory: 4048kb

input:

10000
3
0 2 0
8 -7 -6
-3 -8 0
2
-2 9
1 6
4
-9 0 2 7
-2 0 0 0
-1 0 -8 0
-7 7 9 1
4
-8 -7 -9 2
9 7 -8 -8
2 2 0 10
9 9 -10 7
2
-9 0
-6 -9
3
6 7 9
2 7 -9
0 8 10
1
9
4
9 1 -10 -10
2 1 10 -1
-1 7 -1 -3
5 -7 10 8
3
0 0 10
-9 -4 9
-10 0 -4
4
-1 -3 2 -10
8 -7 4 4
1 -9 7 -1
-2 -1 7 -6
2
-6 9
9 0
3
-2 1 -2
-5 ...

output:

Yes
0 2 6
8 -7 -756
-3 -8 252
1 0 -552
0 1 18
0 0 -6
No
Yes
-9 0 37 -392
-2 0 8 -84
-1 0 0 0
-7 1 0 0
1 0 8 0
0 7 65 1
0 0 2 -21
0 0 0 -2
No
Yes
3 9
2 9
-3 9
0 -3
No
Yes
3
3
No
No
No
No
No
No
No
No
Yes
9 44 560
-7 -33 -420
0 2 28
1 20 -43
0 -4 98
0 0 -7
No
No
No
No
No
No
No
No
Yes
3 10
0 1
3 0
0 1
N...

result:

ok OK, Accepted. (10000 test cases)

Test #9:

score: 0
Accepted
time: 59ms
memory: 4008kb

input:

10000
3
9 -3 0
0 10 0
0 -7 10
4
-2 -5 -8 5
-1 -1 -9 -3
-3 1 -2 0
-10 1 -8 4
3
8 7 5
0 -10 0
0 2 -5
4
9 -5 -4 -8
0 -4 -9 -9
-9 -4 -10 2
4 9 9 -9
4
2 0 -10 -9
0 0 9 0
-9 0 9 0
10 1 0 5
4
5 -9 3 -1
9 -10 9 -6
1 5 -5 4
-9 -2 -9 10
1
4
4
1 10 -10 0
9 -6 -8 -6
-9 2 2 -4
9 -7 -7 3
4
0 -2 5 0
0 -4 -3 -8
10 ...

output:

Yes
-3 -3 -90
0 10 30
0 -7 -20
-3 0 -270
0 1 -30
0 0 10
No
Yes
-2 7 -65
0 -10 20
0 2 -5
-4 0 -130
0 1 10
0 0 5
No
No
No
Yes
2
2
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
0 3 8 -180
-1 2 -2 0
0 5 3 -360
0 0 5 30
10 0 0 -498
0 1 0 -228
0 0 1 18
0 0 0 -3
No
No
No
Yes
-1 10
0 -8
8 0
0 1
No
No
No
Yes
8 ...

result:

ok OK, Accepted. (10000 test cases)

Test #10:

score: 0
Accepted
time: 84ms
memory: 4088kb

input:

10000
3
8 -4 -6
-6 10 0
0 1 0
3
-6 3 -9
10 8 -1
-3 -8 0
4
0 0 -1 3
-4 8 -6 5
0 0 9 0
6 0 10 2
1
10
3
9 5 2
0 0 -10
-8 0 9
3
8 1 -9
-10 3 4
-9 3 -9
4
-8 -6 1 -4
-10 0 0 10
4 0 0 0
8 10 0 0
4
0 -5 -10 8
10 -6 -2 -1
-10 -7 -2 -10
-7 -6 -10 -10
4
0 0 -9 -2
-1 -3 4 -6
0 0 0 -3
-9 0 0 9
4
10 7 -8 -5
-10 2...

output:

Yes
4 -4 -6
-3 10 6
0 1 0
2 0 -6
0 1 0
0 0 -3
No
No
No
Yes
9 2 2
0 0 -10
-8 -2 9
1 5 0
0 -20 0
0 0 1
No
Yes
4 15 -5 -4
5 15 -5 10
-2 -6 2 0
-4 -17 6 0
-2 6 -1 0
0 -2 2 0
0 0 5 0
0 0 0 1
No
Yes
0 0 -9 -2
-1 0 4 -6
0 0 0 -3
-9 1 0 9
1 3 0 0
0 27 0 0
0 0 1 0
0 0 0 1
No
Yes
-1 10
0 -10
10 0
0 1
No
No
No...

result:

ok OK, Accepted. (10000 test cases)