This documentation is automatically generated by online-judge-tools/verification-helper
#pragma once
#include <cassert>
#include <vector>
#include "../utility/bit_width.cpp"
#include "../utility/rep.cpp"
#include "ranked_subset_mobius_transform.cpp"
#include "ranked_subset_zeta_transform.cpp"
template <class R>
std::vector<typename R::Type> subset_convolution(const std::vector<typename R::Type>& a,
const std::vector<typename R::Type>& b) {
assert(a.size() == b.size());
const auto f = ranked_subset_zeta_transform<typename R::Sum>(a);
const auto g = ranked_subset_zeta_transform<typename R::Sum>(b);
const int n = a.size();
const int logn = bit_width(n);
std::vector<std::vector<typename R::Type>> h(n, std::vector<typename R::Type>(logn, R::Sum::identity()));
for (const int i : rep(n))
for (const int j : rep(logn))
for (const int k : rep(logn - j))
h[i][j + k] = R::Sum::operation(h[i][j + k], R::Product::operation(f[i][j], g[i][k]));
return ranked_subset_mobius_transform<typename R::Sum>(std::move(h));
}
#line 2 "algorithm/subset_convolution.cpp"
#include <cassert>
#include <vector>
#line 2 "internal/enable_avx2.cpp"
#ifdef ENABLE_AVX2
#define TARGET_AVX2 __attribute__((target("avx2")))
#else
#define TARGET_AVX2
#endif
#line 2 "utility/int_alias.cpp"
#include <cstdint>
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
#line 4 "utility/countl_zero.cpp"
TARGET_AVX2 constexpr int countl_zero(u64 x) {
#ifdef __GNUC__
return x == 0 ? 64 : __builtin_clzll(x);
#else
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
return 64 - countr_zero(~x);
#endif
}
#line 4 "utility/bit_width.cpp"
TARGET_AVX2 constexpr int bit_width(const u64 x) { return 64 - countl_zero(x); }
#line 2 "utility/rep.cpp"
#include <algorithm>
class Range {
struct Iter {
int itr;
constexpr Iter(const int pos) noexcept : itr(pos) {}
constexpr void operator++() noexcept { ++itr; }
constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
constexpr int operator*() const noexcept { return itr; }
};
const Iter first, last;
public:
explicit constexpr Range(const int first, const int last) noexcept : first(first), last(std::max(first, last)) {}
constexpr Iter begin() const noexcept { return first; }
constexpr Iter end() const noexcept { return last; }
};
constexpr Range rep(const int l, const int r) noexcept { return Range(l, r); }
constexpr Range rep(const int n) noexcept { return Range(0, n); }
#line 4 "utility/popcount.cpp"
TARGET_AVX2 constexpr int popcount(u64 x) {
#ifdef __GNUC__
return __builtin_popcountll(x);
#else
x -= x >> 1 & 0x5555555555555555;
x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F;
return x * 0x0101010101010101 >> 56 & 0x7f;
#endif
}
#line 7 "algorithm/ranked_subset_mobius_transform.cpp"
template <class G>
std::vector<typename G::Type> ranked_subset_mobius_transform(std::vector<std::vector<typename G::Type>> f) {
const int n = f.size();
assert((n & (n - 1)) == 0);
const int logn = bit_width(n);
for (int i = n; i >>= 1;) {
for (const int j : rep(n)) {
if (j & i) {
auto& a = f[j];
const auto& b = f[j & ~i];
for (const int k : rep(logn)) a[k] = G::operation(G::inverse(b[k]), a[k]);
}
}
}
std::vector<typename G::Type> g(n, G::identity());
for (const int i : rep(n)) g[i] = f[i][popcount(i)];
return g;
}
#line 7 "algorithm/ranked_subset_zeta_transform.cpp"
template <class S>
std::vector<std::vector<typename S::Type>> ranked_subset_zeta_transform(const std::vector<typename S::Type>& f) {
const int n = f.size();
assert((n & (n - 1)) == 0);
const int logn = bit_width(n);
std::vector<std::vector<typename S::Type>> g(n, std::vector<typename S::Type>(logn, S::identity()));
for (const int i : rep(n)) g[i][popcount(i)] = f[i];
for (int i = 1; i < n; i <<= 1) {
for (const int j : rep(n)) {
if (j & i) {
auto& a = g[j];
const auto& b = g[j & ~i];
for (const int k : rep(logn)) a[k] = S::operation(b[k], a[k]);
}
}
}
return g;
}
#line 8 "algorithm/subset_convolution.cpp"
template <class R>
std::vector<typename R::Type> subset_convolution(const std::vector<typename R::Type>& a,
const std::vector<typename R::Type>& b) {
assert(a.size() == b.size());
const auto f = ranked_subset_zeta_transform<typename R::Sum>(a);
const auto g = ranked_subset_zeta_transform<typename R::Sum>(b);
const int n = a.size();
const int logn = bit_width(n);
std::vector<std::vector<typename R::Type>> h(n, std::vector<typename R::Type>(logn, R::Sum::identity()));
for (const int i : rep(n))
for (const int j : rep(logn))
for (const int k : rep(logn - j))
h[i][j + k] = R::Sum::operation(h[i][j + k], R::Product::operation(f[i][j], g[i][k]));
return ranked_subset_mobius_transform<typename R::Sum>(std::move(h));
}