結果

問題 No.1907 DETERMINATION
ユーザー flselfflself
提出日時 2023-10-09 00:16:37
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 712 ms / 4,000 ms
コード長 11,598 bytes
コンパイル時間 5,978 ms
コンパイル使用メモリ 326,436 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-07-26 18:17:07
合計ジャッジ時間 30,572 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 206 ms
6,940 KB
testcase_08 AC 83 ms
6,940 KB
testcase_09 AC 138 ms
6,940 KB
testcase_10 AC 501 ms
7,040 KB
testcase_11 AC 195 ms
6,940 KB
testcase_12 AC 489 ms
7,040 KB
testcase_13 AC 462 ms
6,940 KB
testcase_14 AC 418 ms
7,040 KB
testcase_15 AC 91 ms
6,940 KB
testcase_16 AC 30 ms
6,940 KB
testcase_17 AC 465 ms
6,944 KB
testcase_18 AC 321 ms
6,940 KB
testcase_19 AC 9 ms
6,944 KB
testcase_20 AC 460 ms
7,040 KB
testcase_21 AC 42 ms
6,944 KB
testcase_22 AC 417 ms
6,940 KB
testcase_23 AC 538 ms
6,944 KB
testcase_24 AC 146 ms
6,944 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 495 ms
7,168 KB
testcase_27 AC 598 ms
7,168 KB
testcase_28 AC 598 ms
7,040 KB
testcase_29 AC 593 ms
7,168 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 507 ms
7,168 KB
testcase_32 AC 489 ms
7,168 KB
testcase_33 AC 496 ms
7,040 KB
testcase_34 AC 495 ms
7,040 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 499 ms
7,040 KB
testcase_39 AC 494 ms
7,168 KB
testcase_40 AC 712 ms
7,168 KB
testcase_41 AC 495 ms
7,040 KB
testcase_42 AC 707 ms
7,040 KB
testcase_43 AC 711 ms
7,168 KB
testcase_44 AC 558 ms
7,296 KB
testcase_45 AC 602 ms
7,040 KB
testcase_46 AC 492 ms
7,040 KB
testcase_47 AC 488 ms
7,040 KB
testcase_48 AC 495 ms
7,040 KB
testcase_49 AC 521 ms
7,040 KB
testcase_50 AC 491 ms
7,168 KB
testcase_51 AC 493 ms
7,040 KB
testcase_52 AC 2 ms
6,940 KB
testcase_53 AC 512 ms
6,940 KB
testcase_54 AC 510 ms
6,944 KB
testcase_55 AC 2 ms
6,944 KB
testcase_56 AC 513 ms
6,940 KB
testcase_57 AC 512 ms
6,944 KB
testcase_58 AC 427 ms
7,040 KB
testcase_59 AC 318 ms
7,040 KB
testcase_60 AC 319 ms
7,040 KB
testcase_61 AC 399 ms
7,168 KB
testcase_62 AC 315 ms
7,168 KB
testcase_63 AC 494 ms
7,168 KB
testcase_64 AC 2 ms
6,940 KB
testcase_65 AC 2 ms
6,944 KB
testcase_66 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

#ifdef LOCAL
#include "debugger.hpp"
#else
#define dbg(...)
#endif

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 int getRoot() {
        if (getMod() == 1231453023109121) return 3;
        assert(false);
    }
    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; }
    explicit constexpr operator bool() const { return x != 0;}
    constexpr MLong operator-() const { MLong res; res.x = norm(getMod() - x); return res; }
    constexpr MLong inv() const { 
        i64 a = getMod(), b = x;
        i64 y = 0, z = 1;
        for (; b; ) {
            i64 k = a / b;
            std::swap(a -= k * b, b);
            std::swap(y -= k * z, z);
        }
        assert(a == 1);
        return MLong(y);
    }
    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 = i64(1E18) + 9;
 
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 int getRoot() {
        if (getMod() == 998244353) return 3;
        assert(false);
    }
    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 operator MLong<P>() const { return MLong<P>(x); }
    explicit constexpr operator int() const { return x; }
    explicit constexpr operator bool() const { return x != 0;}
    constexpr MInt operator-() const { MInt res; res.x = norm(getMod() - x); return res; }
    constexpr MInt inv() const {
        unsigned a = getMod(), b = x;
        int y = 0, z = 1;
        for (; b; ) {
            int k = a / b;
            std::swap(a -= k * b, b);
            std::swap(y -= k * z, z);
        }
        assert(a == 1U);
        return MInt(y);
    }
    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 = 998244353;
 
template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
 
constexpr int P = 998244353;
// using Z = MInt<P>;
#include "atcoder/all"
using Z = atcoder::modint998244353;


template<typename T>
T count_minor(const std::vector<std::vector<int>>& matrix, int idx = 0, int idy = 0) {
    assert(matrix.size() == matrix[0].size());
    int n = matrix.size();
    assert(idx < n && idy < n);
    if (n == 1) {
        return 0;
    }
    std::vector minor(n-1, std::vector<T>(n-1));
    for (int i = 0; i < n; ++i) {
        if (i == idx) continue;
        for (int j = 0; j < n; ++j) {
            if (j == idy) continue;
            minor[i - (i > idx)][j - (j > idy)] = matrix[i][j];
        }
    }
    n--;
    auto gauss=[&]()->T {
        for (int i = 0; i < n; ++i) {
            if (minor[i][i] == 0) {
                for (int j = i + 1; j < n; ++j) {
                    if (minor[j][i] != T{}) {
                        // for (int k = 0; k < n; ++k) {
                        //     std::swap(minor[i][k], minor[j][k]);
                        // }
                        std::swap(minor[i], minor[j]);
                        break;
                    }
                }
            }
            if (minor[i][i] == 0) {
                return 0;
            }
            for (int j = i + 1; j < n; ++j) {
                if (i == j) continue;
                T mul = minor[j][i] / minor[i][i];
                for (int k = i; k < n; ++k) {
                    minor[j][k] -= mul * minor[i][k];
                }
            }
        }
        T res = 1;
        for (int i = 0; i < n; ++i) {
            res *= minor[i][i];
        }
        return res;
    };
    return gauss();
}

// Kirchhoff 
Z Kirchhoff(const std::vector<std::vector<int>> &G) {
    int n = G.size();
    std::vector<std::vector<int>> L(n, std::vector<int>(n));
    for (int i = 0; i < n; ++i) {
        L[i][i] = G[i].size();
        for (auto &j : G[i]) {
            L[i][j]--;
        }
    }
    return count_minor<Z>(L);
}


/// @param A square matrix of size n
/// @return characteristic polynomial of A, which is `det(xI - M)`
template <typename T>
std::vector<T> char_poly(std::vector<std::vector<T>> M) {
    int N = M.size();
    assert(N == M[0].size());

    // Hessenberg Reduction
    for (int i = 0; i < N - 2; i++) {
        int p = -1;
        for (int j = i + 1; j < N; j++) {
            if (M[j][i] != T(0)) {
                p = j;
                break;
            }
        }
        if (p == -1) {
            continue;
        }
        M[i + 1].swap(M[p]);
        for (int j = 0; j < N; j++) {
            std::swap(M[j][i + 1], M[j][p]);
        }

        T r = T(1) / M[i + 1][i];
        for (int j = i + 2; j < N; j++) {
            T c = M[j][i] * r;
            for (int k = 0; k < N; k++) M[j][k] -= M[i + 1][k] * c;
            for (int k = 0; k < N; k++) M[k][i + 1] += M[k][j] * c;
        }
    }

    // La Budde's Method
    std::vector<std::vector<T>> P = {{T(1)}};
    for (int i = 0; i < N; i++) {
        std::vector<T> f(i + 2, 0);
        for (int j = 0; j <= i; j++) f[j + 1] += P[i][j];
        for (int j = 0; j <= i; j++) f[j] -= P[i][j] * M[i][i];

        T b = 1;
        for (int j = i - 1; j >= 0; j--) {
            b *= M[j + 1][j];
            T h = -M[j][i] * b;
            for (int k = 0; k <= j; k++) f[k] += h * P[j][k];
        }
        P.push_back(f);
    }
    return P.back();
}

/// @brief calculate `det(Ax + B)`, where A and B are square matrices of size n
/// @tparam T usually Z
/// @tparam Matrix usually std::vector<std::vector<T>>, or custom matrix class
/// @param A 
/// @param B 
/// @return `det(Ax + B)`
template <typename T>
std::vector<T> det_linear(std::vector<std::vector<T>> A, std::vector<std::vector<T>> B) {
    int N = A.size(), nu = 0;
    T det = 1;
    for (int i = 0; i < N; i++) {
        // do normal gaussian elimination
        int p = -1;
        for (int j = i; j < N; j++) {
            if (A[j][i] != T(0)) {
                p = j;
                break;
            }
        }
        // replace B with A
        if (p == -1) {
            // Increase nullity by 1
            if (++nu > N) {
                return std::vector<T>(N + 1, 0);
            }
            // i-th column is empty
            for (int j = 0; j < i; j++) {
                for (int k = 0; k < N; k++) {
                    B[k][i] -= B[k][j] * A[j][i];
                }
                A[j][i] = 0;
            }
            for (int j = 0; j < N; j++) {
                std::swap(A[j][i], B[j][i]);
            }
            // retry
            --i;
            continue;
        }

        if (p != i) {
            A[i].swap(A[p]);
            B[i].swap(B[p]);
            det = -det;
        }

        det *= A[i][i];
        T c = T(1) / A[i][i];
        for (int j = 0; j < N; j++) {
            A[i][j] *= c, B[i][j] *= c;
        }

        for (int j = 0; j < N; j++) {
            if (j != i) {
                T c = A[j][i];
                for (int k = 0; k < N; k++) {
                    A[j][k] -= A[i][k] * c, B[j][k] -= B[i][k] * c;
                }
            }
        }
    }

    for (auto &y : B) {
        for (T &x : y) {
            x = -x;
        }
    }

    auto f = char_poly(B);
    for (T &x : f) {
        x *= det;
    }
    f.erase(f.begin(), f.begin() + nu);
    f.resize(N + 1);
    return f;
}

void solv() {
    int n;
    std::cin >> n;
    
    std::vector<std::vector<Z>> D1(n, std::vector<Z>(n));
    auto D2 = D1;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int x;
            std::cin >> x;
            D2[i][j] = x;
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int x;
            std::cin >> x;
            D1[i][j] = x;
        }
    }

    auto poly = det_linear(D1, D2);
    for (int i = 0; i <= n; ++i) {
        if (i >= poly.size()) {
            std::cout << "0\n";
        } else {
            std::cout << poly[i].val() << '\n';
        }
    }
}

signed main() {
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
    
    int tt = 1;
    // std::cin >> tt;
    while (tt--) {
        solv();
    }

    return 0;
}
0