QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#455898#7612. Matrix Inverseucup-team3924#RE 0ms3540kbC++204.5kb2024-06-26 23:31:152024-06-26 23:31:16

Judging History

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

  • [2024-06-26 23:31:16]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3540kb
  • [2024-06-26 23:31:15]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int mod = 1000000007;
using mat = vector<vector<int>>;

struct ModInt{
    int x;
    ModInt (int x = 0): x(x + (x < 0)*mod - (x >= mod) * mod){}
    ModInt operator+(ModInt o) {return x + o.x; }
    ModInt operator-(ModInt o) {return x - o.x; }
    ModInt operator*(ModInt o) {return 1LL * x * o.x % mod;}
    ModInt operator/(ModInt b) {return *this * b.inv();}
    ModInt inv() {return pow(mod - 2);}
    ModInt pow(long long e){
        if(!e)return 1;
        ModInt r = pow(e/2); r = r * r;
        return e % 2 ? *this * r : r;
    }
    bool operator==(ModInt o){return x == o.x;}
    bool operator>(ModInt o){return x > o.x;}
};

pair<vector<int>, int>RowEchelon(vector<vector<ModInt>>& A){
    int n = A.size(), m = A[0].size(), sgn = 1;
    vector<int>piv;
    for(int i = 0, rnk = 0; i < m && rnk < n; ++i){
        for(int j = rnk + 1; j < n; j++)
            if(A[j][i]>A[rnk][i])
                swap(A[j], A[rnk]), sgn = -sgn;
        if(A[rnk][i] == 0)continue;
        for(int j = 0; j < n; j++){
            ModInt coef = A[j][i] / A[rnk][i];
            if(j == rnk || coef == 0)continue;
            for(int k = 0; k < m; k++){
                A[j][k] = A[j][k] - coef * A[rnk][k];
            }
        }
        piv.push_back(i);++rnk;
    }
    return {piv, sgn};
}

vector<ModInt>SolveLinear(vector<vector<ModInt>>& A){
    int m = A[0].size() - 1;
    auto piv = RowEchelon(A).first;
    if(piv.size() > m)return {};
    vector<ModInt>sol(m, 0);
    for(int i = 0; i < (int)piv.size(); i++){
        sol[piv[i]] = A[i][m] / A[i][i];
    }
    return sol;
}

vector<int>left_mult(vector<vector<int>>A, vector<int>b){
    vector<int>res(b.size(), 0);
    int n = b.size();
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            res[i] = (res[i] + ((long long)A[i][j]*b[j])%mod)%mod;
        }
    }
    return res;
}

vector<int>right_mult(vector<vector<int>>A, vector<int>b){
    vector<int>res(b.size(), 0);
    int n = b.size();
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            res[i] = (res[i] + ((long long)A[j][i]*b[j])%mod)%mod;
        }
    }
    return res;
}

vector<int>vecmin(vector<int>v, vector<int>u){
    assert(v.size() == u.size());
    vector<int>res(v.size());
    for(int i = 0; i < (int)v.size(); i++){
        res[i] = (v[i] - u[i])%mod;
    }
    return res;
}


vector<ModInt>solve(const mat &A, const mat &modC, int col, const vector<int>&rows){
    int n = A.size();
    vector<int>b(n, 0);
    b[col] = 1;
    vector<int>c(n, 0);
    for(int i = 0; i < n; i++)c[i] = modC[i][col];
    // b = vecmin(b, left_mult(A, c));
    vector<vector<ModInt>>S(n, vector<ModInt>(rows.size()+1));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < (int)rows.size(); j++){
            S[i][j] = A[i][rows[j]];
        }
    }
    for(int i = 0; i < n; i++){
        S[i][rows.size()] = b[i];
    }

    vector<ModInt>res = SolveLinear(S);
    return res;

}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    mt19937 rng(124981);
    int n;
    cin >> n;
    vector<vector<int>>A(n, vector<int>(n)), C(n, vector<int>(n));
    for(auto &row : A){
        for(auto &x: row)cin >> x;
    }
    for(auto &row : C){
        for(auto &x: row)cin >> x;
    }
    vector<int>r(n);
    vector<int>cols, rows;
    for(auto &x : r)x = rng()%mod;
    vector<int>prod = left_mult(C, left_mult(A, r));
    for(int i = 0; i < n; i++){
        if(prod[i] != r[i])rows.push_back(i);
    }
    prod = right_mult(C, right_mult(A, r));
    for(int i = 0; i < n; i++){
        if(prod[i] != r[i])cols.push_back(i);
    }
    vector<pair<int,int>>vars;
    for(int row: rows){
        for(int col: cols){
            vars.push_back({row, col});
        }
    }
    vector<pair<pair<int,int>, int>>ans;
    vector<vector<int>>d(vars.size(), vector<int>(vars.size()));
    vector<int>Ar = left_mult(A,r);
    vector<vector<int>>modC = C;
    for(auto [i,j]: vars){
        modC[i][j] = 0;
    }
    for(int col : cols){
        auto res = solve(A, modC, col, rows);
        for(int j = 0; j < (int)rows.size(); j++){
            if(C[j][col] != res[j].x){
                ans.push_back({{j+1, col+1}, res[j].x});
            }
        }

    }
    sort(ans.begin(), ans.end(), [](pair<pair<int,int>, int>a, pair<pair<int,int>, int>b){
       if(a.first.first == b.first.first)return a.first.second < b.first.second;
       return a.first.first < b.first.first; 
    });
    cout << ans.size() << '\n';
    for(auto p : ans)cout << p.first.first << ' ' << p.first.second << ' ' << p.second << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3540kb

input:

1
953176428
107682094

output:

0

result:

ok single line: '0'

Test #2:

score: -100
Runtime Error

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: