QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#426230 | #7612. Matrix Inverse | real_sigma_team# | RE | 0ms | 0kb | C++20 | 4.5kb | 2024-05-30 23:28:42 | 2024-05-30 23:28:42 |
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 <= 0) {
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 {
bool bad = false;
for (int iter = 0; iter < 10; ++iter) {
int i = rng() % n;
mint q(0);
for (int l = 0; l < n; ++l) {
q = q + a[i][l] * c[l][j];
}
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);
// cout << i << ' ' << j << ' ' << q.x << endl;
if (q.x != (i == j)) {
bad = true;
}
}
if (!bad) {
for (int i = 0; i < n; ++i) pos.pop_back();
for (int iter = 0; iter < 10; ++iter) sle.pop_back(), ret.pop_back();
}
ptr = pos.size();
}
}
// cout << ptr << endl;
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]);
}
// for (auto i : sle) {
// for (auto j : i) cout << j.x << ' ';
// cout << endl;
// }
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';
}
详细
Test #1:
score: 0
Runtime Error
input:
1 953176428 107682094