QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#326867 | #7612. Matrix Inverse | Pentagonal | TL | 1ms | 5796kb | C++17 | 9.7kb | 2024-02-14 11:49:37 | 2024-02-14 11:49:37 |
Judging History
answer
// #pragma GCC target("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimization ("Ofast")
//#pragma GCC -Wnarrowing
//Template {
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <chrono>
using namespace std;
using namespace __gnu_pbds;
//IO templates
//Note: use endl only for interactive problems or to debug segfaults; use newl for other problems
#define newl "\n"
#define fastIO ios::sync_with_stdio(false); cin.tie(nullptr)
#define fileIO(x) ifstream fin((str) x + (str) ".in"); ofstream fout((str) x + (str) ".out");
// void fileIO(string x) {}
#define flush() fflush(stdout)
#define interact(n) fflush(stdout); cin >> n; if (n == -1) return 0
#define testcases int tt; cin >> tt; fun (i, tt) solve();
#define edgeIO(m) fun (i, m) {int a, b; cin >> a >> b; addEdges(a, b);}
#define WeightedEdgeIO(m) fun (i, m) {int a, b, c; cin >> a >> b >> c; addWeightedEdges(a, b, c);}
#define numberedEdgeIO(m) fun (i, m) {int a, b; cin >> a >> b; addWeightedEdges(a, b, i);}
//types
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define int long long
#define ld long double
#define str string
#define boolean bool
#define String string
//vector
#define pb push_back
#define append push_back
//pairs
#define mp make_pair
#define p2 pair<int, int>
#define p3 pair<int, p2>
#define m3(x, y, z) mp(x, mp(y, z))
#define ich first
#define ni second.first
#define sanshi second.second
//For loops
#define ahegao(i, a, b) for (int i = a; i < b; i++)
#define baka(i, b, a) for (int i = b; i > a; i--)
#define fun(i, n) for (int i = 1; i <= (n); (i)++)
#define fon(i, n) for (int i = 0; i < (n); (i)++)
#define fur(i, n) for (auto i : (n))
#define oniichan(i, n) for (auto &i : (n))
//Sorts
#define sz(aaa) ((signed) aaa.size())
// #define len(aaa) ((signed) aaa.size())
#define all(a) a.begin(), a.end()
#define Sort(a) sort((a).begin(), (a).end())
#define rSort(a) sort((a).rbegin(), (a).rend())
#define clamp(x, y) (x) = min((int) (x), (int) (y))
#define CLAMP(x, y) (x) = max((int) (x), (int) (y))
//Other stuff
#define pqueue priority_queue
#define elif else if
#define addEdges(a, b) adj[a].pb(b); adj[b].pb(a)
#define addWeightedEdges(a, b, c) adj[a].pb(mp(b, c)); adj[b].pb(mp(a, c))
// #define find find_by_order
#define printLength(x) if (x < INF) cout << x << newl; else cout << -1 << newl;
// #define printVector(a) fur (i, a) cout << i << ' '; cout << newl;
void printVector(vector<int> DontUseThisName) {
fur (i, DontUseThisName) cout << i << ' '; cout << newl;
}
void printVector(vector<p2> DontUseThisName) {
fur (i, DontUseThisName) cout << i.first << ' ' << i.second << newl; cout << newl;
}
void printVector(vector<vector<int>> DontUseThisName) {
fur (i, DontUseThisName) printVector(i); cout << newl;
}
ll max(ll a, signed b) {return max(a, (ll) b);}
ll max(signed a, ll b) {return max((ll) a, b);}
void pv(int a) {cout << a << newl;}
void pv(int a, int b) {printVector({a, b});}
void pv(p2 a) {printVector({a.first, a.second});};
void pv(int a, int b, int c) {printVector({a, b, c});}
void pv(int a, int b, int c, int d) {printVector({a, b, c, d});}
void pv(int a, int b, int c, int d, int e) {printVector({a, b, c, d, e});}
void pv(int a, int b, int c, int d, int e, int f) {printVector({a, b, c, d, e, f});}
// void pv(int a, )
// void printVector(vector<char> DontUseThisName) {
// fur (i, DontUseThisName) cout << i << ' '; cout << newl;
// }
// void printRange(vector<int>::iterator Left, vector<int>::iterator Right) {
// for (auto i = Left; i < Right; i++) cout << *i << ' ';
// cout << newl;
// }
//Constants
const int MOD = 1e9+7; // 998244353
// const int SMALLMOD = 998244353;
const int INF = 2e9+1337;
const ll EXCEED = 2e18+1337;
const ll GRAVITY = 8e18;
//#define vectorIO(n, MikuBondage) fun (j, n) {int i; cin >> i; MikuBondage.pb(i);}
void vectorIO(int n, vector<int> &DontUseThisName) {
fun (j, n) {int i; cin >> i; DontUseThisName.pb(i);}
}
//#define vector2IO(n, MikuBondage) fun (j, n) {int i, ii; cin >> i >> ii; MikuBondage.pb(mp(i, ii));}
void vector2IO(int n, vector<p2> &DontUseThisName) {
fun (j, n) {int i, ii; cin >> i >> ii; DontUseThisName.pb(mp(i, ii));}
}
// const int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
#define shortest_path_queue priority_queue<p2, vector<p2>, greater<p2>>
#define printArray(DontUseThisName, NakedLolisGalore, GenshinImpactClimbing) ahegao (j, NakedLolisGalore, GenshinImpactClimbing + 1) cout << DontUseThisName[j] << ' '; cout << newl;
#define print2dArray(SplitComplexProblemsIntoMultipleParts, ScuteSwarm, GenshinImpactClimbing) fun (i, ScuteSwarm) {fun (j, GenshinImpactClimbing) cout << SplitComplexProblemsIntoMultipleParts[i][j] << ' '; cout << newl;}
//}
#include <chrono>
unsigned rng = chrono::steady_clock::now().time_since_epoch().count();
int randint(int k) {
rng = 1337 * (rng % 483187) + (rng << 5) + 987654321 + (rng % 235612) * (rng * 55679);
return rng % k;
}
const int MAX = 2003;
int n, val[MAX][MAX], inv[MAX][MAX];
bool badRow[MAX], badCol[MAX];
int x[MAX], y[MAX], z[MAX];
bool checkRow(int i, int j) {
int res = 0;
fun (k, n) res = (res + inv[i][k] * val[k][j]) % MOD;
int temp = i == j;
pv(res, temp);
if (res != temp) badRow[i] = true;
return res == (i == j);
}
bool checkColumn(int i, int j) {
int res = 0;
fun (k, n) res = (res + val[i][k] * inv[k][j]) % MOD;
// cout << res << newl;
int temp = i == j;
pv(res, temp);
if (res != temp) badCol[j] = true;
return res == (i == j);
}
int inverse(int a) {
int b = MOD - 2;
int res = 1;
// int i = 0;
baka(i, 30, -1) {
res = (res * res) % MOD;
if (b & (1 << i)) {
// cout << i << newl;
res = (res * a) % MOD;
}
}
return res;
}
vector<int> solve(vector<vector<int>> &nums) {
int k = sz(nums);
fon (i, k) {
int temp = inverse(nums[i][i]);
ahegao (j, i, k+1) {
nums[i][j] *= temp;
nums[i][j] %= MOD;
}
// printVector(nums);
ahegao (l, i+1, k) {
int curr = nums[l][i];
ahegao(j, i, k+1) {
nums[l][j] += curr * (MOD - nums[i][j]);
nums[l][j] %= MOD;
}
}
// printVector(nums);
}
baka (i, k-1, -1) {
baka (j, i-1, -1) {
nums[j][k] += nums[j][i] * (MOD - nums[i][k]);
nums[j][k] %= MOD;
nums[j][i] = 0;
}
// printVector(nums);
}
vector<int> res;
fon (i, k) res.pb(nums[i][k]);
return res;
}
// bool checkColumn
signed main() {
fastIO;
cin >> n;
fun (i, n) fun (j, n) cin >> val[i][j];
fun (i, n) fun (j, n) cin >> inv[i][j];
set<int> r, c;
vector<int> rowPos, columnPos;
fun (i, n) x[i] = randint(MOD);
fun (i, n) fun(j, n) y[i] = y[i] + (val[i][j] * x[i]) % MOD;
fun (i, n) fun(j, n) z[i] = z[i] + (inv[i][j] * y[i]) % MOD;
fun (i, n) {
if (x[i] != z[i]) {
rowPos.pb(i);
badRow[i] = true;
}
}
// printArray(x, 1, n);
// printArray(y, 1, n);
// printArray(z, 1, n);
fun (i, n) {
x[i] = randint(MOD);
y[i] = z[i] = 0;
}
fun (i, n) fun(j, n) y[i] = y[i] + (val[j][i] * x[i]) % MOD;
fun (i, n) fun(j, n) z[i] = z[i] + (inv[j][i] * y[i]) % MOD;
fun (i, n) {
if (x[i] != z[i]) {
columnPos.pb(i);
badCol[i] = true;
}
}
// printArray(x, 1, n);
// printArray(y, 1, n);
// printArray(z, 1, n);
int pos = 1;
// pv(sz(r), sz(c));
// while (sz(r) < sz(c)) {
// r.insert(pos);
// pos++;
// }
// pos = 1;
// while (sz(r) > sz(c)) {
// c.insert(pos);
// pos++;
// }
rowPos.insert(rowPos.end(), all(r));
columnPos.insert(columnPos.end(), all(c));
// printVector(rowPos);
// printVector(columnPos);
vector<vector<int>> MikuBondage;
// fon (i, sz(rowPos)) fon (j, sz(columnPos)) {
// pv(rowPos[i], columnPos[j]);
// vector<int> temp(sz(rowPos) * sz(columnPos) + 1);
// temp[sz(rowPos) * sz(columnPos)] = (rowPos[i] == columnPos[j]);
// fon (k, sz(rowPos)) {
// temp[i * sz(rowPos) + k] = val[rowPos[k]][columnPos[j]];
// }
// MikuBondage.pb(temp);
// printVector(temp);
// }
map<p2, int> badMap;
pos = 0;
fur (i, rowPos) fur (j, columnPos) {
badMap[mp(i, j)] = pos;
pos++;
}
fun (i, n) fun (j, n) {
if (badRow[i] and badCol[j]) {
vector<int> temp(sz(badMap)+1);
temp[sz(badMap)] = (i == j);
fun (k, n) {
if (badRow[k]) temp[badMap[mp(k, j)]] = val[i][k];
else temp[sz(badMap)] = (temp[sz(badMap)] + val[i][k] * (MOD - inv[k][j])) % MOD;
}
// pv(i, j);
// printVector(temp);
MikuBondage.pb(temp);
}
}
// printVector(MikuBondage);
vector<int> s = solve(MikuBondage);
vector<vector<int>> MikuLewds;
int out = 0;
fun (i, n) fun (j, n) if (badRow[i] and badCol[j]) {
int k = badMap[mp(i, j)];
// pv(i, j, s[k]);
if (s[k] != inv[i][j])
MikuLewds.pb({i, j, s[k]});
}
Sort(MikuLewds);
cout << sz(MikuLewds) << newl;
printVector(MikuLewds);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5796kb
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...