Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ebi-fly13/Library

:heavy_check_mark: $f(a)$ (Set Power Series, f is FPS)
(set_function/poly_composite_sps.hpp)

説明

$f$ を形式的べき級数、 $a$ を $N$ 変数多項式とする。 $a$ を $f(x)$ に代入する。つまり、$f(a)$ を求める。 $O(N^2 2^N)$

Depends on

Verified with

Code

#pragma once

#include <cassert>
#include <vector>

#include "../set_function/egf_composite_sps.hpp"

namespace ebi {

template <class T, int LIM>
std::vector<T> poly_composite_sps(std::vector<T> a, const std::vector<T> &f) {
    int n = std::bit_width(a.size()) - 1;
    assert(n <= LIM);
    if (f.empty()) return std::vector<T>(1 << n, 0);
    int d = std::min((int)f.size() - 1, n);
    std::vector<T> g(d + 1);
    T c = a[0];
    a[0] = 0;
    std::vector<T> pow(d + 1);
    pow[0] = 1;
    for (int i = 0; i < (int)f.size(); i++) {
        for (int j = 0; j < d + 1; j++) g[j] += f[i] * pow[j];
        for (int j = d; j >= 0; j--)
            pow[j] = pow[j] * c + (j == 0 ? 0 : pow[j - 1]);
    }
    T fact = 1;
    for (int i = 0; i < d + 1; i++) {
        g[i] *= fact;
        fact *= (i + 1);
    }
    return egf_composite_sps<T, LIM>(a, g);
}

}  // namespace ebi
#line 2 "set_function/poly_composite_sps.hpp"

#include <cassert>
#include <vector>

#line 2 "set_function/egf_composite_sps.hpp"

#line 5 "set_function/egf_composite_sps.hpp"

#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
#line 7 "set_function/poly_composite_sps.hpp"

namespace ebi {

template <class T, int LIM>
std::vector<T> poly_composite_sps(std::vector<T> a, const std::vector<T> &f) {
    int n = std::bit_width(a.size()) - 1;
    assert(n <= LIM);
    if (f.empty()) return std::vector<T>(1 << n, 0);
    int d = std::min((int)f.size() - 1, n);
    std::vector<T> g(d + 1);
    T c = a[0];
    a[0] = 0;
    std::vector<T> pow(d + 1);
    pow[0] = 1;
    for (int i = 0; i < (int)f.size(); i++) {
        for (int j = 0; j < d + 1; j++) g[j] += f[i] * pow[j];
        for (int j = d; j >= 0; j--)
            pow[j] = pow[j] * c + (j == 0 ? 0 : pow[j - 1]);
    }
    T fact = 1;
    for (int i = 0; i < d + 1; i++) {
        g[i] *= fact;
        fact *= (i + 1);
    }
    return egf_composite_sps<T, LIM>(a, g);
}

}  // namespace ebi
Back to top page