sotanishy's competitive programming library

sotanishy's code snippets for competitive programming

View the Project on GitHub sotanishy/cp-library-cpp

:heavy_check_mark: Set Power Series
(math/set/set_power_series.hpp)

Depends on

Required by

Verified with

Code

#pragma once
#include <algorithm>
#include <array>
#include <cassert>
#include <vector>

#include "subset_convolution.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 2 "math/set/set_power_series.hpp"
#include <algorithm>
#include <array>
#include <cassert>
#include <vector>

#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;
    }
};
Back to top page