結果

問題 No.1907 DETERMINATION
ユーザー 👑 hitonanodehitonanode
提出日時 2022-02-01 02:14:05
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 10,193 bytes
コンパイル時間 1,459 ms
コンパイル使用メモリ 109,404 KB
実行使用メモリ 5,796 KB
最終ジャッジ日時 2023-08-26 00:06:51
合計ジャッジ時間 96,163 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1,019 ms
4,836 KB
testcase_08 AC 394 ms
4,376 KB
testcase_09 AC 692 ms
4,384 KB
testcase_10 AC 2,491 ms
5,520 KB
testcase_11 AC 575 ms
4,384 KB
testcase_12 AC 2,589 ms
5,620 KB
testcase_13 AC 2,637 ms
5,680 KB
testcase_14 AC 2,588 ms
5,540 KB
testcase_15 AC 513 ms
4,376 KB
testcase_16 AC 139 ms
4,384 KB
testcase_17 AC 2,320 ms
5,692 KB
testcase_18 AC 1,650 ms
5,120 KB
testcase_19 AC 34 ms
4,380 KB
testcase_20 AC 2,476 ms
5,524 KB
testcase_21 AC 199 ms
4,380 KB
testcase_22 AC 1,155 ms
4,380 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 2,640 ms
5,584 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 2 ms
4,380 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 2,632 ms
5,688 KB
testcase_34 AC 2,628 ms
5,624 KB
testcase_35 WA -
testcase_36 AC 2 ms
4,376 KB
testcase_37 WA -
testcase_38 AC 2,625 ms
5,596 KB
testcase_39 AC 2,630 ms
5,560 KB
testcase_40 WA -
testcase_41 AC 2,624 ms
5,576 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 2,616 ms
5,620 KB
testcase_47 AC 2,615 ms
5,592 KB
testcase_48 AC 2,632 ms
5,540 KB
testcase_49 AC 2,808 ms
5,796 KB
testcase_50 AC 2,635 ms
5,560 KB
testcase_51 AC 2,636 ms
5,556 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 1,670 ms
4,380 KB
testcase_54 AC 1,668 ms
4,384 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 1,669 ms
4,384 KB
testcase_57 AC 1,669 ms
4,384 KB
testcase_58 AC 1,595 ms
5,624 KB
testcase_59 AC 1,802 ms
5,580 KB
testcase_60 AC 1,815 ms
5,624 KB
testcase_61 AC 2,241 ms
5,728 KB
testcase_62 AC 1,818 ms
5,628 KB
testcase_63 AC 2,640 ms
5,652 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 1 ms
4,380 KB
testcase_66 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 任意 mod での実行時間検討
#include <cassert>
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;

struct ModIntRuntime {
private:
    static int md;

public:
    using lint = long long;
    static int mod() { return md; }
    int val;
    static std::vector<ModIntRuntime> &facs() {
        static std::vector<ModIntRuntime> facs_;
        return facs_;
    }
    static int &get_primitive_root() {
        static int primitive_root_ = 0;
        if (!primitive_root_) {
            primitive_root_ = [&]() {
                std::set<int> fac;
                int v = md - 1;
                for (lint i = 2; i * i <= v; i++)
                    while (v % i == 0) fac.insert(i), v /= i;
                if (v > 1) fac.insert(v);
                for (int g = 1; g < md; g++) {
                    bool ok = true;
                    for (auto i : fac)
                        if (ModIntRuntime(g).power((md - 1) / i) == 1) {
                            ok = false;
                            break;
                        }
                    if (ok) return g;
                }
                return -1;
            }();
        }
        return primitive_root_;
    }
    static void set_mod(const int &m) {
        if (md != m) facs().clear();
        md = m;
        get_primitive_root() = 0;
    }
    ModIntRuntime &_setval(lint v) {
        val = (v >= md ? v - md : v);
        return *this;
    }
    ModIntRuntime() : val(0) {}
    ModIntRuntime(lint v) { _setval(v % md + md); }
    explicit operator bool() const { return val != 0; }
    ModIntRuntime operator+(const ModIntRuntime &x) const {
        return ModIntRuntime()._setval((lint)val + x.val);
    }
    ModIntRuntime operator-(const ModIntRuntime &x) const {
        return ModIntRuntime()._setval((lint)val - x.val + md);
    }
    ModIntRuntime operator*(const ModIntRuntime &x) const {
        return ModIntRuntime()._setval((lint)val * x.val % md);
    }
    ModIntRuntime operator/(const ModIntRuntime &x) const {
        return ModIntRuntime()._setval((lint)val * x.inv() % md);
    }
    ModIntRuntime operator-() const { return ModIntRuntime()._setval(md - val); }
    ModIntRuntime &operator+=(const ModIntRuntime &x) { return *this = *this + x; }
    ModIntRuntime &operator-=(const ModIntRuntime &x) { return *this = *this - x; }
    ModIntRuntime &operator*=(const ModIntRuntime &x) { return *this = *this * x; }
    ModIntRuntime &operator/=(const ModIntRuntime &x) { return *this = *this / x; }
    friend ModIntRuntime operator+(lint a, const ModIntRuntime &x) {
        return ModIntRuntime()._setval(a % md + x.val);
    }
    friend ModIntRuntime operator-(lint a, const ModIntRuntime &x) {
        return ModIntRuntime()._setval(a % md - x.val + md);
    }
    friend ModIntRuntime operator*(lint a, const ModIntRuntime &x) {
        return ModIntRuntime()._setval(a % md * x.val % md);
    }
    friend ModIntRuntime operator/(lint a, const ModIntRuntime &x) {
        return ModIntRuntime()._setval(a % md * x.inv() % md);
    }
    bool operator==(const ModIntRuntime &x) const { return val == x.val; }
    bool operator!=(const ModIntRuntime &x) const { return val != x.val; }
    bool operator<(const ModIntRuntime &x) const {
        return val < x.val;
    } // To use std::map<ModIntRuntime, T>
    friend std::istream &operator>>(std::istream &is, ModIntRuntime &x) {
        lint t;
        return is >> t, x = ModIntRuntime(t), is;
    }
    friend std::ostream &operator<<(std::ostream &os, const ModIntRuntime &x) {
        return os << x.val;
    }

    lint power(lint n) const {
        lint ans = 1, tmp = this->val;
        while (n) {
            if (n & 1) ans = ans * tmp % md;
            tmp = tmp * tmp % md;
            n /= 2;
        }
        return ans;
    }
    ModIntRuntime pow(lint n) const { return power(n); }
    lint inv() const { return this->power(md - 2); }

    ModIntRuntime fac() const {
        int l0 = facs().size();
        if (l0 > this->val) return facs()[this->val];

        facs().resize(this->val + 1);
        for (int i = l0; i <= this->val; i++)
            facs()[i] = (i == 0 ? ModIntRuntime(1) : facs()[i - 1] * ModIntRuntime(i));
        return facs()[this->val];
    }

    ModIntRuntime doublefac() const {
        lint k = (this->val + 1) / 2;
        return (this->val & 1)
                   ? ModIntRuntime(k * 2).fac() / (ModIntRuntime(2).pow(k) * ModIntRuntime(k).fac())
                   : ModIntRuntime(k).fac() * ModIntRuntime(2).pow(k);
    }

    ModIntRuntime nCr(const ModIntRuntime &r) const {
        return (this->val < r.val) ? ModIntRuntime(0) : this->fac() / ((*this - r).fac() * r.fac());
    }

    ModIntRuntime sqrt() const {
        if (val == 0) return 0;
        if (md == 2) return val;
        if (power((md - 1) / 2) != 1) return 0;
        ModIntRuntime b = 1;
        while (b.power((md - 1) / 2) == 1) b += 1;
        int e = 0, m = md - 1;
        while (m % 2 == 0) m >>= 1, e++;
        ModIntRuntime x = power((m - 1) / 2), y = (*this) * x * x;
        x *= (*this);
        ModIntRuntime z = b.power(m);
        while (y != 1) {
            int j = 0;
            ModIntRuntime t = y;
            while (t != 1) j++, t *= t;
            z = z.power(1LL << (e - j - 1));
            x *= z, z *= z, y *= z;
            e = j;
        }
        return ModIntRuntime(std::min(x.val, md - x.val));
    }
};
int ModIntRuntime::md = 1;
using mint = ModIntRuntime;

// Upper Hessenberg reduction of square matrices
// Complexity: O(n^3)
// Reference:
// http://www.phys.uri.edu/nigh/NumRec/bookfpdf/f11-5.pdf
template <class Tp> void hessenberg_reduction(std::vector<std::vector<Tp>> &M) {
    assert(M.size() == M[0].size());
    const int N = M.size();
    for (int r = 0; r < N - 2; r++) {
        int piv = -1;
        for (int h = r + 1; h < N; ++h) {
            if (M[h][r] != 0) {
                piv = h;
                break;
            }
        }
        if (piv < 0) continue;
        for (int i = 0; i < N; i++) std::swap(M[r + 1][i], M[piv][i]);
        for (int i = 0; i < N; i++) std::swap(M[i][r + 1], M[i][piv]);

        const auto rinv = Tp(1) / M[r + 1][r];
        for (int i = r + 2; i < N; i++) {
            const auto n = M[i][r] * rinv;
            for (int j = 0; j < N; j++) M[i][j] -= M[r + 1][j] * n;
            for (int j = 0; j < N; j++) M[j][r + 1] += M[j][i] * n;
        }
    }
}

// Characteristic polynomial of matrix M (|xI - M|)
// Complexity: O(n^3)
// R. Rehman, I. C. Ipsen, "La Budde's Method for Computing Characteristic Polynomials," 2011.
template <class Tp> std::vector<Tp> characteristic_poly(std::vector<std::vector<Tp>> M) {
    hessenberg_reduction(M);
    const int N = M.size();
    // p[i + 1] = (Characteristic polynomial of i-th leading principal minor)
    std::vector<std::vector<Tp>> p(N + 1);
    p[0] = {1};
    for (int i = 0; i < N; i++) {
        p[i + 1].assign(i + 2, 0);
        for (int j = 0; j < i + 1; j++) p[i + 1][j + 1] += p[i][j];
        for (int j = 0; j < i + 1; j++) p[i + 1][j] -= p[i][j] * M[i][i];
        Tp betas = 1;
        for (int j = i - 1; j >= 0; j--) {
            betas *= M[j + 1][j];
            Tp hb = -M[j][i] * betas;
            for (int k = 0; k < j + 1; k++) p[i + 1][k] += hb * p[j][k];
        }
    }
    return p[N];
}


int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    mint::set_mod(998244353);
    int N;
    cin >> N;

    vector M0(N, vector<mint>(N)), M1(N, vector<mint>(N));

    for (auto &vec : M0) {
        for (auto &x : vec) {
            int v;
            cin >> v;
            x = v;
        }
    }

    for (auto &vec : M1) {
        for (auto &x : vec) {
            int v;
            cin >> v;
            x = v;
        }
    }

    int multiply_by_x = 0; // 基本変形の最中に M0 + M1x に x をかけた回数
    mint detAdetBinv = 1;

    for (int p = 0; p < N; ++p) {
        // M1[p][p] に nonzero を持ってきて、M1 の第 p 行と第 p 列を全て掃き出す
        int piv = -1;
        for (int r = p; r < N; ++r) {
            if (M1[r][p] != 0) {
                piv = r;
                break;
            }
        }
        if (piv < 0) {
            ++multiply_by_x;
            if (multiply_by_x > N) break;
            for (int i = 0; i < N; ++i) {
                swap(M1[i][p], M0[i][p]);
            }
            --p;
            continue;
        }

        if (piv != p) {
            M1[piv].swap(M1[p]);
            M0[piv].swap(M0[p]);
            detAdetBinv *= -1;
        }
        mint v = M1[p][p], vinv = v.inv();
        detAdetBinv *= v;

        // p 行目を定数倍して M1[p][p] == 1 にする
        for (int j = 0; j < N; ++j) {
            M0[p][j] *= vinv;
            M1[p][j] *= vinv;
        }
        assert(M1[p][p] == 1);

        // p 行目を使用して M1 の p 列目を p 行目以外ゼロにする
        for (int r = 0; r < N; ++r) {
            if (r == p) continue;
            if (M1[r][p] != 0) {
                auto v = M1[r][p];
                for (int j = 0; j < N; ++j) {
                    M0[r][j] -= M0[p][j] * v;
                    M1[r][j] -= M1[p][j] * v;
                }
            }
        }
        // p 列目を使用して M1 の p 行目を p 列目以外ゼロにする
        for (int j = p + 1; j < N; ++j) {
            if (M1[p][j] != 0) {
                auto v = M1[p][j];
                for (int r = 0; r < N; ++r) {
                    M0[r][j] -= M0[r][p] * v;
                    M1[r][j] -= M1[r][p] * v;
                }
            }
        }
    }

    if (multiply_by_x > N) {
        // 行列式がゼロであることが確定
        for (int i = 0; i <= N; ++i) cout << 0 << '\n';
        return 0;
    }

    // この時点で M1 = I なので det(x + M0) を求める
    for (auto &vec : M0) {
        for (auto &x : vec) x = -x;
    }
    auto poly = characteristic_poly(M0);
    for (auto &x : poly) x *= detAdetBinv;

    for (int i = 0; i < multiply_by_x; ++i) poly.erase(poly.begin());
    poly.resize(N + 1);
    for (auto a : poly) cout << a << '\n';
}
0