icpc_library

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

View the Project on GitHub ebi-fly13/icpc_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

Verified with

Code

#pragma once

#include "../template/template.hpp"
#include "../math/ranked_subset_transform.hpp"

namespace lib {

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"

#line 2 "template/template.hpp"

#include <bits/stdc++.h>

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <typename T> bool chmin(T &a, const T &b) {
    if (a <= b) return false;
    a = b;
    return true;
}
template <typename T> bool chmax(T &a, const T &b) {
    if (a >= b) return false;
    a = b;
    return true;
}

namespace lib {

using namespace std;

}  // namespace lib

// using namespace lib;
#line 2 "math/ranked_subset_transform.hpp"

#line 4 "math/ranked_subset_transform.hpp"

namespace lib {

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 5 "convolution/subset_convolution.hpp"

namespace lib {

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