結果

問題 No.2255 Determinant Sum
ユーザー jianglyjiangly
提出日時 2023-03-24 22:11:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 7,783 bytes
コンパイル時間 2,295 ms
コンパイル使用メモリ 214,472 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 21:08:07
合計ジャッジ時間 3,836 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 118 ms
4,348 KB
testcase_07 AC 24 ms
4,348 KB
testcase_08 AC 25 ms
4,348 KB
testcase_09 AC 25 ms
4,348 KB
testcase_10 AC 84 ms
4,348 KB
testcase_11 AC 8 ms
4,348 KB
testcase_12 AC 16 ms
4,348 KB
testcase_13 AC 9 ms
4,348 KB
testcase_14 AC 30 ms
4,348 KB
testcase_15 AC 26 ms
4,348 KB
testcase_16 AC 40 ms
4,348 KB
testcase_17 AC 27 ms
4,348 KB
testcase_18 AC 18 ms
4,348 KB
testcase_19 AC 20 ms
4,348 KB
testcase_20 AC 28 ms
4,348 KB
testcase_21 AC 16 ms
4,348 KB
testcase_22 AC 20 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;
template<class T>
constexpr T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}

constexpr i64 mul(i64 a, i64 b, i64 p) {
    i64 res = a * b - i64(1.L * a * b / p) * p;
    res %= p;
    if (res < 0) {
        res += p;
    }
    return res;
}
template<i64 P>
struct MLong {
    i64 x;
    constexpr MLong() : x{} {}
    constexpr MLong(i64 x) : x{norm(x % getMod())} {}
    
    static i64 Mod;
    constexpr static i64 getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(i64 Mod_) {
        Mod = Mod_;
    }
    constexpr i64 norm(i64 x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr i64 val() const {
        return x;
    }
    explicit constexpr operator i64() const {
        return x;
    }
    constexpr MLong operator-() const {
        MLong res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MLong inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MLong &operator*=(MLong rhs) & {
        x = mul(x, rhs.x, getMod());
        return *this;
    }
    constexpr MLong &operator+=(MLong rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MLong &operator-=(MLong rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MLong &operator/=(MLong rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MLong operator*(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MLong operator+(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MLong operator-(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MLong operator/(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MLong &a) {
        i64 v;
        is >> v;
        a = MLong(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MLong &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MLong lhs, MLong rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MLong lhs, MLong rhs) {
        return lhs.val() != rhs.val();
    }
};

template<>
i64 MLong<0LL>::Mod = 1;

template<int P>
struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x) : x{norm(x % getMod())} {}
    
    static int Mod;
    constexpr static int getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(int Mod_) {
        Mod = Mod_;
    }
    constexpr int norm(int x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr int val() const {
        return x;
    }
    explicit constexpr operator int() const {
        return x;
    }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator*=(MInt rhs) & {
        x = 1LL * x * rhs.x % getMod();
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};

template<>
int MInt<0>::Mod = 1;

template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

using Z = MInt<0>;

using u64 = unsigned long long;

Z det(std::vector<std::vector<Z>> a) {
    int n = a.size();
    Z ans = 1;
    for (int i = 0; i < n; i++) {
        int j = i;
        while (j < n && a[j][i] == 0) {
            j++;
        }
        if (j == n) return 0;
        if (i != j) {
            std::swap(a[i], a[j]);
            ans *= -1;
        }
        ans *= a[i][i];
        auto v = Z(a[i][i]).inv();
        for (int j = i; j < n; j++) {
            a[i][j] *= v;
        }
        for (int j = i + 1; j < n; j++) {
            Z v = a[j][i];
            for (int k = i; k < n; k++) {
                a[j][k] -= a[i][k] * v;
            }
        }
    }
    return ans;
}


void solve() {
    int N, P;
    std::cin >> N >> P;
    
    Z::setMod(P);
    
    int m = 0;
    if (P != 2) {
        std::vector A(N, std::vector<Z>(N));
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                int x;
                std::cin >> x;
                
                if (x == -1) {
                    m += 1;
                } else {
                    A[i][j] = x;
                }
            }
        }
        
        auto ans = det(A);
        if (m > 0) {
            ans = 0;
        }
        std::cout << ans << "\n";
    } else {
        std::vector A(N, std::vector<u64>(N));
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                int x;
                std::cin >> x;
                
                if (x == -1) {
                    m += 1;
                    A[i][j] = 1;
                } else {
                    A[i][j] = x * 2;
                }
            }
        }
        
        u64 M = 1LL << (N + 1);
        M -= 1;
        
        u64 ans = 1;
        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                while (A[j][i]) {
                    std::swap(A[i], A[j]);
                    ans = -ans & M;
                    u64 t = A[j][i] / A[i][i];
                    for (int k = i; k < N; k++) {
                        A[j][k] -= A[i][k] * t;
                        A[j][k] &= M;
                    }
                }
            }
            ans *= A[i][i];
            ans &= M;
        }
        
        if (m > 60) {
            ans = 0;
        } else {
            ans <<= m;
        }
        ans &= M;
        ans >>= N;
        std::cout << ans << "\n";
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int T;
    std::cin >> T;
    
    while (T--) {
        solve();
    }
    
    return 0;
}
0