sotanishy's competitive programming library

sotanishy's code snippets for competitive programming

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

:heavy_check_mark: Subset Convolution
(math/set/subset_convolution.hpp)

Description

数列 $f$ と $g$ の subset convolution $f * g$ は以下で定義される.

\[(f * g)(S) = \sum_{T \subset S} f(T) g(S\setminus T)\]

$f, g$ の長さを $2^n$ とするとき,素朴に部分集合を列挙する方法では計算量は $O(3^n)$ となるが,$O(n^2 2^n)$ で計算することができる.

Operations

Reference

Depends on

Required by

Verified with

Code

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

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

#line 2 "math/set/zeta_moebius_transform.hpp"
#include <bit>
#include <cassert>
#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;
}
Back to top page