QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#318514 | #7612. Matrix Inverse | HaccerKat | WA | 1345ms | 43392kb | C++20 | 4.5kb | 2024-01-31 12:45:22 | 2024-01-31 12:45:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int mod = 1e9 + 7;
template <class T>
T qpow(T x, int p) {
T res = 1;
for (; p; p >>= 1, x *= x) {
if (p & 1) res *= x;
}
return res;
}
struct Mint {
int x;
Mint() : x(0) {}
Mint(ll x) : x((x % mod + mod) % mod) {}
Mint& operator += (const Mint &rhs) {
x += rhs.x;
if (x >= mod) x -= mod;
return *this;
}
Mint& operator -= (const Mint &rhs) {
x -= rhs.x;
if (x < 0) x += mod;
return *this;
}
Mint& operator *= (const Mint &rhs) {
x = (ll)x * rhs.x % mod;
return *this;
}
Mint& operator /= (const Mint &rhs) {
return (*this *= qpow(rhs, mod - 2));
}
};
Mint operator + (Mint lhs, const Mint &rhs) {return lhs += rhs;}
Mint operator - (Mint lhs, const Mint &rhs) {return lhs -= rhs;}
Mint operator * (Mint lhs, const Mint &rhs) {return lhs *= rhs;}
Mint operator / (Mint lhs, const Mint &rhs) {return lhs /= rhs;}
int gauss(vector<vector<Mint>> a, vector<Mint> &ans) {
int n = a.size(), m = a[0].size() - 1;
vector<int> where(m, -1);
for (int r = 0, c = 0; r < n && c < m; c++) {
int sel = r;
for (int i = r; i < n; i++) {
if (a[i][c].x != 0) sel = i;
}
if (a[sel][c].x == 0) continue;
swap(a[sel], a[r]);
where[c] = r;
for (int i = 0; i < n; i++) {
if (i != r) {
Mint mul = a[i][c] / a[r][c];
for (int j = c; j <= m; j++) {
a[i][j] -= mul * a[r][j];
}
}
}
r++;
}
ans = vector<Mint>(m);
bool free = false;
for (int i = 0; i < m; i++) {
if (where[i] != -1) {
ans[i] = a[where[i]][m] / a[where[i]][i];
}
else free = true;
}
for (int i = 0; i < n; i++) {
Mint sum = 0;
for (int j = 0; j < m; j++) {
sum += ans[j] * a[i][j];
}
if (sum.x != a[i][m].x) return 0;
}
return (free ? 2 : 1);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int N = 2005;
int n;
Mint A[N][N], C[N][N];
int usedc[N], usedr[N], I[N][N];
int temp;
void solve() {
cin >> n;
for (int i = 0; i < n; i++) {
I[i][i] = 1;
for (int j = 0; j < n; j++) {
cin >> temp;
A[i][j] = temp;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> temp;
C[i][j] = temp;
}
}
vector<int> rows, cols;
for (int iter = 0; iter < 20; iter++) {
int r = rnd(0, n - 1);
for (int i = 0; i < n; i++) {
Mint x = 0, y = 0;
for (int j = 0; j < n; j++) {
x += A[r][j] * C[j][i];
y += C[i][j] * A[j][r];
}
if (x.x != I[r][i] && !usedc[i]) {
usedc[i] = 1, cols.push_back(i);
}
if (y.x != I[i][r] && !usedr[i]) {
usedr[i] = 1, rows.push_back(i);
}
}
}
sort(rows.begin(), rows.end());
sort(cols.begin(), cols.end());
vector<array<int, 3>> out;
for (int y : cols) {
int sz = rows.size();
vector<vector<Mint>> M(n, vector<Mint>(sz + 1));
vector<Mint> ans(sz);
for (int i = 0; i < n; i++) {
int cur = 0;
for (int j = 0; j < n; j++) {
if (usedr[j]) {
M[i][cur++] = A[i][j];
}
else {
M[i][sz] -= A[i][j] * C[j][y];
}
}
M[i][sz] += I[i][y];
}
gauss(M, ans);
for (int i = 0; i < sz; i++) {
int x = rows[i];
if (C[x][y].x != ans[i].x) {
out.push_back({x, y, ans[i].x});
}
}
}
cout << out.size() << "\n";
for (auto [x, y, z] : out) {
cout << x + 1 << " " << y + 1 << " " << z << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 35044kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 1316ms
memory: 43184kb
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: 1345ms
memory: 43392kb
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'