QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#317632 | #7612. Matrix Inverse | karuna | WA | 483ms | 51700kb | C++20 | 3.5kb | 2024-01-29 10:47:27 | 2024-01-29 10:47:28 |
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(int n, int k) {
// (n, k) * (k, 1) = (n, 1)
for (int i = 0; i < k; i++) { // there are at most k pivots
int p = -1;
for (int j = i; j < n; j++) {
if (M[j][i]) p = j;
}
for (int j = 0; j <= k; j++) swap(M[i][j], M[p][j]);
mint inv = mpow(M[i][i], MOD - 2);
for (int j = 0; j <= k; j++) M[i][j] *= inv;
for (int j = 0; j < n; j++) if (i != j) {
if (M[j][i]) {
for (int l = k; l >= i; l--) M[j][l] -= M[j][i] * M[i][l];
}
}
}
}
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" rows and columns
vector<int> BR, BC;
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] += A[j][i] * x[j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
z[i] += C[j][i] * y[j];
}
}
for (int i = 0; i < n; i++) if (x[i] != z[i]) BC.push_back(i);
for (int i = 0; i < n; i++) y[i] = z[i] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
y[i] += A[i][j] * x[j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
z[i] += C[i][j] * y[j];
}
}
for (int i = 0; i < n; i++) if (x[i] != z[i]) BR.push_back(i);
vector<tuple<int, int, mint>> R;
for (int y : BC) {
for (int i = 0; i < n; i++) {
for (int j = 0; j <= BR.size(); j++) M[i][j] = 0;
}
M[y][BR.size()] = 1;
for (int i = 0; i < BR.size(); i++) {
int x = BR[i];
for (int j = 0; j < n; j++) {
M[j][i] = A[j][x];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
M[i][BR.size()] -= A[i][j] * C[j][y];
}
}
// cout << "before\n";
// for (int i = 0; i < n; i++) {
// for (int j = 0; j <= BR.size(); j++) {
// cout << M[i][j] << ' ';
// }
// cout << '\n';
// }
gaussian(n, BR.size());
// cout << "after\n";
// for (int i = 0; i < n; i++) {
// for (int j = 0; j <= BR.size(); j++) {
// cout << M[i][j] << ' ';
// }
// cout << '\n';
// }
for (int i = 0; i < BR.size(); i++) {
if (M[i][BR.size()] != 0) {
R.push_back({BR[i] + 1, y + 1, C[BR[i]][y] + M[i][BR.size()]});
}
}
}
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: 0ms
memory: 51700kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 479ms
memory: 51452kb
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 238 84815305 1466 499 206940592
result:
ok 3 lines
Test #3:
score: -100
Wrong Answer
time: 483ms
memory: 51544kb
input:
1995 436890614 28924575 276129332 63568266 576410175 399540058 591733285 531509939 637241038 596750662 811926780 760228238 317196903 751498201 993802643 102539089 382116597 233386377 974332817 495280100 575832855 616941506 297856263 216480938 638907269 434126707 499611855 764625526 51141033 64624519...
output:
3 315 590 222982023 745 1803 328256562 421 1912 523778307
result:
wrong answer 3rd lines differ - expected: '421 1912 523778307', found: '745 1803 328256562'