QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#422555 | #7612. Matrix Inverse | SampsonYW | TL | 1ms | 6036kb | C++14 | 3.2kb | 2024-05-27 16:02:56 | 2024-05-27 16:02:57 |
Judging History
answer
#include <bits/stdc++.h>
#define db double
#define il inline
#define re register
#define ll long long
#define ui unsigned
#define ull ui ll
#define i128 __int128
#define pii pair<int, int>
#define fi first
#define se second
#define eb emplace_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define mems(v, x) memset(v, x, sizeof(v))
#define memc(a, b) memcpy(a, b, sizeof(a))
#define FOR(i, L, R) for(re int i = (L); i <= (R); ++i)
#define ROF(i, R, L) for(re int i = (R); i >= (L); --i)
#define LS i << 1, l, mid
#define RS i << 1 | 1, mid + 1, r
#define popc(x) __builtin_popcount(x)
using namespace std;
#define N 2005
#define M 15
#define P 1000000007
il int add(int x, int y) {return x + y < P ? x + y : x + y - P;}
il void addr(int &x, int y) {(x += y) >= P && (x -= P);}
il int qpow(int p, int n = P - 2) {
int s = 1;
while(n) {
if(n & 1) s = 1ll * s * p % P;
p = 1ll * p * p % P, n >>= 1;
}
return s;
}
il void chkmin(int &x, int y) {(x > y) && (x = y);}
il void chkmax(int &x, int y) {(x < y) && (x = y);}
mt19937 rnd(114514);
int n, m, A[N][N], B[N][N], C[N], v[N];
bool r[N], c[N]; int tr[M][M][M];
il void solve(int id) {
auto &T = tr[id];
// FOR(i, 0, m - 1) FOR(j, 0, m) cout << T[i][j] << " \n"[j == m];
FOR(i, 0, m - 1) {
int t = i;
FOR(j, i, m - 1) if(T[j][i]) {t = j; break;}
swap(T[i], T[t]);
int inv = qpow(T[i][i]);
FOR(j, 0, m - 1) if(i != j) {
int c = 1ll * T[j][i] * inv % P;
FOR(k, i, m) addr(T[j][k], P - 1ll * c * T[i][k] % P);
}
}
FOR(i, 0, m - 1) T[i][m] = 1ll * T[i][m] * qpow(T[i][i]) % P;
// cout << "x = "; FOR(i, 0, m - 1) cout << T[i][m] << " \n"[i == m - 1];
}
struct node {pii t; int v;} ;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
FOR(i, 1, n) FOR(j, 1, n) cin >> A[i][j];
FOR(i, 1, n) FOR(j, 1, n) cin >> B[i][j];
FOR(i, 1, n) v[i] = rnd() % P;
FOR(i, 1, n) {C[i] = 0; FOR(j, 1, n) addr(C[i], 1ll * B[i][j] * v[j] % P);}
FOR(i, 1, n) {int s = 0; FOR(j, 1, n) addr(s, 1ll * A[i][j] * C[j] % P); if(s != v[i]) r[i] = 1;}
FOR(j, 1, n) {C[j] = 0; FOR(i, 1, n) addr(C[j], 1ll * v[i] * A[i][j] % P);}
FOR(j, 1, n) {int s = 0; FOR(i, 1, n) addr(s, 1ll * C[i] * B[i][j] % P); if(s != v[j]) c[j] = 1;}
vector<int> row, col;
FOR(i, 1, n) if(r[i]) row.eb(i);
FOR(i, 1, n) if(c[i]) col.eb(i);
m = SZ(row);
FOR(p, 0, m - 1) {
FOR(i, 1, n) v[i] = rnd() % P;
FOR(j, 1, n) {C[j] = 0; FOR(i, 1, n) addr(C[j], 1ll * v[i] * A[i][j] % P);}
FOR(id, 0, SZ(col) - 1) {
int j = col[id], s = 0;
FOR(i, 1, n) if(!r[i]) addr(s, 1ll * C[i] * B[i][j] % P);
FOR(q, 0, m - 1) {int i = row[q]; tr[id][p][q] = C[i];}
tr[id][p][m] = add(v[j], P - s);
}
}
vector<node> ns;
FOR(id, 0, SZ(col) - 1) {
// cout << "solve = " << col[id] << "\n";
int j = col[id]; solve(id);
auto &T = tr[id];
FOR(q, 0, m - 1) {
int i = row[q];
if(B[i][j] != T[q][m]) ns.eb(node {{i, j}, T[q][m]});
}
}
cout << SZ(ns) << "\n";
sort(ALL(ns), [](node A, node B) {return A.t < B.t;});
for(auto [t, v] : ns) cout << t.fi << " " << t.se << " " << v << "\n";
cerr << 1.0 * clock() / CLOCKS_PER_SEC << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6036kb
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...