Library

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

View the Project on GitHub ebi-fly13/Library

:heavy_check_mark: Subset Convolution
(convolution/subset_convolution.hpp)

説明

長さ $2^N$ の整数列 $a$ と $b$ について、 $c_k = \sum_{i\& j=0, i|j=k} a_i b_j$ を求める。 $O(N^2 2^N)$

Depends on

Required by

Verified with

Code

#pragma once

/*
    refernce: https://www.slideshare.net/wata_orz/ss-12131479
              https://37zigen.com/subset-convolution/
*/

#include <array>

#include <bit>

#include <cassert>

#include <vector>


#include "../set_function/ranked_subset_transform.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 2 "convolution/subset_convolution.hpp"

/*
    refernce: https://www.slideshare.net/wata_orz/ss-12131479
              https://37zigen.com/subset-convolution/
*/

#include <array>

#include <bit>

#include <cassert>

#include <vector>


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