QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#359082 | #7612. Matrix Inverse | zyz07 | RE | 1ms | 3896kb | C++17 | 2.4kb | 2024-03-20 12:24:20 | 2024-03-20 12:24:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define For(Ti,Ta,Tb) for(auto Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(auto Ti=(Ta);Ti>=(Tb);--Ti)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
using ll=long long;
mt19937 eng(random_device{}());
const int N=3005,Mod=1e9+7;
void reduce(ll& x){
x=(x<0?x+Mod:x);
}
ll power(ll x,ll y){
ll r=1;
for(;y;y>>=1,x=x*x%Mod){
if(y&1) r=r*x%Mod;
}
return r;
}
using Mat=ll[N][N];
int n,ok1[N],ok2[N],id[N];
void rmul(Mat& A,ll* v){
static ll v_[N];
memset(v_,0,sizeof(v_));
For(i,1,n){
For(j,1,n){
(v_[i]+=A[i][j]*v[j])%=Mod;
}
}
memcpy(v,v_,sizeof(v_));
}
void mul(ll* v,Mat& A){
static ll v_[N];
memset(v_,0,sizeof(v_));
For(i,1,n){
For(j,1,n){
(v_[j]+=v[i]*A[i][j])%=Mod;
}
}
memcpy(v,v_,sizeof(v_));
}
Mat A,C;
ll v1[N],v2[N],M[N][15];
void gauss(int R,int C){
for(int r=1,c=1;r<=R&&c<=C;){
if(!M[r][c]){
For(i,r+1,R){
if(M[i][c]){
swap(M[r],M[i]);
break;
}
}
}
if(!M[r][c]){
++c;
continue;
}
ll inv=power(M[r][c],Mod-2);
For(i,c,C+1){
(M[r][i]*=inv)%=Mod;
}
For(i,r+1,R){
Dec(j,C+1,c){
reduce(M[i][j]-=M[i][c]*M[r][j]%Mod);
}
}
++r;
}
Dec(i,C,1){
For(j,i+1,C){
reduce(M[i][C+1]-=M[j][C+1]*M[i][j]%Mod);
}
}
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n;
For(i,1,n){
For(j,1,n){
cin>>A[i][j];
}
}
For(i,1,n){
For(j,1,n){
cin>>C[i][j];
}
}
For($,1,2){
For(i,1,n){
v2[i]=v1[i]=eng()%Mod;
}
rmul(C,v1);
rmul(A,v1);
For(i,1,n){
ok1[i]|=(v1[i]!=v2[i]);
}
}
For($,1,2){
For(i,1,n){
v2[i]=v1[i]=eng()%Mod;
}
mul(v1,A);
mul(v1,C);
For(i,1,n){
ok2[i]|=(v1[i]!=v2[i]);
}
}
int cnt=0;
For(i,1,n){
if(ok1[i]){
id[i]=++cnt;
}
}
assert(cnt<=12);
vector<tuple<int,int,ll>> ans;
For(j,1,n){
if(!ok2[j]) continue;
memset(M,0,sizeof(M));
For(i,1,n){
ll val=(i==j);
For(k,1,n){
if(!ok1[k]){
(val-=A[i][k]*C[k][j])%=Mod;
}else{
M[i][id[k]]=A[i][k];
}
}
M[i][cnt+1]=(val%Mod+Mod)%Mod;
}
gauss(n,cnt);
For(i,1,n){
if(ok1[i]){
ll val=M[id[i]][cnt+1];
if(val!=C[i][j]){
ans.emplace_back(i,j,val);
}
}
}
}
sort(range(ans));
cout<<ans.size()<<'\n';
for(auto [i,j,x]:ans){
cout<<i<<' '<<j<<' '<<x<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3896kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Runtime Error
input:
1995 586309310 548144807 578573993 437893403 641164340 712256053 172321263 108058526 768610920 123320669 762746291 856047593 979279376 29067913 309867338 292286426 45124325 239705174 675003623 213743652 620561338 116308277 695369179 669459894 682522334 846995555 159510341 999359657 645579085 7499563...