結果
問題 | No.1907 DETERMINATION |
ユーザー | flself |
提出日時 | 2023-10-08 23:49:10 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 18,799 bytes |
コンパイル時間 | 4,665 ms |
コンパイル使用メモリ | 272,936 KB |
実行使用メモリ | 13,888 KB |
最終ジャッジ日時 | 2024-07-26 18:15:23 |
合計ジャッジ時間 | 57,116 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
10,012 KB |
testcase_01 | AC | 2 ms
6,812 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 559 ms
6,940 KB |
testcase_08 | AC | 222 ms
6,944 KB |
testcase_09 | AC | 365 ms
6,944 KB |
testcase_10 | AC | 1,313 ms
6,940 KB |
testcase_11 | AC | 186 ms
6,940 KB |
testcase_12 | AC | 1,246 ms
7,168 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | AC | 74 ms
6,940 KB |
testcase_17 | AC | 1,186 ms
7,040 KB |
testcase_18 | AC | 824 ms
6,944 KB |
testcase_19 | AC | 19 ms
6,940 KB |
testcase_20 | AC | 1,207 ms
7,040 KB |
testcase_21 | AC | 104 ms
6,940 KB |
testcase_22 | AC | 1,190 ms
6,944 KB |
testcase_23 | AC | 1,438 ms
7,040 KB |
testcase_24 | AC | 383 ms
6,944 KB |
testcase_25 | AC | 2 ms
6,944 KB |
testcase_26 | AC | 1,287 ms
7,040 KB |
testcase_27 | AC | 1,605 ms
7,168 KB |
testcase_28 | AC | 1,602 ms
7,168 KB |
testcase_29 | AC | 1,592 ms
7,168 KB |
testcase_30 | AC | 2 ms
6,940 KB |
testcase_31 | AC | 1,320 ms
7,296 KB |
testcase_32 | AC | 1,304 ms
7,168 KB |
testcase_33 | AC | 1,264 ms
7,168 KB |
testcase_34 | AC | 1,276 ms
7,168 KB |
testcase_35 | AC | 2 ms
6,944 KB |
testcase_36 | AC | 2 ms
6,944 KB |
testcase_37 | AC | 2 ms
6,940 KB |
testcase_38 | AC | 1,279 ms
7,168 KB |
testcase_39 | AC | 1,306 ms
7,168 KB |
testcase_40 | AC | 2,007 ms
7,040 KB |
testcase_41 | AC | 1,271 ms
7,168 KB |
testcase_42 | AC | 2,015 ms
7,168 KB |
testcase_43 | AC | 1,996 ms
7,168 KB |
testcase_44 | AC | 1,518 ms
7,040 KB |
testcase_45 | AC | 1,645 ms
7,040 KB |
testcase_46 | AC | 1,250 ms
7,168 KB |
testcase_47 | AC | 1,265 ms
7,040 KB |
testcase_48 | AC | 1,266 ms
7,040 KB |
testcase_49 | AC | 1,363 ms
7,168 KB |
testcase_50 | AC | 1,263 ms
7,040 KB |
testcase_51 | AC | 1,268 ms
7,040 KB |
testcase_52 | AC | 2 ms
6,940 KB |
testcase_53 | AC | 855 ms
6,940 KB |
testcase_54 | AC | 854 ms
6,940 KB |
testcase_55 | TLE | - |
testcase_56 | -- | - |
testcase_57 | -- | - |
testcase_58 | -- | - |
testcase_59 | -- | - |
testcase_60 | -- | - |
testcase_61 | -- | - |
testcase_62 | -- | - |
testcase_63 | -- | - |
testcase_64 | -- | - |
testcase_65 | -- | - |
testcase_66 | -- | - |
ソースコード
#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>; std::vector<int> rev; std::vector<Z> roots{0, 1}; void dft(std::vector<Z> &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<Z> &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<typename T> struct Poly { std::vector<T> a; Poly() {} Poly(const std::vector<T> &a) : a(a) {} Poly(const std::initializer_list<T> &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<T>(a.begin(), a.begin() + k)); } Poly divxk(int k) const { if (size() <= k) { return Poly(); } return Poly(std::vector<T>(a.begin() + k, a.end())); } friend Poly operator+(const Poly &a, const Poly &b) { std::vector<T> 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<T> 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<T> 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<T> 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<T>(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<T> eval(std::vector<T> x) const { if (size() == 0) { return std::vector<T>(x.size(), 0); } const int n = std::max((int)(x.size()), size()); std::vector<Poly> q(4 * n); std::vector<T> ans(x.size()); x.resize(n); std::function<void(int, int, int)> 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<void(int, int, int, const Poly &)> 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<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]) { // 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(A - xI)` template<typename T, typename Matrix> Poly<T> characteristic_polynomial(Matrix A) { const int n = A.size(); if (n == 0) return Poly<T>{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<Poly<T>> P(n + 1); P[0] = Poly<T>{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<std::vector<T>>, or custom matrix class /// @param A /// @param B /// @return `det(Ax + B)` template<typename T, typename Matrix> Poly<T> linear_det(Matrix A, Matrix B) { const int n = A.size(); if (n == 0) return Poly<T>{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<T> {}; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { B[i][j] = -B[i][j]; } } auto poly = characteristic_polynomial<T>(B); if (pay > poly.size()) return Poly<T>{}; 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<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) { 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<Z>(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; }