QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#641632#7612. Matrix InversepropaneTL 0ms3780kbC++203.9kb2024-10-14 21:46:182024-10-14 21:46:18

Judging History

你现在查看的是最新测评结果

  • [2024-10-14 21:46:18]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3780kb
  • [2024-10-14 21:46:18]
  • 提交

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];
        }
    }
    vector<int> id(n);
    iota(id.begin(), id.end(), 0);
    vector<int> row, col;
    for(int i = 0; i < n; i++){
        shuffle(id.begin(), id.end(), rnd);
        bool ok = true;
        for(int j = 0; j < min(50, n); j++){
            int sum = 0;
            for(int k = 0; k < n; k++){
                sum = (sum + 1LL * a[i][k] * b[k][id[j]]) % mod;
            }
            if (sum != int(i == id[j])){
                ok = false;
                break;
            }
        }
        if (!ok) row.push_back(i);
    }
    for(int i = 0; i < n; i++){
        shuffle(id.begin(), id.end(), rnd);
        bool ok = true;
        for(int j = 0; j < min(50, n); j++){
            int sum = 0;
            for(int k = 0; k < n; k++){
                sum = (sum + 1LL * b[id[j]][k] * a[k][i]) % mod;
            }
            if (sum != int(i == id[j])){
                ok = false;
                break;
            }
        }
        if (!ok) 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: 3780kb

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...

output:


result: