QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#426222 | #7612. Matrix Inverse | real_sigma_team# | WA | 482ms | 35080kb | C++20 | 4.0kb | 2024-05-30 23:12:48 | 2024-05-30 23:12:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct mint {
int x = 0;
mint() = default;
mint(int x_) : x(x_) {
if (x >= mod) x -= mod;
if (x >= mod) x %= mod;
}
int power(int b) {
int res = 1;
for (; b; b >>= 1, x = 1ll * x * x % mod) if (b & 1) res = 1ll * res * x % mod;
return res;
}
int inv() {
return power(mod - 2);
}
};
int abs(mint x) {
return x.x;
}
mint operator+(mint a, mint b) {
int ret = a.x + b.x;
if (ret >= mod) ret -= mod;
return mint(ret);
}
mint operator-(mint a, mint b) {
int ret = a.x - b.x;
if (ret < 0) ret += mod;
return mint(ret);
}
mint operator*(mint a, mint b) {
return mint(1ll * a.x * b.x % mod);
}
mint operator/(mint a, mint b) {
return mint(1ll * a.x * b.inv() % mod);
}
int gauss (vector < vector<mint> > a, vector<mint> & ans) {
int n = (int) a.size();
int m = (int) a[0].size() - 1;
vector<int> where (m, -1);
for (int col=0, row=0; col<m && row<n; ++col) {
int sel = row;
for (int i=row; i<n; ++i)
if (abs (a[i][col]) > abs (a[sel][col]))
sel = i;
if (abs (a[sel][col]) == 0)
continue;
for (int i=col; i<=m; ++i)
swap (a[sel][i], a[row][i]);
where[col] = row;
for (int i=0; i<n; ++i)
if (i != row) {
mint c = a[i][col] / a[row][col];
for (int j=col; j<=m; ++j)
a[i][j] = a[i][j] - a[row][j] * c;
}
++row;
}
ans.assign (m, 0);
for (int i=0; i<m; ++i)
if (where[i] != -1)
ans[i] = a[where[i]][m] / a[where[i]][i];
for (int i=0; i<n; ++i) {
mint sum = 0;
for (int j=0; j<m; ++j)
sum = sum + ans[j] * a[i][j];
if (abs (sum - a[i][m]) != 0)
return 0;
}
for (int i=0; i<m; ++i)
if (where[i] == -1)
return 1e9;
return 1;
}
mt19937 rng(time(0));
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<vector<mint>> a(n, vector<mint>(n));
auto c = a;
for (auto &i: a) {
for (auto &j: i) cin >> j.x;
}
for (auto &i: c) {
for (auto &j: i) cin >> j.x;
}
int ptr = 0;
vector<vector<mint>> sle;
vector<pair<int, int>> pos;
vector<mint> ret;
for (int j = 0; j < n; ++j) {
if (n <= 100) {
for (int i = 0; i < n; ++i) {
sle.emplace_back(ptr + n, mint(0));
for (int l = 0; l < n; ++l) {
if (pos.size() < ptr + n) pos.emplace_back(l, j);
sle.back()[ptr + l] = a[i][l];
}
ret.emplace_back(i == j);
}
ptr += n;
} else {
int i = rng() % n;
mint q(0);
for (int l = 0; l < n; ++l) {
q = q + a[i][l] * c[l][j];
}
if (q.x != (i == j)) {
sle.emplace_back(ptr + n, mint(0));
for (int l = 0; l < n; ++l) {
pos.emplace_back(l, j);
sle.back()[ptr + l] = a[i][l];
}
ptr += n;
ret.emplace_back(i == j);
}
}
}
for (auto &x: sle) {
while (x.size() < ptr) x.emplace_back(0);
}
for (int i = 0; i < sle.size(); ++i) {
sle[i].push_back(ret[i]);
}
vector<mint> ans;
gauss(sle, ans);
vector<tuple<int, int, mint>> res;
for (int i = 0; i < ans.size(); ++i) {
if (ans[i].x != c[pos[i].first][pos[i].second].x) {
res.emplace_back(pos[i].first, pos[i].second, ans[i]);
}
}
cout << res.size() << '\n';
for (auto [x, y, z]: res) cout << x + 1 << ' ' << y + 1 << ' ' << z.x << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3748kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Wrong Answer
time: 482ms
memory: 35080kb
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 2 238 0 3 238 0 4 238 0 5 238 0 6 238 0 7 238 0 8 238 0 9 238 0 10 238 0 11 238 0 12 238 0 13 238 0 14 238 0 15 238 0 16 238 0 17 238 0 18 238 0 19 238 0 20 238 0 21 238 0 22 238 0 23 238 0 24 238 0 25 238 0 26 238 0 27 238 0 28 238 0 29 238 0 30 238 0 31 238 0 32 238 0 33 238 0 34 238 ...
result:
wrong answer 1st lines differ - expected: '2', found: '3990'