QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404876 | #7612. Matrix Inverse | ucup-team3215# | WA | 584ms | 96632kb | C++20 | 4.3kb | 2024-05-04 21:06:03 | 2024-05-04 21:06:03 |
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; }
bool operator==(const Matrix&& oth) { return b == oth.b; }
};
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)
#pragma GCC unroll 4
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.m, a.n);
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 = 1;
mt19937_64 rng(21431243);
vector<int> badcol(const Matrix& a, const Matrix& b, const Matrix& eye, int l, int r) {
vector<int> res;
// if (r - l < 2 * K) {
Matrix chk = prod(a, cut(b, 0, b.n, l, r)), targ = cut(eye, 0, a.n, l, r);
for (int j = l; j < r; ++j) {
bool ok = 1;
for (int i = 0; i < a.n; ++i) ok &= chk[i][j - l] == targ[i][j - l];
if (!ok) res.push_back(j);
}
return res;
// }
// Matrix u(r - l, K);
// for (auto& x: u.b) x = rng() % mod;
// if (prod(a, prod(cut(b, 0, b.n, l, r), u)) == prod(cut(eye, 0, a.n, l, r), u)) return res;
// res = badcol(a, b, eye, l, (l + r) / 2);
// auto t = badcol(a, b, eye, (l + r) / 2, r);
// res.insert(res.end(), t.begin(), t.end());
// return res;
}
vector<int> badcol(const Matrix& a, const Matrix& b) {
Matrix eye(a.n, a.n);
for (int i = 0; i < a.n; ++i) eye[i][i] = 1;
Matrix u(4, a.n);
for (auto& x: u.b) x = rng() % mod;
return badcol(prod(u, a), b, prod(u, eye), 0, b.m);
}
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 = 2e3; cin >> n;
Matrix a(n, n), b(n, n);
// for (int i = 0; i < n; ++i) a[i][i] = 1;
// for (int i = 0; i < n; ++i) b[i][i] = 1;
// for (int i = 0; i < n; i += n / 12) b[i][i] = 0;
for (auto& x: a.b) cin >> x;
for (auto& x: b.b) cin >> x;
auto bc = badcol(a, b), br = badcol(trans(a), trans(b));
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';
// auto p = prod(a, b);
// for (int i = 0; i < n; ++i, cout << '\n')
// for (int j = 0; j < n; ++j) cout << p[i][j];
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3504kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Wrong Answer
time: 584ms
memory: 96632kb
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:
2 827 827 994811372 1466 1466 761111752
result:
wrong answer 2nd lines differ - expected: '827 238 84815305', found: '827 827 994811372'