QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#442047 | #8473. Matrices and Determinants | bulijiojiodibuliduo# | RE | 0ms | 3820kb | C++17 | 3.9kb | 2024-06-15 05:10:20 | 2024-06-15 05:10:21 |
Judging History
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) {
assert(x!=y);
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);
rep(j,i+1,n) {
if (A[j][i]<0) mulr(j,-1);
while (A[j][i]!=0) {
addr(i,j,-A[j][i]/A[i][i]);
swapr(i,j);
}
}
}
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) {
//printf("cnm %d %d\n",i,j);
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);
//rep(i,0,n) rep(j,0,n) printf("%lld%c",A[i][j]," \n"[j==n-1]);
//puts("!!!");
if (A[i][j]==0) break;
swapr(i,j);
swapc(i,j);
while (A[j][i]!=0) {
addr(i,j,-A[j][i]/A[i][i]);
swapr(i,j);
}
}
}
gauss();
break;
}
}
if (!alive) break;
//rep(i,0,n) rep(j,0,n) printf("%lld%c",A[i][j]," \n"[j==n-1]);
}
int z=sqrt(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]);
//check();
}
int _;
int main() {
for (scanf("%d",&_);_;_--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3820kb
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 ...