QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#625443 | #7612. Matrix Inverse | ucup-team3519# | TL | 0ms | 3644kb | C++17 | 5.4kb | 2024-10-09 19:20:50 | 2024-10-09 19:20:50 |
Judging History
answer
#include <bits/stdc++.h>
template <int m>
class ModInt {
int raw;
using mint = ModInt;
using i64 = int64_t;
public:
ModInt() : raw(0) {}
ModInt(const auto &v) {
raw = v % m;
if (raw < 0) {
raw += m;
}
}
int value() const {
return raw;
}
mint &operator+=(const mint &rhs) {
raw += rhs.raw;
if (raw >= m) {
raw -= m;
}
return *this;
}
mint &operator-=(const mint &rhs) {
raw -= rhs.raw;
if (raw < 0) {
raw += m;
}
return *this;
}
mint &operator*=(const mint &rhs) {
raw = (i64)raw * rhs.raw % m;
return *this;
}
mint &operator/=(const mint &rhs) {
return *this *= qpow(rhs, m - 2);
}
friend mint operator+(const mint &lhs, const mint &rhs) {
return mint{lhs} += rhs;
}
friend mint operator-(const mint &lhs, const mint &rhs) {
return mint{lhs} -= rhs;
}
friend mint operator*(const mint &lhs, const mint &rhs) {
return mint{lhs} *= rhs;
}
friend mint operator/(const mint &lhs, const mint &rhs) {
return mint{lhs} /= rhs;
}
static constexpr int mod() {
return m;
}
static mint qpow(mint a, i64 b) {
mint res = 1;
while (b) {
if (b & 1) {
res *= a;
}
a *= a, b >>= 1;
}
return res;
}
};
template <typename T>
int solve_linear_equations(std::vector<std::vector<T>> &a, auto abs, auto is_zero) {
const int n = a.size();
for (int i = 0; i < n; ++i) {
int p = i;
for (int j = i + 1; j < n; ++j) {
if (abs(a[j][i]) > abs(a[p][i])) {
p = j;
}
}
if (i != p) {
std::swap(a[i], a[p]);
}
if (is_zero(a[i][i])) {
return -1;
}
T f = 1 / a[i][i];
for (int k = i; k <= n; ++k) {
a[i][k] *= f;
}
for (int j = i + 1; j < n; ++j) {
T f = a[j][i];
for (int k = i; k <= n; ++k) {
a[j][k] -= a[i][k] * f;
}
}
}
for (int i = n - 1; i >= 0; --i) {
for (int j = i - 1; j >= 0; --j) {
a[j][n] -= a[j][i] * a[i][n];
a[j][i] = 0;
}
}
return 0;
}
using mint = ModInt<static_cast<int>(1e9 + 7)>;
constexpr int MAXN = 2e3 + 19;
int buf[MAXN][MAXN];
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
namespace fast
{
char B[1 << 18], *S = B, *T = B;
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 18, stdin), S == T) ? 0 : *S++)
int read() {
int ret = 0;
char c;
while (c = getc(), c < '0' || c > '9');
for (; c >= '0' && c <= '9'; c = getc()) ret = ret * 10 + c - '0';
return ret;
}
}
using fast::read;
int main() {
int n = read();
std::vector<std::vector<mint>> a(n), b(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
buf[i][j] = read();
}
}
for (int i = 0; i < n; ++i) {
a[i].resize(n);
for (int j = 0; j < n; ++j) {
a[i][j] = buf[i][j];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
buf[i][j] = read();
}
}
for (int i = 0; i < n; ++i) {
b[i].resize(n);
for (int j = 0; j < n; ++j) {
b[i][j] = buf[j][i];
}
}
std::vector<mint> fac(n);
std::vector<uint8_t> fault(n);
for (int _ = 0; _ < 3; ++_) {
for (int i = 0; i < n; ++i) {
fac[i] = rng();
}
std::vector<mint> e(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
e[j] += fac[i] * a[i][j];
}
}
for (int col = 0; col < n; ++col) {
mint expected = fac[col];
mint actual = 0;
for (int i = 0; i < n; ++i) {
actual += e[i] * b[col][i];
}
if (expected.value() != actual.value()) {
fault[col] = true;
}
}
}
std::vector<std::pair<std::pair<int, int>, int>> ans;
std::vector<std::vector<mint>> w(n, std::vector<mint>(n + 1));
for (int col = 0; col < n; ++col) {
if (!fault[col]) {
continue;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
w[i][j] = a[i][j];
}
w[i][n] = (i == col);
}
solve_linear_equations(w,
[](mint x) {
return x.value();
},
[](mint x) {
return x.value() == 0;
}
);
for (int i = 0; i < n; ++i) {
if (b[col][i].value() != w[i][n].value()) {
std::pair<int, int> pos = {i, col};
int v = w[i][n].value();
ans.emplace_back(pos, v);
}
}
}
std::sort(ans.begin(), ans.end());
std::cout << ans.size() << '\n';
for (auto [pos, v] : ans) {
std::cout << pos.first + 1 << ' ' << pos.second + 1 << ' ' << v << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3644kb
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...