QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#449666#8473. Matrices and Determinants275307894aWA 48ms4020kbC++143.6kb2024-06-21 15:50:352024-06-21 15:50:35

Judging History

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

  • [2024-06-21 15:50:35]
  • 评测
  • 测评结果:WA
  • 用时:48ms
  • 内存:4020kb
  • [2024-06-21 15:50:35]
  • 提交

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=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]);
	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];
	// for(int i=1;i<=n;i++) for(j=1;j<=n;j++) cerr<<D[i][j]<<" \n"[j==n];cerr<<'\n';
	inve(D);
	// for(int i=1;i<=n;i++) for(j=1;j<=n;j++) cerr<<A[i][j]<<" \n"[j==n];cerr<<'\n';
	// A=A*D;
	// for(int i=1;i<=n;i++) for(j=1;j<=n;j++) cerr<<A[i][j]<<" \n"[j==n];cerr<<'\n';
	// return;
	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';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 48ms
memory: 4020kb

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

result:

wrong answer WA, B*C not equal to A 
3
-5 0 0
-7 -2 -6
-4 0 10 (test case 13)