QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#426232 | #7612. Matrix Inverse | real_sigma_team# | TL | 0ms | 3700kb | C++20 | 4.2kb | 2024-05-30 23:33:47 | 2024-05-30 23:33: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) {
if (a.empty()) return 1e9;
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) {
bool bad = false;
if (n <= 0) {
for (int i = 0; i < n; ++i) {
mint q(0);
for (int l = 0; l < n; ++l) {
q = q + a[i][l] * c[l][j];
}
if (q.x != (i == j)) {
bad = true;
}
}
} else {
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];
}
if (q.x != (i == j)) {
bad = true;
}
}
}
if (bad) {
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;
}
}
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';
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3700kb
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...