QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#442053#8473. Matrices and Determinantsbulijiojiodibuliduo#WA 1ms3912kbC++204.0kb2024-06-15 05:26:182024-06-15 05:26:18

Judging History

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

  • [2024-06-15 05:26:18]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3912kb
  • [2024-06-15 05:26:18]
  • 提交

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();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3912kb

input:

3
2
2 0
0 2
2
2 1
1 2
1
1

output:

2 0
0 2
Yes
1 0
0 2
2 0
0 1
No
1
Yes
1
1

result:

wrong answer Token "2" doesn't correspond to pattern "No|Yes" (test case 1)