sotanishy's code snippets for competitive programming
View the Project on GitHub sotanishy/cp-library-cpp
#define PROBLEM "https://judge.yosupo.jp/problem/exp_of_set_power_series" #include <bits/stdc++.h> #include "../../math/modint.hpp" #include "../../math/set/set_power_series.hpp" using namespace std; using ll = long long; using mint = Modint<998244353>; int main() { int N; cin >> N; SetPowerSeries<mint, 20> b(1 << N); for (int i = 0; i < 1 << N; ++i) cin >> b[i]; auto c = b.exp(); for (int i = 0; i < 1 << N; ++i) cout << c[i] << (i < (1 << N) - 1 ? " " : "\n"); }
#line 1 "test/yosupo/exp_of_set_power_series.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/exp_of_set_power_series" #include <bits/stdc++.h> #line 4 "math/modint.hpp" /** * @brief Mod int */ template <int m> class Modint { using mint = Modint; static_assert(m > 0, "Modulus must be positive"); public: static constexpr int mod() { return m; } constexpr Modint(long long y = 0) : x(y >= 0 ? y % m : (y % m + m) % m) {} constexpr int val() const { return x; } constexpr mint& operator+=(const mint& r) { if ((x += r.x) >= m) x -= m; return *this; } constexpr mint& operator-=(const mint& r) { if ((x += m - r.x) >= m) x -= m; return *this; } constexpr mint& operator*=(const mint& r) { x = static_cast<int>(1LL * x * r.x % m); return *this; } constexpr mint& operator/=(const mint& r) { return *this *= r.inv(); } constexpr bool operator==(const mint& r) const { return x == r.x; } constexpr mint operator+() const { return *this; } constexpr mint operator-() const { return mint(-x); } constexpr friend mint operator+(const mint& l, const mint& r) { return mint(l) += r; } constexpr friend mint operator-(const mint& l, const mint& r) { return mint(l) -= r; } constexpr friend mint operator*(const mint& l, const mint& r) { return mint(l) *= r; } constexpr friend mint operator/(const mint& l, const mint& r) { return mint(l) /= r; } constexpr mint inv() const { int a = x, b = m, u = 1, v = 0; while (b > 0) { int t = a / b; std::swap(a -= t * b, b); std::swap(u -= t * v, v); } return mint(u); } constexpr mint pow(long long n) const { mint ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend std::ostream& operator<<(std::ostream& os, const mint& r) { return os << r.x; } friend std::istream& operator>>(std::istream& is, mint& r) { long long t; is >> t; r = mint(t); return is; } private: int x; }; #line 6 "math/set/set_power_series.hpp" #line 4 "math/set/subset_convolution.hpp" #line 2 "math/set/zeta_moebius_transform.hpp" #include <bit> #line 5 "math/set/zeta_moebius_transform.hpp" template <typename T> void superset_fzt(std::vector<T>& a) { assert(std::has_single_bit(a.size())); const int n = a.size(); for (int i = 1; i < n; i <<= 1) { for (int j = 0; j < n; ++j) { if (!(j & i)) a[j] += a[j | i]; } } } template <typename T> void superset_fmt(std::vector<T>& a) { assert(std::has_single_bit(a.size())); const int n = a.size(); for (int i = 1; i < n; i <<= 1) { for (int j = 0; j < n; ++j) { if (!(j & i)) a[j] -= a[j | i]; } } } template <typename T> void subset_fzt(std::vector<T>& a) { assert(std::has_single_bit(a.size())); const int n = a.size(); for (int i = 1; i < n; i <<= 1) { for (int j = 0; j < n; ++j) { if (!(j & i)) a[j | i] += a[j]; } } } template <typename T> void subset_fmt(std::vector<T>& a) { assert(std::has_single_bit(a.size())); const int n = a.size(); for (int i = 1; i < n; i <<= 1) { for (int j = 0; j < n; ++j) { if (!(j & i)) a[j | i] -= a[j]; } } } #line 6 "math/set/subset_convolution.hpp" template <typename T, std::size_t N> std::array<T, N>& operator+=(std::array<T, N>& lhs, const std::array<T, N>& rhs) { for (int i = 0; i < (int)N; ++i) lhs[i] += rhs[i]; return lhs; } template <typename T, std::size_t N> std::array<T, N>& operator-=(std::array<T, N>& lhs, const std::array<T, N>& rhs) { for (int i = 0; i < (int)N; ++i) lhs[i] -= rhs[i]; return lhs; } template <typename T, int N> std::vector<T> subset_convolution(const std::vector<T>& a, const std::vector<T>& b) { using Poly = std::array<T, N + 1>; const int n = std::bit_ceil(std::max(a.size(), b.size())); // convert to polynomials std::vector<Poly> pa(n), pb(n); for (int i = 0; i < (int)a.size(); ++i) { pa[i][std::popcount((unsigned int)i)] = a[i]; } for (int i = 0; i < (int)b.size(); ++i) { pb[i][std::popcount((unsigned int)i)] = b[i]; } // bitwise or convolution subset_fzt(pa); subset_fzt(pb); for (int i = 0; i < n; ++i) { Poly pc; for (int j = 0; j <= N; ++j) { for (int k = 0; k <= N - j; ++k) { pc[j + k] += pa[i][j] * pb[i][k]; } } pa[i].swap(pc); } subset_fmt(pa); // convert back std::vector<T> ret(n); for (int i = 0; i < n; ++i) { ret[i] = pa[i][std::popcount((unsigned int)i)]; } return ret; } #line 8 "math/set/set_power_series.hpp" /** * @brief Set Power Series */ template <typename mint, int N> class SetPowerSeries : public std::vector<mint> { using SPS = SetPowerSeries<mint, N>; using Poly = std::array<mint, N + 1>; public: using std::vector<mint>::vector; using std::vector<mint>::operator=; // -- binary operation with scalar --- SPS& operator+=(const mint& rhs) { if (this->empty()) this->resize(1); (*this)[0] += rhs; return *this; } SPS& operator-=(const mint& rhs) { if (this->empty()) this->resize(1); (*this)[0] -= rhs; return *this; } SPS& operator*=(const mint& rhs) { for (auto& x : *this) x *= rhs; return *this; } SPS& operator/=(const mint& rhs) { return *this *= rhs.inv(); } SPS operator+(const mint& rhs) const { return SPS(*this) += rhs; } SPS operator-(const mint& rhs) const { return SPS(*this) -= rhs; } SPS operator*(const mint& rhs) const { return SPS(*this) *= rhs; } SPS operator/(const mint& rhs) const { return SPS(*this) /= rhs; } // --- binary operation with SPS --- SPS& operator+=(const SPS& rhs) { if (this->size() < rhs.size()) this->resize(rhs.size()); for (int i = 0; i < (int)rhs.size(); ++i) (*this)[i] += rhs[i]; return *this; } SPS& operator-=(const SPS& rhs) { if (this->size() < rhs.size()) this->resize(rhs.size()); for (int i = 0; i < (int)rhs.size(); ++i) (*this)[i] -= rhs[i]; return *this; } SPS& operator*=(const SPS& rhs) { *this = subset_convolution<mint, N>(*this, rhs); return *this; } SPS operator+(const SPS& rhs) const { return SPS(*this) += rhs; } SPS operator-(const SPS& rhs) const { return SPS(*this) -= rhs; } SPS operator*(const SPS& rhs) const { return SPS(*this) *= rhs; } // --- compositions --- SPS exp() const { assert((*this)[0] == mint(0)); const int n = std::bit_width(std::bit_ceil(this->size())) - 1; SPS res(1 << n); res[0] = 1; for (int i = 0; i < n; ++i) { SPS a(this->begin() + (1 << i), this->begin() + (1 << (i + 1))); SPS b(res.begin(), res.begin() + (1 << i)); a *= b; std::copy(a.begin(), a.end(), res.begin() + (1 << i)); } return res; } }; #line 7 "test/yosupo/exp_of_set_power_series.test.cpp" using namespace std; using ll = long long; using mint = Modint<998244353>; int main() { int N; cin >> N; SetPowerSeries<mint, 20> b(1 << N); for (int i = 0; i < 1 << N; ++i) cin >> b[i]; auto c = b.exp(); for (int i = 0; i < 1 << N; ++i) cout << c[i] << (i < (1 << N) - 1 ? " " : "\n"); }