QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#317335 | #7612. Matrix Inverse | karuna# | TL | 3ms | 51736kb | C++20 | 2.7kb | 2024-01-28 20:59:19 | 2024-01-28 20:59:20 |
Judging History
answer
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MOD = 1e9 + 7;
struct mint {
int x;
mint() : x(0) {}
mint(int x) : x(x) {}
mint operator+(int ot) const { return x + ot >= MOD ? x + ot - MOD : x + ot; }
mint operator-(int ot) const { return x - ot < 0 ? x - ot + MOD : x - ot; }
mint operator*(int ot) const { return 1ll * x * ot % MOD; }
mint operator+=(int ot) { return *this = *this + ot; }
mint operator-=(int ot) { return *this = *this - ot; }
mint operator*=(int ot) { return *this = *this * ot; }
operator int() const { return x; }
};
mint mpow(mint a, ll x) {
mint r = 1;
while (x) {
if (x & 1) r *= a;
a *= a;
x /= 2;
}
return r;
}
mt19937 rnd(1557);
int rng() {
return uniform_int_distribution<int>(0, MOD - 1)(rnd);
}
const int MAXN = 2020;
int n; mint A[MAXN][MAXN], C[MAXN][MAXN];
mint x[MAXN], y[MAXN], z[MAXN];
mint M[MAXN][MAXN], V[MAXN];
void gaussian() {
// stores M^{-1} V in V
// M is guaranteed to have an inverse matrix
for (int i = 0; i < n; i++) M[i][n] = V[i];
for (int i = 0; i < n; i++) {
int p = -1;
for (int j = i; j < n; j++) {
if (M[j][i]) p = j;
}
for (int j = 0; j <= n; j++) swap(M[i][j], M[p][j]);
mint inv = mpow(M[i][i], MOD - 2);
for (int j = 0; j <= n; j++) M[i][j] *= inv;
for (int j = 0; j < n; j++) if (i != j) {
if (M[j][i]) {
for (int k = n; k >= i; k--) M[j][k] -= M[j][i] * M[i][k];
}
}
}
for (int i = 0; i < n; i++) V[i] = M[i][n];
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int x; cin >> x;
A[i][j] = x;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int x; cin >> x;
C[i][j] = x;
}
}
// find 12 "bad" columns
for (int i = 0; i < n; i++) x[i] = rng();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
y[i] += C[j][i] * x[j];
}
}
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) M[i][j] = A[j][i];
for (int i = 0; i < n; i++) V[i] = x[i];
gaussian();
vector<int> B; // bad columns
for (int i = 0; i < n; i++) if (V[i] != y[i]) B.push_back(i);
vector<tuple<int, int, mint>> R;
for (int c : B) {
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) M[i][j] = A[i][j];
for (int i = 0; i < n; i++) V[i] = 0;
V[c] = 1;
gaussian();
for (int i = 0; i < n; i++) if (C[i][c] != V[i]) {
R.push_back({i + 1, c + 1, V[i]});
}
}
cout << R.size() << '\n';
for (auto [x, y, z] : R) {
cout << x << ' ' << y << ' ' << z << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 51736kb
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...