QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#359081 | #7612. Matrix Inverse | zyz07 | WA | 650ms | 100820kb | C++17 | 2.4kb | 2024-03-20 12:20:48 | 2024-03-20 12:20:48 |
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;
}
}
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: 0ms
memory: 5904kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Wrong Answer
time: 650ms
memory: 100820kb
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 907977435 1 499 907977435 2 238 561173818 2 499 561173818 3 238 422161662 3 499 422161662 4 238 388954220 4 499 388954220 5 238 482434381 5 499 482434381 6 238 286426899 6 499 286426899 7 238 310282795 7 499 310282795 8 238 765899744 8 499 765899744 9 238 290399919 9 499 290399919 10 238 ...
result:
wrong answer 1st lines differ - expected: '2', found: '3990'