QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#318522 | #7612. Matrix Inverse | HaccerKat | TL | 4ms | 35072kb | C++20 | 4.9kb | 2024-01-31 13:05:35 | 2024-01-31 13:05:36 |
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], a[N], b[N];
int usedc[N], usedr[N], I[N][N], on[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 < 15; iter++) {
for (int i = 0; i < n; i++) {
a[i] = b[i] = 0;
}
for (int i = 0; i < n; i++) {
on[i] = rnd(0, 1);
if (!on[i]) continue;
for (int j = 0; j < n; j++) {
a[i] += A[i][j], b[i] += A[j][i];
}
}
for (int i = 0; i < n; i++) {
Mint x = 0, y = 0;
for (int j = 0; j < n; j++) {
x += a[j] * C[j][i];
y += C[i][j] * b[j];
if (on[j]) {
x -= C[j][i], y -= C[i][j];
}
}
if (x.x != 0 && !usedc[i]) {
usedc[i] = 1, cols.push_back(i);
}
if (y.x != 0 && !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});
}
}
}
sort(out.begin(), out.end());
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: 4ms
memory: 35072kb
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...