QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#462013#8473. Matrices and Determinantsucup-team052#AC ✓29ms3912kbC++143.1kb2024-07-03 12:44:222024-07-03 12:44:23

Judging History

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

  • [2024-07-03 12:44:23]
  • 评测
  • 测评结果:AC
  • 用时:29ms
  • 内存:3912kb
  • [2024-07-03 12:44:22]
  • 提交

answer

#include<bits/stdc++.h>
#ifdef xay5421
#define D(...) fprintf(stderr,__VA_ARGS__)
#define DD(...) D(#__VA_ARGS__ "="),debug_helper::debug(__VA_ARGS__),D("\n")
#include"/home/xay5421/debug.hpp"
#else
#define D(...) ((void)0)
#define DD(...) ((void)0)
#endif
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define each(x,v) for(auto&x:v)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
template<class T>void rd(T&x){int f=0,c;while(!isdigit(c=getchar()))f^=!(c^45);x=(c&15);while(isdigit(c=getchar()))x=x*10+(c&15);if(f)x=-x;}
template<class T>void pt(T x,int c=-1){if(x<0)putchar('-'),x=-x;if(x>9)pt(x/10);putchar(x%10+48);if(c!=-1)putchar(c);}
using namespace std;
using LL=long long;
using ULL=unsigned long long;
int T,n;
struct mat{
	LL a[4][4];
	mat operator*(const mat&b)const{
		mat c;
		memset(c.a,0,sizeof(a));
		rep(i,0,n-1)rep(j,0,n-1)rep(k,0,n-1){
			c.a[i][j]+=a[i][k]*b.a[k][j];
		}
		return c;
	}
	bool operator==(const mat&rhs)const{
		rep(i,0,n-1)rep(j,0,n-1)if(a[i][j]!=rhs.a[i][j])return 0;
		return 1;
	}
	void add_row(int i,int j,LL k){
		rep(t,0,n-1){
			a[j][t]+=a[i][t]*k;
		}
	}
	void add_col(int i,int j,LL k){
		rep(t,0,n-1){
			a[t][j]+=a[t][i]*k;
		}
	}
	void swap_row(int i,int j){swap(a[i],a[j]);}
	void swap_col(int i,int j){rep(k,0,n-1)swap(a[k][i],a[k][j]);}
	void print(){
		rep(i,0,n-1){
			rep(j,0,n-1)D("%lld ",a[i][j]);
			D("\n");
		}
		D("\n");
	}
};
void exgcd(LL a,LL b,LL&x,LL&y){
	if(!b){x=1,y=0;return;}
	exgcd(b,a%b,y,x);
	y-=a/b*x;
}
LL inv(LL x,LL m){
	LL v0,v1;
	exgcd(x,m,v0,v1);
	v0=(v0%m+m)%m;
	assert(1LL*v0*x%m==1);
	return v0;
}
void solve(){
	cin>>n;
	mat A,I;
	memset(A.a,0,sizeof(A));
	memset(I.a,0,sizeof(I));
	rep(i,0,n-1){
		rep(j,0,n-1){
			cin>>A.a[i][j];
			I.a[i][j]=i==j;
		}
	}
	mat A0=A;
	mat B(I),C(I);
	// (B*A*C).print();
	LL det=1;
	rep(i,0,n-1){
		if(!A.a[i][i]){
			rep(j,i+1,n-1)if(A.a[j][i]){
				A.swap_row(i,j);
				B.swap_col(i,j);
				det=-det;
				break;
			}
			if(!A.a[i][i]){
				puts("No");
				return;
			}
		}
		rep(j,i+1,n-1){
			while(1){
				if(abs(A.a[i][i])<abs(A.a[j][i])){
					A.swap_row(i,j);
					B.swap_col(i,j);
					det=-det;
				}
				if(!A.a[j][i])break;
				LL t=A.a[i][i]/A.a[j][i];
				A.add_row(j,i,-t);
				B.add_col(i,j,t);
			}
		}
		det*=A.a[i][i];
	}
	if(det<=0){
		puts("No");
		return;
	}
	
	LL val=sqrt(det+0.5);
	if(val*val!=det){
		puts("No");
		return;
	}
	per(i,n-1,0){
		LL t=abs(__gcd(val,A.a[i][i]));
		rep(j,i+1,n-1){
			if(A.a[i][j]%t!=0){
				LL tmp=inv((A.a[j][j]%t+t)%t,t)*(A.a[i][j]%t)%t;
				A.add_row(j,i,-tmp);
				B.add_col(i,j,tmp);
			}
		}
		rep(j,0,n-1){
			assert(A.a[i][j]%t==0);
			A.a[i][j]/=t;
			B.a[j][i]*=t;
		}
		val/=t;
	}
	assert(A0==(B*A*C));
	puts("Yes");
	rep(i,0,n-1)rep(j,0,n-1)printf("%lld%c",B.a[i][j],j==n-1?'\n':' ');
	rep(i,0,n-1)rep(j,0,n-1)printf("%lld%c",A.a[i][j],j==n-1?'\n':' ');
}
int main(){
#ifdef xay5421
	freopen("a.in","r",stdin);
#endif
	cin>>T;
	rep(tc,1,T){
		solve();
	}
	return 0;
}

详细

Test #1:

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

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: 25ms
memory: 3856kb

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 0 0
0 0 1 0
0 0 1 10
8 -7 0 0
1 -7 10 16
0 -7 11 17
0 0 -10 0
0 0 0 -1
No
No
No
Yes
-2 7 40
0 10 60
1 0 0
-5 -1 4
0 1 24
0 0 -4
No
No
No
Yes
0 9 18
0 5 9
1 0 0
-3 -3 -2
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 0
0 10
-10 7
0 -1
No
Yes
1 0 0 0
0 ...

result:

ok OK, Accepted. (10000 test cases)

Test #3:

score: 0
Accepted
time: 18ms
memory: 3680kb

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
0 7 6 -4
0 0 0 9
0 0 1 0
1 0 0 0
7 8 9 2
0 1 8 -1
0 0 -9 0
0 0 0 -1
No
No
No
Yes
1 0
0 9
9 -1
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 12 105 25
1 0 0 0
0 7 63 15
-3 -4 -7 -3
0 -1 -63 -20
0 0 7 2...

result:

ok OK, Accepted. (10000 test cases)

Test #4:

score: 0
Accepted
time: 25ms
memory: 3736kb

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 0
0 10
-10 -3
0 -1
No
No
No
No
No
Yes
1 0
0 3
3 -7
0 1
No
Yes
1 0
0 10
10 8
0 1
No
Yes
3 1
0 1
3 2
0 1
No
No
No
Yes
8 8
7 8
1 -8
0 8
No
Yes
1 0 0
0 0 5
-5 2 0
-2 2 -2
0 5 -6
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 -1 -4
1 0 0 0
0 10 -25 40
-10 -2 -9 2
0...

result:

ok OK, Accepted. (10000 test cases)

Test #5:

score: 0
Accepted
time: 20ms
memory: 3860kb

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 -1
0 -2 4
1 0 0
1 3 9
0 -2 -6
0 0 -3
No
Yes
-6 3 -1
7 -3 1
0 0 6
1 9 -7
0 18 -14
0 0 -1
No
Yes
0 0 0 4
9 -10 15 0
1 0 0 0
-8 7 -10 0
1 0 0 3
0 1 30 17
0 0 20 10
0 0 0 -1
No
Yes
7 0 7
10 1 7
1 0 0
1 0 1
0 7 -4
0 0 -1
No
Yes
1 0 0
0 4 0
0 0 5
-10 1 0
0 -2 -1
0 0 1
No
Yes
4 -7 30
-3 7 -30
5 -9 ...

result:

ok OK, Accepted. (10000 test cases)

Test #6:

score: 0
Accepted
time: 12ms
memory: 3708kb

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 -3
-5 -12 6
4 8 -2
2 4 -7
0 -2 5
0 0 3
No
Yes
1 0 0 0
10 0 1 0
-6 1 0 0
10 0 0 100
1 0 0 10
0 -10 1 66
0 0 -10 -110
0 0 0 -1
No
Yes
0 7 0 21
1 0 0 0
5 -28 10 -82
0 2 0 7
-2 7 -10 -1
0 1 0 -21
0 0 5 -1
0 0 0 7
No
Yes
3
3
No
Yes
1 0 0 0
10 1 0 0
0 0 1 0
0 0 -3 10
1 0 -3 -7
0 -10 34 67
0 0 1 0
...

result:

ok OK, Accepted. (10000 test cases)

Test #7:

score: 0
Accepted
time: 25ms
memory: 3712kb

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
1 0 0 0
0 0 1 0
0 7 -6 0
-7 2 -9 -1
0 -1 8 -1
0 0 10 0
0 0 0 1
No
Yes
3
3
No
Yes
1 0
0 4
4 5
0 1
No
No
No
Yes
-3 10 39 119
2 1 4 14
9 0 0 7
1 0 0 0
1 0 0 0
0 1 -28 313...

result:

ok OK, Accepted. (10000 test cases)

Test #8:

score: 0
Accepted
time: 25ms
memory: 3808kb

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 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 1
0 1
3 3
0...

result:

ok OK, Accepted. (10000 test cases)

Test #9:

score: 0
Accepted
time: 29ms
memory: 3724kb

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 0 0
0 -10 30
0 7 -20
3 -1 0
0 -1 30
0 0 10
No
Yes
2 1 0
0 -10 10
0 2 -1
4 3 5
0 1 -5
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 31 -180
1 0 0 0
0 5 62 -360
0 0 -5 30
-10 2 -2 6
0 1 13 6
0 0 -1 -18
0 0 0 -3
No
No
No
Yes
1 0
0 8
-8 10
0 -1
No
No
No
Yes
-8 24
3 -8
-1 2...

result:

ok OK, Accepted. (10000 test cases)

Test #10:

score: 0
Accepted
time: 29ms
memory: 3912kb

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 -2 -1
0 0 10
-8 2 1
1 5 11
0 20 49
0 0 -1
No
Yes
4 15 -5 -40
5 15 -5 -40
-2 -6 2 20
-4 -17 6 0
-2 6 -1 14
0 -2 2 32
0 0 5 100
0 0 0 1
No
Yes
0 0 9 -7
1 0 0 0
0 0 0 3
9 1 0 0
-1 -3 4 -6
0 27 -36 63
0 0 -1 -1
0 0 0 -1
No
Yes
1 0
0 10
-10 10...

result:

ok OK, Accepted. (10000 test cases)