結果

問題 No.2255 Determinant Sum
ユーザー milanis48663220milanis48663220
提出日時 2023-03-24 22:32:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 6,790 bytes
コンパイル時間 1,529 ms
コンパイル使用メモリ 133,944 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 21:17:47
合計ジャッジ時間 2,770 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 31 ms
4,348 KB
testcase_07 AC 38 ms
4,348 KB
testcase_08 AC 21 ms
4,348 KB
testcase_09 AC 30 ms
4,348 KB
testcase_10 AC 24 ms
4,348 KB
testcase_11 AC 9 ms
4,348 KB
testcase_12 AC 11 ms
4,348 KB
testcase_13 AC 7 ms
4,348 KB
testcase_14 AC 48 ms
4,348 KB
testcase_15 AC 17 ms
4,348 KB
testcase_16 AC 13 ms
4,348 KB
testcase_17 AC 18 ms
4,348 KB
testcase_18 AC 15 ms
4,348 KB
testcase_19 AC 10 ms
4,348 KB
testcase_20 AC 12 ms
4,348 KB
testcase_21 AC 23 ms
4,348 KB
testcase_22 AC 18 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/modint>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using mint = atcoder::modint;

ostream& operator<<(ostream& os, const mint& m){
    os << m.val();
    return os;
}

template <typename T>
struct matrix{
    int n, m;
    vector<vector<T>> dat;
    matrix(int n_, int m_){
        n = n_; m = m_;
        for(int i = 0; i < n; i++){
            dat.push_back(vector<T>(m));
        }
    }

    matrix(int n, int m, T val): n(n), m(m){
        for(int i = 0; i < n; i++){
            dat.push_back(vector<T>(m, val));
        }
    }

    matrix(int n, int m, vector<vector<T>> dat): n(n), m(m), dat(dat){
        assert((int)dat.size() == n);
        for(int i = 0; i < n; i++) assert(dat[i].size() == m);
    }
    
    vector<T>& operator[](int x) {
        return dat[x];
	}
};

template <typename T>
bool prod(matrix<T> a, matrix<T> b, matrix<T> &ans){
    assert(a.m == b.n);
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < b.m; j++){
            ans.dat[i][j] = 0;
            for(int k = 0; k < b.n; k++){
                ans.dat[i][j] += (a.dat[i][k]*b.dat[k][j]);
            }
        }
    }
    return true;
}

template <typename T>
void copy_mat(matrix<T> a, matrix<T> &b){
    assert(a.n == b.n);
    assert(a.m == b.m);
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.m; j++){
            b.dat[i][j] = a.dat[i][j];
        }
    }
}

template <typename T>
void pow_mat(matrix<T> a, ll n, matrix<T> &ans){
    assert(n < ((ll)1<<61));
    matrix<T> buf(a.n, a.n);
    matrix<T> tmp(a.n, a.n);
    copy_mat(a, tmp);

    for(int i = 0; i < a.n; i++) {
        for(int j = 0; j < a.n; j++){
            ans.dat[i][j] = 0;
        }
        ans.dat[i][i] = 1;
    }

    while (n > 0) {
        if (n&1) {
            prod(tmp, ans, buf);
            copy_mat(buf, ans);
        }
        prod(tmp, tmp, buf);
        copy_mat(buf, tmp);
        n >>= 1;
    }
}

template <typename T>
void print_mat(matrix<T> a){
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.m; j++){
            cout << a.dat[i][j] << ' ';
        }
        cout << endl;
    }
}

template <typename T>
ostream& operator<<(ostream& os, const matrix<T>& m){
    print_mat<T>(m);
    return os;
}

/**
 * I+a+...+a^nを求めます(ちゃんと検証していないのであってないかも) 
 */ 
template<typename T>
void sum_pow(matrix<T> a, ll n, matrix<T> &ans){
    assert(a.n == a.m);
    assert(a.n == ans.n);
    assert(a.n == ans.m);
    matrix<T> A(2*a.n, 2*a.n);
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j  < a.n; j++){
            A[i][j] = a[i][j];
            if(i == j){
                A[i+a.n][j] = 1;
                A[i+a.n][j+a.n] = 1;
            }
        }
    }
    matrix<T> B(2*a.n, a.n);
    for(int i = 0; i < a.n; i++){
        B[i][i] = 1;
    }
    matrix<T> S(2*a.n, a.n);
    matrix<T> buf(2*a.n, 2*a.n);
    pow_mat(A, n+1, buf);
    prod(buf, B, S);
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.n; j++){
            ans[i][j] = S[i+a.n][j];
        }
    }
}

/**
 * verified: https://judge.yosupo.jp/submission/35808 
 */
template<typename T>
T det(matrix<T> a, T unit_prod, T unit_sum){
    assert(a.n == a.m);
    int n = a.n;
    matrix<T> b(n, n);
    copy_mat(a, b);
    int cnt_swap = 0;
    for(int i = 0; i < n; i++){
        if(b[i][i] == unit_sum){
            for(int j = i+1; j < n; j++){
                if(b[j][i] != unit_sum){
                    // swap
                    cnt_swap++;
                    for(int k = 0; k < n; k++){
                        swap(b[i][k], b[j][k]);
                    }
                    break;
                }
            }
        }
        if(b[i][i] ==  unit_sum) continue;
        T inv = unit_prod/b[i][i];
        for(int j = i+1; j < n; j++){
            for(int k = n-1; k >= 0; k--){
                b[j][k] -= b[i][k]*b[j][i]*inv;
            }
        }
    }
    T ans = unit_prod;
    for(int i = 0; i < a.n; i++) ans *= b[i][i];
    if(cnt_swap%2 == 1) ans *= -1;
    return ans;
}

template<typename T>
matrix<T> operator*(matrix<T> m1, matrix<T> m2){
    matrix<T> ans(m1.n, m2.m);
    prod(m1, m2, ans);
    return ans;
}

/**
 * 累乗
 */ 
template<typename T>
matrix<T> operator^(matrix<T> m, ll r){
    matrix<T> ans(m.n, m.n);
    pow_mat(m, r, ans);
    return ans;
}

using M = matrix<mint>;

void solve(){
    int n, p; cin >> n >> p;
    mint::set_mod(p);
    auto a = vec2d(n, n, 0);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++) {
            cin >> a[i][j];
        }
    }
    if(p == 2){
        vector<int> cnt_row(n), cnt_col(n);
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(a[i][j] == -1) {
                    cnt_row[i]++;
                    cnt_col[j]++;
                }
            }
        }
        for(int i = 0; i < n; i++){
            if(cnt_col[i] >= 2 || cnt_row[i] >= 2){
                cout << 0 << endl;
                return;
            }
        }
        M b(n, n);
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(a[i][j] == -1){
                    b[i][j] = 1;
                }else if(cnt_row[i] >= 1 || cnt_col[j] >= 1){
                    b[i][j] = 0;
                }else{
                    b[i][j] = a[i][j];
                }
            }
        }
        cout << det(b, mint(1), mint(0)) << endl;
    }else{
        cout << 0 << endl;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int t; cin >> t;
    while(t--) solve();
}
0