QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#641688 | #7612. Matrix Inverse | propane | WA | 693ms | 112768kb | C++20 | 4.9kb | 2024-10-14 22:20:23 | 2024-10-14 22:20:24 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<random>
#include<numeric>
#include<array>
#include<algorithm>
using namespace std;
using LL = long long;
const int mod = 1e9 + 7;
void add(int &a, int b){
a += b;
if (a >= mod) a -= mod;
}
void sub(int &a, int b){
a -= b;
if (a < 0) a += mod;
}
int mul(int a, int b){
return 1LL * a * b % mod;
}
int qpow(int a, int b){
int ans = 1;
while(b){
if (b & 1) ans = mul(ans, a);
b >>= 1;
a = mul(a, a);
}
return ans;
}
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
mt19937 rnd(213423421);
int n;
cin >> n;
vector<vector<int> > a(n, vector<int>(n));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> a[i][j];
}
}
vector<vector<int> > b(n, vector<int>(n));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> b[i][j];
}
}
uniform_int_distribution<int> dist(1, mod - 1);
vector<int> row, col;
if (n <= 12){
for(int i = 0; i < n; i++){
row.push_back(i);
col.push_back(i);
}
}
else{
{
vector<int> v(n);
for(int i = 0; i < n; i++){
v[i] = dist(rnd);
}
auto bk = v;
{
vector<int> nv(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
add(nv[j], mul(v[i], a[i][j]));
}
}
v.swap(nv);
}
{
vector<int> nv(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
add(nv[j], mul(v[i], b[i][j]));
}
}
v.swap(nv);
}
for(int i = 0; i < n; i++){
if (bk[i] != v[i]){
row.push_back(i);
}
}
}
{
vector<int> v(n);
for(int i = 0; i < n; i++){
v[i] = dist(rnd);
}
auto bk = v;
{
vector<int> nv(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
add(nv[j], mul(v[i], b[i][j]));
}
}
v.swap(nv);
}
{
vector<int> nv(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
add(nv[j], mul(v[i], a[i][j]));
}
}
v.swap(nv);
}
for(int i = 0; i < n; i++){
if (bk[i] != v[i]){
col.push_back(i);
}
}
}
}
auto guass = [&](vector<vector<int> > &a){
const int n = a.size();
for(int i = 0; i < n; i++){
int t = -1;
for(int j = i; j < n; j++){
if (a[j][i]){
t = j;
break;
}
}
swap(a[t], a[i]);
int inv = qpow(a[i][i], mod - 2);
for(int j = 0; j < n + 1; j++) a[i][j] = mul(a[i][j], inv);
for(int j = 0; j < n; j++){
if (i != j and a[j][i]){
for(int k = n; k >= i; k--){
sub(a[j][k], mul(a[j][i], a[i][k]));
}
}
}
}
vector<int> ans(n);
for(int i = 0; i < n; i++) ans[i] = a[i].back();
return ans;
};
vector<pair<int, int> > p;
vector<vector<int> > q(n, vector<int>(n, -1));
for(auto x : row){
for(auto y : col){
q[x][y] = p.size();
p.push_back({x, y});
}
}
vector<vector<int> > c(p.size(), vector<int>(p.size() + 1));
for(int l = 0; l < p.size(); l++){
auto [x, y] = p[l];
c[l].back() = (x == y);
for(int i = 0; i < n; i++){
if (q[i][y] == -1){
sub(c[l].back(), mul(a[x][i], b[i][y]));
}
else{
c[l][q[i][y]] = a[x][i];
}
}
}
auto res = guass(c);
vector<array<int, 3> > ans;
for(int i = 0; i < p.size(); i++){
auto [x, y] = p[i];
if (res[i] != b[x][y]){
ans.push_back({x, y, res[i]});
}
}
cout << ans.size() << '\n';
for(auto [x, y, z] : ans) cout << x + 1 << ' ' << y + 1 << ' ' << z << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Wrong Answer
time: 693ms
memory: 112768kb
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:
4 238 238 426153420 238 499 703395553 499 238 604559761 499 499 904951262
result:
wrong answer 1st lines differ - expected: '2', found: '4'