QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#455904 | #7612. Matrix Inverse | ucup-team3924 | TL | 0ms | 3804kb | C++20 | 4.1kb | 2024-06-26 23:49:21 | 2024-06-26 23:49:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
using mat = vector<vector<int>>;
struct ModInt{
int x;
ModInt (int x = 0): x(x + (x < 0)*mod - (x >= mod) * mod){}
ModInt operator+(ModInt o) {return x + o.x; }
ModInt operator-(ModInt o) {return x - o.x; }
ModInt operator*(ModInt o) {return 1LL * x * o.x % mod;}
ModInt operator/(ModInt b) {return *this * b.inv();}
ModInt inv() {return pow(mod - 2);}
ModInt pow(long long e){
if(!e)return 1;
ModInt r = pow(e/2); r = r * r;
return e % 2 ? *this * r : r;
}
bool operator==(ModInt o){return x == o.x;}
bool operator>(ModInt o){return x > o.x;}
};
pair<vector<int>, int>RowEchelon(vector<vector<ModInt>>& A){
int n = A.size(), m = A[0].size(), sgn = 1;
vector<int>piv;
for(int i = 0, rnk = 0; i < m && rnk < n; ++i){
for(int j = rnk + 1; j < n; j++)
if(A[j][i]>A[rnk][i])
swap(A[j], A[rnk]), sgn = -sgn;
if(A[rnk][i] == 0)continue;
for(int j = 0; j < n; j++){
ModInt coef = A[j][i] / A[rnk][i];
if(j == rnk || coef == 0)continue;
for(int k = 0; k < m; k++){
A[j][k] = A[j][k] - coef * A[rnk][k];
}
}
piv.push_back(i);++rnk;
}
return {piv, sgn};
}
vector<ModInt>SolveLinear(vector<vector<ModInt>>& A){
int m = A[0].size() - 1;
auto piv = RowEchelon(A).first;
if(piv.size() > m)return {};
vector<ModInt>sol(m, 0);
for(int i = 0; i < (int)piv.size(); i++){
sol[piv[i]] = A[i][m] / A[i][i];
}
return sol;
}
vector<int>left_mult(vector<vector<int>>A, vector<int>b){
vector<int>res(b.size(), 0);
int n = b.size();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
res[i] = (res[i] + ((long long)A[i][j]*b[j])%mod)%mod;
}
}
return res;
}
vector<int>right_mult(vector<vector<int>>A, vector<int>b){
vector<int>res(b.size(), 0);
int n = b.size();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
res[i] = (res[i] + ((long long)A[j][i]*b[j])%mod)%mod;
}
}
return res;
}
vector<int>vecmin(vector<int>v, vector<int>u){
assert(v.size() == u.size());
vector<int>res(v.size());
for(int i = 0; i < (int)v.size(); i++){
res[i] = (v[i] - u[i])%mod;
}
return res;
}
vector<ModInt>solve(const mat &A , int col){
int n = A.size();
vector<int>b(n, 0);
b[col] = 1;
vector<vector<ModInt>>S(n, vector<ModInt>(n+1));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
S[i][j] = A[i][j];
}
}
for(int i = 0; i < n; i++){
S[i][n] = b[i];
}
vector<ModInt>res = SolveLinear(S);
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
mt19937 rng(124981);
int n;
cin >> n;
vector<vector<int>>A(n, vector<int>(n)), C(n, vector<int>(n));
for(auto &row : A){
for(auto &x: row)cin >> x;
}
for(auto &row : C){
for(auto &x: row)cin >> x;
}
vector<int>r(n);
vector<int>cols;
for(auto &x : r)x = rng()%mod;
// vector<int>prod = left_mult(C, left_mult(A, r));
// for(int i = 0; i < n; i++){
// if(prod[i] != r[i])rows.push_back(i);
// }
vector<int>prod = right_mult(C, right_mult(A, r));
for(int i = 0; i < n; i++){
if(prod[i] != r[i])cols.push_back(i);
}
if(cols.empty()){
cout << "0\n";
return 0;
}
vector<pair<pair<int,int>, int>>ans;
for(int col : cols){
auto res = solve(A, col);
for(int j = 0; j < n; j++){
if(C[j][col] != res[j].x){
ans.push_back({{j+1, col+1}, res[j].x});
}
}
}
sort(ans.begin(), ans.end(), [](pair<pair<int,int>, int>a, pair<pair<int,int>, int>b){
if(a.first.first == b.first.first)return a.first.second < b.first.second;
return a.first.first < b.first.first;
});
cout << ans.size() << '\n';
for(auto p : ans)cout << p.first.first << ' ' << p.first.second << ' ' << p.second << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3804kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Time Limit Exceeded
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...