QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#442054#8473. Matrices and Determinantsbulijiojiodibuliduo#AC ✓14ms3976kbC++204.0kb2024-06-15 05:27:042024-06-15 05:27:05

Judging History

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

  • [2024-06-15 05:27:05]
  • 评测
  • 测评结果:AC
  • 用时:14ms
  • 内存:3976kb
  • [2024-06-15 05:27:04]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(3); 
const ll mod=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=5;
ll A[N][N],pA[N][N],L[N][N],R[N][N];
int p[11],n;
void solve() {
	scanf("%d",&n);
	//n=4;
	rep(i,0,n) rep(j,0,n) {
		scanf("%lld",&A[i][j]);
		//A[i][j]=rnd(21)-10;
		pA[i][j]=A[i][j];
	}
	rep(i,0,n) p[i]=i;
	ll d=0;
	while (1) {
		ll s=1,sg=0;
		rep(i,0,n) s=s*A[i][p[i]];
		rep(i,0,n) rep(j,i+1,n) sg+=p[i]>p[j];
		if (sg%2==1) d-=s; else d+=s;
		if (!next_permutation(p,p+n)) break;
	}
	auto isq=[&](int x) {
		int y=sqrt(x)+0.1;
		return y*y==x;
	};
	if (d==0) {
		puts("No");
		return;
	}
	if (d<=0||!isq(d)) {
		puts("No");
		return;
	}
	rep(i,0,n) rep(j,0,n) R[i][j]=(i==j),L[i][j]=(i==j);
	auto check=[&]() {
		rep(x1,0,n) rep(x2,0,n) {
			ll s=0;
			rep(x3,0,n) rep(x4,0,n) s+=L[x1][x3]*A[x3][x4]*R[x4][x2];
			//printf("cnm %d %d %lld\n",x1,x2,s);
			assert(s==pA[x1][x2]);
		}
	};
	auto swapr=[&](int x,int y) {
		rep(i,0,n) swap(A[x][i],A[y][i]),swap(L[i][x],L[i][y]);
		// puts("SWAPR"); check();
	};
	auto swapc=[&](int x,int y) {
		rep(i,0,n) swap(R[x][i],R[y][i]),swap(A[i][x],A[i][y]);
		// puts("SWAPC"); check();
	};
	auto addr=[&](int x,int y,int d) {
		rep(i,0,n) {
			A[y][i]+=A[x][i]*d;
			L[i][x]-=L[i][y]*d;
		}
		// puts("ADDR"); check();
	};
	auto addc=[&](int x,int y,int d) {
		rep(i,0,n) {
			A[i][y]+=A[i][x]*d;
			R[x][i]-=R[y][i]*d;
		}
		// puts("ADDC"); check();
	};
	auto mulr=[&](int x,int d) {
		rep(i,0,n) A[x][i]*=d,L[i][x]/=d;
		// puts("MULR"); check();
	};
	auto divr=[&](int x,int d) {
		rep(i,0,n) A[x][i]/=d,L[i][x]*=d;
		// puts("DIVR"); check();
	};
	auto divc=[&](int x,int d) {
		rep(i,0,n) A[i][x]/=d,R[x][i]*=d;
		//puts("DIVC"); check();
	};
	auto floordiv=[&](ll a,ll b) {
		if (a>=0||a%b==0) return a/b;
		else return a/b-1;
	};
	auto gauss=[&]{
		rep(i,0,n) {
			rep(j,i,n) if (A[j][i]!=0) {
				swapr(j,i);
				break;
			}
			if (A[i][i]<0) mulr(i,-1);
			assert(A[i][i]!=0);
			rep(j,i+1,n) {
				if (A[j][i]<0) mulr(j,-1);
				//assert(A[i][i]!=0);
				while (1) {
					addr(i,j,-A[j][i]/A[i][i]);
					if (A[j][i]==0) break;
					swapr(j,i);
				}
			}
		}
		rep(i,1,n) {
			rep(j,0,i) addr(j,i,-floordiv(A[i][j],A[j][j]));
		}
	};
	gauss();
	//rep(i,0,n) rep(j,0,n) printf("%lld%c",A[i][j]," \n"[j==n-1]);
	//rep(i,0,n) rep(j,0,n) if (j<i) assert(A[i][j]==0);
	while (1) {
		bool alive=0;
		rep(i,0,n) {
			bool hv=0;
			rep(j,i+1,n) hv|=A[i][j]!=0;
			if (hv) {
				alive=1;
				rep(j,i+1,n) {
					while (A[i][j]!=0) {
						ll d=floordiv(A[i][j],A[i][i]);
						addc(i,j,-d);
						d=floordiv(A[i][j],A[j][j]);
						addr(j,i,-d);
						if (A[i][j]==0) break;
						swapr(i,j);
						swapc(i,j);
						while (1) {
							addr(i,j,-A[j][i]/A[i][i]);
							if (A[j][i]==0) break;
							swapr(j,i);
						}
					}
					gauss();
				}
				break;
			}
		}
		if (!alive) break;
		//rep(i,0,n) rep(j,0,n) printf("%lld%c",A[i][j]," \n"[j==n-1]);
	}
	//rep(i,0,n) rep(j,0,n) printf("%lld%c",A[i][j]," \n"[j==n-1]);
	int z=sqrt(abs(d))+0.1;
	rep(i,0,n) {
		ll o=gcd(z,A[i][i]);
		z/=o;
		divc(i,o);
		divr(i,A[i][i]);
	}
	puts("Yes");
	rep(i,0,n) rep(j,0,n) printf("%lld%c",L[i][j]," \n"[j==n-1]);
	rep(i,0,n) rep(j,0,n) printf("%lld%c",R[i][j]," \n"[j==n-1]);
	//rep(i,0,n) rep(j,0,n) printf("%lld%c",A[i][j]," \n"[j==n-1]);
	//check();
}

int _;
int main() {
	for (scanf("%d",&_);_;_--) {
		solve();
	}
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3976kb

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: 14ms
memory: 3908kb

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 -21 14 140
0 -10 7 80
0 -10 7 70
8 21 -14 -140
1 -7 10 16
0 49 -97 -139
0 70 -140 -210
0 0 0 1
No
No
No
Yes
-110 9 -40
0 10 -60
47 -1 0
5 0 -28
240 1 -1320
40 0 -220
No
No
No
Yes
0 9 234
-3 5 117
-2 0 -9
-39 -39 1
-234 -233 0
9 9 0
No
Yes
-5 -10 -40
-7 -15 -60
-4 -8 -30
1 -4 -12
0 2 -14...

result:

ok OK, Accepted. (10000 test cases)

Test #3:

score: 0
Accepted
time: 14ms
memory: 3772kb

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 10 20
2 5 10
1 -2 0
0 1 -10
0 0 5
No
No
No
Yes
7 143 -7 -243
0 0 0 -9
0 144 -7 -243
1 0 0 0
7 8 9 2
49 49 52 17
1008 1008 1071 315
0 0 0 1
No
No
No
Yes
8 9
9 9
-9 10
9 -9
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
171 -9 -105 -500
-64 -1 0 0
0 -7 -63...

result:

ok OK, Accepted. (10000 test cases)

Test #4:

score: 0
Accepted
time: 14ms
memory: 3812kb

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
-3 -10
-10 -30
-30 1
10 0
No
No
No
No
No
Yes
2 3
3 3
-3 10
3 -9
No
Yes
4 10
5 10
-10 2
5 0
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 0
0 5 -20
5 8 -30
2 -2 2
0 20 -39
0 5 -10
No
No
No
No
No
Yes
2 -6
3 -6
3 6
0 2
No
No
No
Yes
0 0 -6 780
5 178 26 480
-2 -89 -13 -...

result:

ok OK, Accepted. (10000 test cases)

Test #5:

score: 0
Accepted
time: 14ms
memory: 3900kb

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 0 0
0 2 0
1 2 3
1 -1 0
0 2 0
0 0 3
No
Yes
-6 13 -36
7 -13 36
0 -6 18
1 9 -7
0 54 -53
0 18 -18
No
Yes
0 0 -2 20
9 -10 75 -720
1 0 0 0
-8 7 -50 480
1 0 0 3
0 1 30 17
0 0 100 2
0 0 10 0
No
Yes
7 84 182
10 126 273
1 13 28
1 -14 2
0 -14 15
0 7 -7
No
Yes
-9 44 180
-8 44 180
0 5 20
10 -9 -4
-80 80 81...

result:

ok OK, Accepted. (10000 test cases)

Test #6:

score: 0
Accepted
time: 14ms
memory: 3768kb

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
24 15 -12
-47 -30 24
38 14 -12
2 4 -7
26 56 -104
36 78 -144
No
Yes
1 0 0 0
10 -10 -1 0
-6 -9 -1 0
10 0 -2 100
1 0 0 10
0 -10 11 176
0 100 -100 -1650
0 2 -2 -34
No
Yes
0 7 147 2520
-1 -1 0 0
-5 -33 -584 -10010
0 2 49 840
2 -8 10 22
0 1 0 -21
0 0 -600 1
0 0 35 0
No
Yes
3
3
No
Yes
1 0 0 0
10 -3 -6 ...

result:

ok OK, Accepted. (10000 test cases)

Test #7:

score: 0
Accepted
time: 14ms
memory: 3768kb

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 42
10 42 63
8 32 49
1 -8 -10
0 2 -8
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
-5 86 182 5460
0 10 21 630
-7 129 273 8190
21 -20 19 -11
-147 147 1 84
70 -70 0 -70
0 0 0 1
No
Yes
3
3
No
Yes
1 0
4 -4
4 5
4 4
No
No
No
Yes
-3 -23 -90 280
2 23 90 -280
...

result:

ok OK, Accepted. (10000 test cases)

Test #8:

score: 0
Accepted
time: 13ms
memory: 3952kb

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 -255 756
-3 85 -252
1 31 6
0 1 18
0 0 6
No
Yes
-9 -56 1560 -112
-2 -14 390 -28
-1 -7 779 -56
-7 -20 1 0
1 0 -2 -7
0 7 -7 -27
0 147 -145 -588
0 2044 -2016 -8176
No
Yes
-3 -9
-2 -9
3 -9
0 3
No
Yes
3
3
No
No
No
No
No
No
No
No
Yes
9 -2004 560
-7 1503 -420
0 -100 28
1 20 -43
0 4 -98
0 14 -35...

result:

ok OK, Accepted. (10000 test cases)

Test #9:

score: 0
Accepted
time: 14ms
memory: 3960kb

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 6 0
0 10 30
0 -7 -20
3 -3 60
0 1 -30
0 0 10
No
Yes
7 -39 60
-10 40 -60
2 -13 20
8 1 20
32 0 65
20 0 40
No
No
No
Yes
2
2
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
0 3 -31 -180
-97 -8 -8 0
-3 5 -62 -360
0 0 5 30
10 -10 -110 -198
-30 31 343 600
-90 90 991 1800
15 -15 -165 -300
No
No
No
Yes
-3 -8...

result:

ok OK, Accepted. (10000 test cases)

Test #10:

score: 0
Accepted
time: 14ms
memory: 3952kb

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 -28 6
-3 28 -6
0 1 0
2 6 -6
0 1 0
0 0 3
No
No
No
Yes
9 -17 100
0 -10 60
-8 17 -100
1 5 11
0 120 241
0 20 40
No
Yes
1 -15 5 -40
0 -15 5 -40
0 6 -2 20
0 17 -6 0
2 -6 1 -14
4 -10 0 -60
10 -30 0 -170
0 0 0 1
No
Yes
0 -2 -1 0
-1 0 0 0
0 -3 0 -27
-9 9 2 54
1 3 -4 6
0 -27 54 -53
0 54 -99 108
0 3 -6 6...

result:

ok OK, Accepted. (10000 test cases)