// 任意 mod での実行時間検討 #include #include #include #include #include using namespace std; struct ModIntRuntime { private: static int md; public: using lint = long long; static int mod() { return md; } int val; static std::vector &facs() { static std::vector facs_; return facs_; } static int &get_primitive_root() { static int primitive_root_ = 0; if (!primitive_root_) { primitive_root_ = [&]() { std::set 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 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 void hessenberg_reduction(std::vector> &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 std::vector characteristic_poly(std::vector> M) { hessenberg_reduction(M); const int N = M.size(); // p[i + 1] = (Characteristic polynomial of i-th leading principal minor) std::vector> 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(N)), M1(N, vector(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'; }