QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#449588#8473. Matrices and Determinants275307894aWA 13ms3992kbC++142.8kb2024-06-21 15:04:282024-06-21 15:04:29

Judging History

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

  • [2024-06-21 15:04:29]
  • 评测
  • 测评结果:WA
  • 用时:13ms
  • 内存:3992kb
  • [2024-06-21 15:04:28]
  • 提交

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){
	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++) 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]);
		}
	}
	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=1;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]);
	for(i=1;i<=n;i++) A[i][i+n]=1;
	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);
	C=C*D;
	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]);
}
int main(){
	int t=1;
	scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Wrong Answer
time: 13ms
memory: 3988kb

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

result:

wrong answer WA, B*C not equal to A 
4
-7 0 7 7
0 0 -10 0
0 0 -10 -10
8 -7 3 9 (test case 3)