This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ebi-fly13/Library
#include "set_function/egf_composite_sps.hpp"
$f$ を指数型母関数 (EGF)として、 集合べき級数 $a$ を $f(x)$ に代入する。つまり、$f(a)$ を求める。 $O(N^2 2^N)$
#pragma once #include <cassert> #include <vector> #include "../convolution/subset_convolution.hpp" namespace ebi { template <class T, int LIM> std::vector<T> egf_composite_sps(const std::vector<T> &a, std::vector<T> egf) { int n = std::bit_width(a.size()) - 1; assert(n <= LIM); assert((int)a.size() == (1 << n) && a[0] == T(0)); if ((int)egf.size() > n) egf.resize(n + 1); int d = egf.size() - 1; std::vector<T> f(1 << n); f[0] = egf[d]; for (int k = d - 1; k >= 0; k--) { std::vector<T> fk(1 << n); fk[0] = egf[k]; for (int i = 0; i < n - k; i++) { std::vector<T> s = {a.begin() + (1 << i), a.begin() + (2 << i)}; std::vector<T> t = {f.begin(), f.begin() + (1 << i)}; auto c = subset_convolution<T, LIM>(s, t); std::copy(c.begin(), c.end(), fk.begin() + (1 << i)); } f = fk; } return f; } } // namespace ebi
#line 2 "set_function/egf_composite_sps.hpp" #include <cassert> #include <vector> #line 2 "convolution/subset_convolution.hpp" /* refernce: https://www.slideshare.net/wata_orz/ss-12131479 https://37zigen.com/subset-convolution/ */ #include <array> #include <bit> #line 12 "convolution/subset_convolution.hpp" #line 2 "set_function/ranked_subset_transform.hpp" #line 7 "set_function/ranked_subset_transform.hpp" namespace ebi { template <class T, int LIM = 20> std::vector<std::array<T, LIM + 1>> ranked_zeta(const std::vector<T> &f) { int n = std::bit_width(f.size()) - 1; assert(n <= LIM); assert((int)f.size() == (1 << n)); std::vector<std::array<T, LIM + 1>> rf(1 << n); for (int s = 0; s < (1 << n); s++) rf[s][std::popcount((unsigned int)(s))] = f[s]; for (int i = 0; i < n; i++) { int w = 1 << i; for (int p = 0; p < (1 << n); p += 2 * w) { for (int s = p; s < p + w; s++) { int t = s | (1 << i); for (int d = 0; d <= n; d++) rf[t][d] += rf[s][d]; } } } return rf; } template <class T, int LIM = 20> std::vector<T> ranked_mobius(std::vector<std::array<T, LIM + 1>> rf) { int n = std::bit_width(rf.size()) - 1; assert((int)rf.size() == (1 << n)); for (int i = 0; i < n; i++) { int w = 1 << i; for (int p = 0; p < (1 << n); p += 2 * w) { for (int s = p; s < p + w; s++) { int t = s | (1 << i); for (int d = 0; d <= n; d++) rf[t][d] -= rf[s][d]; } } } std::vector<T> f(1 << n); for (int s = 0; s < (1 << n); s++) { f[s] = rf[s][std::popcount((unsigned int)(s))]; } return f; } } // namespace ebi #line 14 "convolution/subset_convolution.hpp" namespace ebi { template <class T, int LIM = 20> std::vector<T> subset_convolution(const std::vector<T> &a, const std::vector<T> &b) { auto ra = ranked_zeta<T, LIM>(a); auto rb = ranked_zeta<T, LIM>(b); int n = std::bit_width(a.size()) - 1; for (int s = (1 << n) - 1; s >= 0; s--) { auto &f = ra[s]; const auto &g = rb[s]; for (int d = n; d >= 0; d--) { T x = 0; for (int i = 0; i <= d; i++) { x += f[i] * g[d - i]; } f[d] = x; } } return ranked_mobius<T, LIM>(ra); } } // namespace ebi #line 7 "set_function/egf_composite_sps.hpp" namespace ebi { template <class T, int LIM> std::vector<T> egf_composite_sps(const std::vector<T> &a, std::vector<T> egf) { int n = std::bit_width(a.size()) - 1; assert(n <= LIM); assert((int)a.size() == (1 << n) && a[0] == T(0)); if ((int)egf.size() > n) egf.resize(n + 1); int d = egf.size() - 1; std::vector<T> f(1 << n); f[0] = egf[d]; for (int k = d - 1; k >= 0; k--) { std::vector<T> fk(1 << n); fk[0] = egf[k]; for (int i = 0; i < n - k; i++) { std::vector<T> s = {a.begin() + (1 << i), a.begin() + (2 << i)}; std::vector<T> t = {f.begin(), f.begin() + (1 << i)}; auto c = subset_convolution<T, LIM>(s, t); std::copy(c.begin(), c.end(), fk.begin() + (1 << i)); } f = fk; } return f; } } // namespace ebi