QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#370979 | #7612. Matrix Inverse | ideograph_advantage | WA | 550ms | 66268kb | C++20 | 4.0kb | 2024-03-29 20:32:03 | 2024-03-29 20:32:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0);
#define iter(a) a.begin(), a.end()
#define pb emplace_back
#define ff first
#define ss second
#define SZ(a) int(a.size())
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef zisk
void debug(){cerr << "\n";}
template<class T, class ... U> void debug(T a, U ... b){cerr << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cerr << *l << " ", l++;
cerr << "\n";
}
#else
#define debug(...) void()
#define pary(...) void()
#endif
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.ff << ',' << p.ss << ')';
}
const ll MOD = 1000000007;
using matrix = vector<vector<ll>>;
matrix operator*(const matrix &a, const matrix &b){
matrix c(SZ(a), vector<ll>(SZ(b[0])));
for(int i = 0; i < SZ(a); i++)
for(int j = 0; j < SZ(b[0]); j++)
for(int k = 0; k < SZ(b); k++){
c[i][j] += a[i][k] * b[k][j] % MOD;
c[i][j] %= MOD;
}
return c;
}
ll inv(ll a){
ll b = MOD - 2;
ll ans = 1;
while(b){
if(b & 1) ans = ans * a % MOD;
b >>= 1;
a = a * a % MOD;
}
return ans;
}
void topos(ll &a){
a = (a % MOD + MOD) % MOD;
}
void debug_matrix(const matrix &mat){
debug("matrix");
for(auto &i : mat) pary(iter(i));
}
void gauss(matrix &mat){
//debug("gauss");
//debug_matrix(mat);
int n = SZ(mat);
for(int i = 0; i < n; i++){
for(int j = i; j < n; j++)
if(mat[j][i] != 0){
mat[i].swap(mat[j]);
break;
}
ll t = inv(mat[i][i]);
for(int j = i; j <= n; j++)
mat[i][j] = mat[i][j] * t % MOD;
for(int j = 0; j < n; j++){
if(i == j) continue;
t = mat[j][i];
for(int k = i; k <= n; k++){
mat[j][k] -= t * mat[i][k] % MOD;
topos(mat[j][k]);
}
}
//debug("ok row", i);
//debug_matrix(mat);
}
}
int main(){
StarBurstStream;
mt19937 rng(880301);
int n;
cin >> n;
matrix a(n, vector<ll>(n));
matrix c(n, vector<ll>(n));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> a[i][j];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> c[i][j];
matrix v(n, vector<ll>(1));
for(int i = 0; i < n; i++) v[i][0] = rng() % MOD;
matrix tmp = c * (a * v);
//debug_matrix(v);
//debug_matrix(tmp);
vector<int> bad_row, bad_col;
vector<bool> col(n);
for(int i = 0; i < n; i++){
if(tmp[i][0] != v[i][0]) bad_row.pb(i);
}
v = matrix(1, vector<ll>(n));
//debug_matrix(v);
//debug_matrix(tmp);
for(int i = 0; i < n; i++) v[0][i] = rng() % MOD;
tmp = v * a * c;
for(int i = 0; i < n; i++){
if(tmp[0][i] != v[0][i]) bad_col.pb(i), col[i] = true;
}
//pary(iter(bad_row));
//pary(iter(bad_col));
vector<pair<pii, ll>> sol;
for(int i : bad_row){
//debug("bad row", i);
vector<vector<ll>> mat;
for(int j = 0; j < SZ(bad_col); j++){
mat.pb();
ll sum = 0;
for(int k = 0; k < n; k++){
if(col[k]) mat.back().pb(a[k][j]);
else sum += c[i][k] * a[k][j];
}
mat.back().pb(-sum + (i == j));
//debug("ok", i, j, sum);
}
//debug_matrix(mat);
gauss(mat);
//debug_matrix(mat);
for(int j = 0; j < SZ(bad_col); j++){
if(mat[j].back() != c[i][bad_col[j]])
sol.pb(make_pair(pii(i + 1, bad_col[j] + 1), mat[j].back()));
}
}
cout << SZ(sol) << "\n";
for(auto [p, val] : sol) cout << p.ff << " " << p.ss << " " << val << "\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3564kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Wrong Answer
time: 550ms
memory: 66268kb
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:
4 827 238 548234094 827 499 92853578 1466 238 193910055 1466 499 34647985
result:
wrong answer 1st lines differ - expected: '2', found: '4'