QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#462004#8473. Matrices and Determinantsucup-team052#RE 0ms3788kbC++143.0kb2024-07-03 12:34:222024-07-03 12:34:22

Judging History

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

  • [2024-07-03 12:34:22]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3788kb
  • [2024-07-03 12:34: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;
	}
	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 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;
	}
	// (B*A*C).print();
	per(i,n-1,0){
		LL t=__gcd(val,A.a[i][i]);
		rep(j,i+1,n-1){
			if(A.a[i][j]%t!=0){
				A.add_row(j,i,-inv(A.a[j][j]%t,t)*(A.a[i][j]%t)%t);
				B.add_col(i,j,inv(A.a[j][j]%t,t)*(A.a[i][j]%t)%t);
			}
		}
		rep(j,0,n-1){
			assert(A.a[i][j]%t==0);
			A.a[i][j]/=t;
			B.a[j][i]*=t;
		}
		val/=t;
	}
	// (B*A*C).print();
	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;
	while(T--){
		solve();
	}
	return 0;
}

詳細信息

Test #1:

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

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: -100
Runtime Error

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 ...

result: