結果
問題 | No.1907 DETERMINATION |
ユーザー | 松杰方 |
提出日時 | 2023-10-28 20:09:15 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 793 ms / 4,000 ms |
コード長 | 7,154 bytes |
コンパイル時間 | 3,238 ms |
コンパイル使用メモリ | 259,684 KB |
実行使用メモリ | 7,040 KB |
最終ジャッジ日時 | 2024-09-25 16:34:30 |
合計ジャッジ時間 | 31,078 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 243 ms
6,944 KB |
testcase_08 | AC | 102 ms
6,940 KB |
testcase_09 | AC | 170 ms
6,940 KB |
testcase_10 | AC | 567 ms
7,040 KB |
testcase_11 | AC | 274 ms
6,940 KB |
testcase_12 | AC | 561 ms
6,940 KB |
testcase_13 | AC | 530 ms
6,944 KB |
testcase_14 | AC | 494 ms
6,940 KB |
testcase_15 | AC | 106 ms
6,948 KB |
testcase_16 | AC | 42 ms
6,940 KB |
testcase_17 | AC | 525 ms
6,940 KB |
testcase_18 | AC | 366 ms
6,940 KB |
testcase_19 | AC | 14 ms
6,940 KB |
testcase_20 | AC | 530 ms
6,940 KB |
testcase_21 | AC | 55 ms
6,940 KB |
testcase_22 | AC | 491 ms
6,940 KB |
testcase_23 | AC | 592 ms
7,040 KB |
testcase_24 | AC | 181 ms
6,940 KB |
testcase_25 | AC | 2 ms
6,944 KB |
testcase_26 | AC | 562 ms
7,040 KB |
testcase_27 | AC | 684 ms
7,040 KB |
testcase_28 | AC | 686 ms
7,040 KB |
testcase_29 | AC | 659 ms
7,040 KB |
testcase_30 | AC | 3 ms
6,940 KB |
testcase_31 | AC | 575 ms
6,944 KB |
testcase_32 | AC | 562 ms
6,944 KB |
testcase_33 | AC | 572 ms
6,944 KB |
testcase_34 | AC | 568 ms
6,944 KB |
testcase_35 | AC | 2 ms
6,944 KB |
testcase_36 | AC | 2 ms
6,940 KB |
testcase_37 | AC | 2 ms
6,940 KB |
testcase_38 | AC | 570 ms
6,944 KB |
testcase_39 | AC | 575 ms
7,040 KB |
testcase_40 | AC | 786 ms
7,040 KB |
testcase_41 | AC | 565 ms
7,040 KB |
testcase_42 | AC | 793 ms
6,940 KB |
testcase_43 | AC | 788 ms
7,040 KB |
testcase_44 | AC | 631 ms
7,040 KB |
testcase_45 | AC | 674 ms
7,040 KB |
testcase_46 | AC | 567 ms
7,040 KB |
testcase_47 | AC | 569 ms
7,040 KB |
testcase_48 | AC | 573 ms
7,040 KB |
testcase_49 | AC | 597 ms
6,944 KB |
testcase_50 | AC | 575 ms
7,040 KB |
testcase_51 | AC | 570 ms
7,040 KB |
testcase_52 | AC | 2 ms
6,940 KB |
testcase_53 | AC | 589 ms
6,948 KB |
testcase_54 | AC | 590 ms
6,944 KB |
testcase_55 | AC | 2 ms
6,940 KB |
testcase_56 | AC | 593 ms
6,944 KB |
testcase_57 | AC | 594 ms
6,944 KB |
testcase_58 | AC | 460 ms
6,940 KB |
testcase_59 | AC | 389 ms
7,040 KB |
testcase_60 | AC | 396 ms
7,040 KB |
testcase_61 | AC | 476 ms
7,040 KB |
testcase_62 | AC | 397 ms
7,040 KB |
testcase_63 | AC | 562 ms
7,040 KB |
testcase_64 | AC | 2 ms
6,944 KB |
testcase_65 | AC | 2 ms
6,940 KB |
testcase_66 | AC | 2 ms
6,940 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; // https://github.com/hos-lyric/libra/blob/master/algebra/modint.h template <unsigned M_> struct ModInt { static constexpr unsigned M = M_; unsigned x; constexpr ModInt() : x(0U) {} constexpr ModInt(unsigned x_) : x(x_ % M) {} constexpr ModInt(unsigned long long x_) : x(x_ % M) {} constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {} constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {} ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; } ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; } ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; } ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); } ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b; } ModInt inv() const { unsigned a = M, b = x; int y = 0, z = 1; for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; } assert(a == 1U); return ModInt(y); } ModInt operator+() const { return *this; } ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; } ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); } ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); } ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); } ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); } template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); } template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); } template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); } template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); } explicit operator bool() const { return x; } bool operator==(const ModInt &a) const { return (x == a.x); } bool operator!=(const ModInt &a) const { return (x != a.x); } friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; } }; // https://yukicoder.me/submissions/737488 // Editorial 解 #include <cassert> #include <iostream> #include <utility> #include <vector> using namespace std; // Library Checker Characteristic Polynomial https://judge.yosupo.jp/problem/characteristic_polynomial // 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]; } // Library Checker ここまで template <class T> std::vector<T> det_of_first_degree_mat(std::vector<std::vector<T>> M0, std::vector<std::vector<T>> M1) { const int N = M0.size(); int multiply_by_x = 0; // 「特定の列に x をかける」操作を行った回数 T detAdetBinv = 1; // 解説中の 1 / (det A det B) の値 for (int p = 0; p < N; ++p) { // M1[p][p] に nonzero を持ってきて、M1 の第 p 列を掃き出す int pivot = -1; for (int row = p; row < N; ++row) { if (M1[row][p] != T()) { pivot = row; break; } } if (pivot < 0) { ++multiply_by_x; if (multiply_by_x > N) return std::vector<T>(N + 1); // M1 の第 p 列で pivot が見つからなかった場合、M0 + x M1 の第 p 列に x をかけたい // かける前に M1 の第 p 列を第 1 ~ (p - 1) 列を使って掃き出して、 // x をかけた後で x の二次の項が出てこないようにする for (int row = 0; row < p; ++row) { T v = M1[row][p]; M1[row][p] = 0; for (int i = 0; i < N; ++i) M0[i][p] -= v * M0[i][row]; } for (int i = 0; i < N; ++i) swap(M0[i][p], M1[i][p]); --p; // 第 p 列をもう一度やり直す この処理は高々 N 回しか走らないので全体の計算量は O(n^3) が保たれる continue; } if (pivot != p) { M1[pivot].swap(M1[p]); M0[pivot].swap(M0[p]); detAdetBinv *= -1; } // p 行目を定数倍して M1[p][p] == 1 にする T v = M1[p][p], vinv = v.inv(); detAdetBinv *= v; for (int col = 0; col < N; ++col) { M0[p][col] *= vinv; M1[p][col] *= vinv; } // p 行目を使用して M1 の p 列目を p 行目以外ゼロにする for (int row = 0; row < N; ++row) { if (row == p) continue; T v = M1[row][p]; for (int col = 0; col < N; ++col) { M0[row][col] -= M0[p][col] * v; M1[row][col] -= M1[p][col] * v; } } } // この時点で M1 = I なので det(xI + M0) を求める for (auto &vec : M0) { for (auto &x : vec) x = -x; } auto poly = characteristic_poly(M0); for (auto &x : poly) x *= detAdetBinv; poly.erase(poly.begin(), poly.begin() + multiply_by_x); poly.resize(N + 1); return poly; } using mint = ModInt<998244353>; int main() { int n; cin >> n; vector<vector<mint>> M0(n, vector<mint>(n)), M1(n, vector<mint>(n)); for (auto& v : M0) { for (auto &x: v) { int x1; cin >> x1; x = x1; } } for (auto& v : M1) { for (auto& x : v) { int x1; cin >> x1; x = x1; } } for (auto v : det_of_first_degree_mat(M0, M1)) { cout << v << '\n'; } }