#include #ifdef LOCAL #include "debugger.hpp" #else #define dbg(...) #endif using i64 = long long; template 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 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 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

() const { return MLong

(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 constexpr MInt

CInv = MInt

(V).inv(); constexpr int P = 998244353; using Z = MInt

; std::vector rev; std::vector roots{0, 1}; void dft(std::vector &a) { int n = a.size(); if ((int)(rev.size()) != n) { int k = __builtin_ctz(n) - 1; rev.resize(n); for (int i = 0; i < n; i++) { rev[i] = rev[i >> 1] >> 1 | (i & 1) << k; } } for (int i = 0; i < n; i++) { if (rev[i] < i) { std::swap(a[i], a[rev[i]]); } } if ((int)(roots.size()) < n) { int k = __builtin_ctz(roots.size()); roots.resize(n); while ((1 << k) < n) { Z e = power(Z(Z::getRoot()), (Z::getMod() - 1) >> (k + 1)); for (int i = 1 << (k - 1); i < (1 << k); i++) { roots[2 * i] = roots[i]; roots[2 * i + 1] = roots[i] * e; } k++; } } for (int k = 1; k < n; k *= 2) { for (int i = 0; i < n; i += 2 * k) { for (int j = 0; j < k; j++) { Z u = a[i + j]; Z v = a[i + j + k] * roots[k + j]; a[i + j] = u + v; a[i + j + k] = u - v; } } } } void idft(std::vector &a) { int n = a.size(); std::reverse(a.begin() + 1, a.end()); dft(a); Z inv = (1 - Z::getMod()) / n; for (int i = 0; i < n; i++) { a[i] *= inv; } } template struct Poly { std::vector a; Poly() {} Poly(const std::vector &a) : a(a) {} Poly(const std::initializer_list &a) : a(a) {} int size() const { return a.size(); } void resize(int n) { a.resize(n); } T operator[](int idx) const { if (idx < size()) { return a[idx]; } else { return 0; } } T &operator[](int idx) { return a[idx]; } Poly mulxk(int k) const { auto b = a; b.insert(b.begin(), k, 0); return Poly(b); } Poly modxk(int k) const { k = std::min(k, size()); return Poly(std::vector(a.begin(), a.begin() + k)); } Poly divxk(int k) const { if (size() <= k) { return Poly(); } return Poly(std::vector(a.begin() + k, a.end())); } friend Poly operator+(const Poly &a, const Poly &b) { std::vector res(std::max(a.size(), b.size())); for (int i = 0; i < (int)(res.size()); i++) { res[i] = a[i] + b[i]; } return Poly(res); } friend Poly operator-(const Poly &a, const Poly &b) { std::vector res(std::max(a.size(), b.size())); for (int i = 0; i < (int)(res.size()); i++) { res[i] = a[i] - b[i]; } return Poly(res); } friend Poly operator*(Poly a, Poly b) { if (a.size() == 0 || b.size() == 0) { return Poly(); } int sz = 1, tot = a.size() + b.size() - 1; while (sz < tot) { sz *= 2; } a.a.resize(sz); b.a.resize(sz); dft(a.a); dft(b.a); for (int i = 0; i < sz; ++i) { a.a[i] = a[i] * b[i]; } idft(a.a); a.resize(tot); return a; } friend Poly operator*(T a, Poly b) { for (int i = 0; i < (int)(b.size()); i++) { b[i] *= a; } return b; } friend Poly operator*(Poly a, T b) { for (int i = 0; i < (int)(a.size()); i++) { a[i] *= b; } return a; } Poly &operator+=(Poly b) { return (*this) = (*this) + b; } Poly &operator-=(Poly b) { return (*this) = (*this) - b; } Poly &operator*=(Poly b) { return (*this) = (*this) * b; } Poly deriv() const { if (a.empty()) { return Poly(); } std::vector res(size() - 1); for (int i = 0; i < size() - 1; ++i) { res[i] = (i + 1) * a[i + 1]; } return Poly(res); } Poly integr() const { std::vector res(size() + 1); for (int i = 0; i < size(); ++i) { res[i + 1] = a[i] / (i + 1); } return Poly(res); } Poly inv(int m) const { Poly x{a[0].inv()}; int k = 1; while (k < m) { k *= 2; x = (x * (Poly{2} - modxk(k) * x)).modxk(k); } return x.modxk(m); } Poly log(int m) const { return (deriv() * inv(m)).integr().modxk(m); } Poly exp(int m) const { Poly x{1}; int k = 1; while (k < m) { k *= 2; x = (x * (Poly{1} - x.log(k) + modxk(k))).modxk(k); } return x.modxk(m); } Poly pow(int k, int m) const { int i = 0; while (i < size() && a[i].val() == 0) { i++; } if (i == size() || 1LL * i * k >= m) { return Poly(std::vector(m)); } T v = a[i]; auto f = divxk(i) * v.inv(); return (f.log(m - i * k) * k).exp(m - i * k).mulxk(i * k) * power(v, k); } Poly sqrt(int m) const { Poly x{1}; int k = 1; while (k < m) { k *= 2; x = (x + (modxk(k) * x.inv(k)).modxk(k)) * ((T::getMod() + 1) / 2); } return x.modxk(m); } Poly mulT(Poly b) const { if (b.size() == 0) { return Poly(); } int n = b.size(); std::reverse(b.a.begin(), b.a.end()); return ((*this) * b).divxk(n - 1); } std::vector eval(std::vector x) const { if (size() == 0) { return std::vector(x.size(), 0); } const int n = std::max((int)(x.size()), size()); std::vector q(4 * n); std::vector ans(x.size()); x.resize(n); std::function build = [&](int p, int l, int r) { if (r - l == 1) { q[p] = Poly{1, -x[l]}; } else { int m = (l + r) / 2; build(2 * p, l, m); build(2 * p + 1, m, r); q[p] = q[2 * p] * q[2 * p + 1]; } }; build(1, 0, n); std::function work = [&](int p, int l, int r, const Poly &num) { if (r - l == 1) { if (l < (int)(ans.size())) { ans[l] = num[0]; } } else { int m = (l + r) / 2; work(2 * p, l, m, num.mulT(q[2 * p + 1]).modxk(m - l)); work(2 * p + 1, m, r, num.mulT(q[2 * p]).modxk(r - m)); } }; work(1, 0, n, mulT(q[1].inv(n))); return ans; } }; template T count_minor(const std::vector> 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(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]) { // 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> &G) { int n = G.size(); std::vector> L(n, std::vector(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(L); } /// @param A square matrix of size n /// @return characteristic polynomial of A, which is `det(A - xI)` template Poly characteristic_polynomial(Matrix A) { const int n = A.size(); if (n == 0) return Poly{1}; assert(n == A[0].size()); for (int i = 1; i < n; ++i) { for (int k = i; k < n; ++k) if (A[k][i - 1] != T{}) { for (int j = 0; j < n; ++j) { std::swap(A[i][j], A[k][j]); } for (int j = 0; j < n; ++j) { std::swap(A[j][i], A[j][k]); } break; } if (A[i][i-1] == T{}) continue; for (int k = i + 1; k < n; ++k) if (A[k][i - 1] != T{}) { T z = A[k][i - 1] / A[i][i - 1]; for (int j = 0; j < n; ++j) A[k][j] -= A[i][j] * z; for (int j = 0; j < n; ++j) A[j][i] += A[j][k] * z; } } std::vector> P(n + 1); P[0] = Poly{1}; for (int i = 1; i <= n; ++i) { P[i].resize(i + 1); for (int j = 0; j < P[i-1].size(); ++j) { P[i][j + 1] += P[i - 1][j]; } T z = 1; for (int k = i; k >= 1; --k) { P[i] -= z * A[k - 1][i - 1] * P[k - 1]; if (k >= 2) z *= A[k - 1][k - 2]; } } return P[n]; } /// @brief calculate `det(Ax + B)`, where A and B are square matrices of size n /// @tparam T usually Z /// @tparam Matrix usually std::vector>, or custom matrix class /// @param A /// @param B /// @return `det(Ax + B)` template Poly linear_det(Matrix A, Matrix B) { const int n = A.size(); if (n == 0) return Poly{1}; assert(n == A[0].size()); assert(n == B.size()); assert(n == B[0].size()); int pay = 0; T coef = 1; auto go = [&](int start) -> int { for (int i = start; i < n; ++i) { int r = -1; for (int j = i; j < n; ++j) if (A[j][i] != T{}) { r = j; break; } if (r == -1) return i; if (r != i) { coef = -coef; for (int j = 0; j < n; ++j) { std::swap(A[i][j], A[r][j]); std::swap(B[i][j], B[r][j]); } } if (A[i][i] != T{1}) { T z = T{1} / A[i][i]; coef *= A[i][i]; for (int j = 0; j < n; ++j) { A[i][j] *= z; B[i][j] *= z; } } for (int k = 0; k < n; ++k) if (k != i && A[k][i] != T{}) { T z = A[k][i]; for (int j = 0; j < n; ++j) { A[k][j] -= A[i][j] * z; B[k][j] -= B[i][j] * z; } } } return n; }; auto zero_check = [](const Matrix& mat, int i0, int i1, int j0, int j1) -> bool { for (int i = i0; i <= i1; ++i) { for (int j = j0; j <= j1; ++j) { if (mat[i][j] != T{}) return false; } } return true; }; for (int at = 0, i = go(at); i < n; i = go(at)) { if (zero_check(A, 0, n - 1, i, i) && !zero_check(B, 0, n-1, i, i)) { ++pay; for (int j = 0; j < n; ++j) std::swap(A[j][i], B[j][i]); at = i; } else if (zero_check(A, i, i, 0, n-1) && !zero_check(B, i, i, 0, n-1)) { ++pay; for (int j = 0; j < n; ++j) std::swap(A[i][j], B[i][j]); at = 0; while (at < i && A[i][at] == T{}) ++at; } else { return Poly {}; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { B[i][j] = -B[i][j]; } } auto poly = characteristic_polynomial(B); if (pay > poly.size()) return Poly{}; if (pay) poly.a.erase(poly.a.begin(), poly.a.begin() + pay); for (T &x: poly.a) x *= coef; return poly; } void solv() { int n; std::cin >> n; std::vector> D1(n, std::vector(n)); auto D2 = D1; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { std::cin >> D2[i][j]; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { std::cin >> D1[i][j]; } } auto poly = linear_det(D1, D2); for (int i = 0; i <= n; ++i) { if (i >= poly.size()) { std::cout << "0\n"; } else { std::cout << poly[i] << '\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; }