QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404851 | #7612. Matrix Inverse | ucup-team3215# | TL | 0ms | 3528kb | C++20 | 3.8kb | 2024-05-04 20:29:41 | 2024-05-04 20:29:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int mod = 1e9 + 7;
auto& mul(auto&& a, auto... b) { return ((a = a * (uint64_t)b % mod), ...); }
auto& add(auto&& a, auto... b) { return ((a = (a + b) % mod), ...); }
int inv(int a) { for (int r = 1, b = mod - 2; ; b /= 2, mul(a, a)) if (!b) return r; else if (b % 2) mul(r, a); }
struct Matrix {
int n, m;
vector<int> b;
Matrix(int n, int m): n{n}, m{m}, b(n * m) { }
auto* operator[](int i) const { return b.data() + i * m; }
auto* operator[](int i) { return b.data() + i * m; }
};
Matrix prod(const Matrix& a, const Matrix& b) {
Matrix res(a.n, b.m);
for (int i = 0; i < a.n; ++i)
for (int j = 0; j < a.m; ++j)
for (int k = 0; k < b.m; ++k) {
res[i][k] = (res[i][k] + a[i][j] * (uint64_t)b[j][k]) % mod;
}
return res;
}
Matrix cut(const Matrix& a, int li, int ri, int lj, int rj) {
Matrix res(ri - li, rj - lj);
for (int i = li; i < ri; ++i)
for (int j = lj; j < rj; ++j) res[i - li][j - lj] = a[i][j];
return res;
}
Matrix trans(const Matrix& a) {
Matrix res(a.n, a.m);
for (int i = 0; i < a.n; ++i)
for (int j = 0; j < a.m; ++j) {
res[j][i] = a[i][j];
}
return res;
}
constexpr int K = 4;
mt19937_64 rng(214312421);
vector<int> badcol(const Matrix& a, const Matrix& b, int l, int r) {
vector<int> res;
if (r - l < 2 * K) {
Matrix chk = prod(a, cut(b, 0, b.n, l, r));
for (int j = l; j < r; ++j) {
bool ok = 1;
for (int i = 0; i < b.n; ++i) ok &= i == j == chk[i][j - l];
if (!ok) res.push_back(j);
}
return res;
}
Matrix u(r - l, K);
for (auto& x: u.b) x = rng() % mod;
Matrix proj = prod(a, prod(cut(b, 0, b.n, l, r), u));
bool ok = 1;
for (int i = 0; i < a.n; ++i)
for (int j = 0; j < K; ++j) {
ok &= i < l || i >= r? !proj[i][j - l]: proj[i][j] == u[i - l][j];
}
if (ok) return res;
res = badcol(a, b, l, (l + r) / 2);
auto t = badcol(a, b, (l + r) / 2, r);
res.insert(res.end(), t.begin(), t.end());
return res;
}
vector<int> gelim(Matrix a) {
for (int i = 0; i < a.m - 1; ++i) {
int piv = i;
while (!a[piv][i]) ++piv;
swap_ranges(a[piv], a[piv + 1], a[i]);
if (int c = inv(a[i][i]); 1)
for (int j = 0; j < a.m; ++j) mul(a[i][j], c);
for (int j = i + 1; j < a.n; ++j) if (uint64_t c = mod - a[j][i]; 1)
for (int k = i + 1; k < a.m; ++k) a[j][k] = (a[j][k] + a[i][k] * c) % mod;
}
vector<int> ans(a.m - 1);
for (int i = a.m - 1; i--; ) {
int s = 0;
for (int j = i + 1; j < a.m - 1; ++j) s = (s + a[i][j] * (uint64_t)ans[j]) % mod;
s = (mod + 0ull + a[i][a.m - 1] - s) % mod;
ans[i] = s;
}
return ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
Matrix a(n, n), b(n, n);
for (auto& x: a.b) cin >> x;
for (auto& x: b.b) cin >> x;
auto bc = badcol(a, b, 0, n), br = badcol(trans(a), trans(b), 0, n);
Matrix sys(n * bc.size(), br.size() * bc.size() + 1);
for (int i = 0; i < n; ++i)
for (int j = 0; j < bc.size(); ++j) {
int s = 0;
for (int k = 0; k < n; ++k) s = (s + a[i][k] * (uint64_t)b[k][bc[j]]) % mod;
s = (mod - s) % mod;
for (int k = 0; k < br.size(); ++k) {
sys[i * bc.size() + j][k * bc.size() + j] = a[i][br[k]];
s = (s + a[i][br[k]] * (uint64_t)b[br[k]][bc[j]]) % mod;
}
sys[i * bc.size() + j][sys.m - 1] = add(s, i == bc[j]);
}
auto correct = gelim(sys);
vector<array<int, 2>> ans;
for (int i = 0; i < br.size(); ++i)
for (int j = 0; j < bc.size(); ++j) if (correct[i * bc.size() + j] != b[br[i]][bc[j]]) ans.push_back({br[i], br[j]}), b[br[i]][bc[j]] = correct[i * bc.size() + j];
cout << ans.size() << '\n';
for (auto [x, y]: ans) cout << x + 1 << ' ' << y + 1 << ' ' << b[x][y] << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3528kb
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...