QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#359057 | #7612. Matrix Inverse | zyz07 | WA | 547ms | 101688kb | C++17 | 2.4kb | 2024-03-20 11:40:54 | 2024-03-20 11:40:54 |
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(ok2[i]){
id[i]=++cnt;
}
}
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: 5908kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Wrong Answer
time: 547ms
memory: 101688kb
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...
output:
3990 1 238 0 1 499 0 2 238 0 2 499 0 3 238 0 3 499 0 4 238 0 4 499 0 5 238 0 5 499 0 6 238 0 6 499 0 7 238 0 7 499 0 8 238 0 8 499 0 9 238 0 9 499 0 10 238 0 10 499 0 11 238 0 11 499 0 12 238 0 12 499 0 13 238 0 13 499 0 14 238 0 14 499 0 15 238 0 15 499 0 16 238 0 16 499 0 17 238 0 17 499 0 18 238 ...
result:
wrong answer 1st lines differ - expected: '2', found: '3990'