This documentation is automatically generated by online-judge-tools/verification-helper
#include "setfunc/subset_convolution.hpp"#include "other/bit.hpp"
#include "setfunc/ranked_zeta.hpp"
template <typename T, int LIM = 20>
vc<T> subset_convolution_square(const vc<T>& A) {
auto RA = ranked_zeta<T, LIM>(A);
int n = topbit(len(RA));
FOR(s, len(RA)) {
auto& f = RA[s];
FOR_R(d, n + 1) {
T x = 0;
FOR(i, d + 1) x += f[i] * f[d - i];
f[d] = x;
}
}
return ranked_mobius<T, LIM>(RA);
}
template <typename T, int LIM = 20>
vc<T> subset_convolution(const vc<T>& A, const vc<T>& B) {
if (A == B) return subset_convolution_square(A);
auto RA = ranked_zeta<T, LIM>(A);
auto RB = ranked_zeta<T, LIM>(B);
int n = topbit(len(RA));
FOR(s, len(RA)) {
auto &f = RA[s], &g = RB[s];
FOR_R(d, n + 1) {
T x = 0;
FOR(i, d + 1) x += f[i] * g[d - i];
f[d] = x;
}
}
return ranked_mobius<T, LIM>(RA);
}#line 1 "other/bit.hpp"
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T>
T kth_bit(int k) {
return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
return x >> k & 1;
}
template <typename UINT>
struct all_bit {
UINT s;
all_bit(UINT s) : s(s) {}
struct iter {
UINT s;
int operator*() const { return lowbit(s); }
void operator++() { s &= s - 1; }
bool operator!=(nullptr_t) const { return s; }
};
iter begin() const { return {s}; }
nullptr_t end() const { return nullptr; }
};
template <typename UINT>
struct all_subset {
UINT s;
all_subset(UINT s) : s(s) {}
struct iter {
UINT s, t;
bool done = false;
UINT operator*() const { return t; }
void operator++() {
done = (t == 0);
t = (t - 1) & s;
}
bool operator!=(nullptr_t) const { return !done; }
};
iter begin() const { return {s, s}; }
nullptr_t end() const { return nullptr; }
};
constexpr u64 full_mask(int n) { return n == 64 ? -1ULL : (1ULL << n) - 1; }
u64 bit_reverse(u64 x) {
x = ((x & 0x5555555555555555ULL) << 1) | ((x >> 1) & 0x5555555555555555ULL);
x = ((x & 0x3333333333333333ULL) << 2) | ((x >> 2) & 0x3333333333333333ULL);
x = ((x & 0x0f0f0f0f0f0f0f0fULL) << 4) | ((x >> 4) & 0x0f0f0f0f0f0f0f0fULL);
x = ((x & 0x00ff00ff00ff00ffULL) << 8) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
x = ((x & 0x0000ffff0000ffffULL) << 16) | ((x >> 16) & 0x0000ffff0000ffffULL);
x = (x << 32) | (x >> 32);
return x;
}
#line 1 "setfunc/ranked_zeta.hpp"
#line 1 "setfunc/bitwise_transform.hpp"
namespace bitwise {
enum class trans_type {
hadamard,
superset_zeta,
superset_mobius,
subset_zeta,
subset_mobius,
ranked_zeta,
ranked_mobius,
superset_zeta_or
};
template <typename ARR>
inline void ranked_add(ARR& a, const ARR& b) {
for (int d = 0; d < int(a.size()); ++d) a[d] += b[d];
}
template <typename ARR>
inline void ranked_sub(ARR& a, const ARR& b) {
for (int d = 0; d < int(a.size()); ++d) a[d] -= b[d];
}
template <trans_type type, int N, typename T>
inline void bitwise_transform_fixed(T* a) {
static_assert(N >= 1 && (N & (N - 1)) == 0);
if constexpr (N == 1) {
return;
} else {
constexpr int H = N / 2;
bitwise_transform_fixed<type, H>(a);
bitwise_transform_fixed<type, H>(a + H);
if constexpr (type == trans_type::hadamard) {
for (int i = 0; i < H; ++i) {
auto x = a[i], y = a[H + i];
a[i] = x + y, a[H + i] = x - y;
}
}
if constexpr (type == trans_type::superset_zeta) {
for (int i = 0; i < H; ++i) a[i] += a[H + i];
}
if constexpr (type == trans_type::superset_mobius) {
for (int i = 0; i < H; ++i) a[i] -= a[H + i];
}
if constexpr (type == trans_type::subset_zeta) {
for (int i = 0; i < H; ++i) a[H + i] += a[i];
}
if constexpr (type == trans_type::subset_mobius) {
for (int i = 0; i < H; ++i) a[H + i] -= a[i];
}
if constexpr (type == trans_type::ranked_zeta) {
for (int i = 0; i < H; ++i) ranked_add(a[H + i], a[i]);
}
if constexpr (type == trans_type::ranked_mobius) {
for (int i = 0; i < H; ++i) ranked_sub(a[H + i], a[i]);
}
if constexpr (type == trans_type::superset_zeta_or) {
for (int i = 0; i < H; ++i) a[i] |= a[H + i];
}
}
}
template <trans_type type, int N, typename T>
inline void bitwise_transform_dispatch(vc<T>& a) {
if (len(a) == N) {
return bitwise_transform_fixed<type, N>(a.data());
}
if constexpr (N > 1) {
return bitwise_transform_dispatch<type, N / 2>(a);
}
}
template <trans_type type, typename T>
inline void bitwise_transform(vc<T>& a) {
int n = len(a);
assert(n >= 1);
assert((n & (n - 1)) == 0);
assert(n <= (1 << 25));
bitwise_transform_dispatch<type, 1 << 25>(a);
}
} // namespace bitwise
#line 3 "setfunc/ranked_zeta.hpp"
template <typename T, int LIM>
vc<array<T, LIM + 1>> ranked_zeta(const vc<T>& f) {
int n = topbit(len(f));
assert(n <= LIM);
assert(len(f) == 1 << n);
vc<array<T, LIM + 1>> Rf(1 << n);
for (int s = 0; s < (1 << n); ++s) Rf[s][popcnt(s)] = f[s];
bitwise::bitwise_transform<bitwise::trans_type::ranked_zeta>(Rf);
return Rf;
}
template <typename T, int LIM>
vc<T> ranked_mobius(vc<array<T, LIM + 1>>& Rf) {
bitwise::bitwise_transform<bitwise::trans_type::ranked_mobius>(Rf);
vc<T> f(len(Rf));
for (int s = 0; s < len(f); ++s) f[s] = Rf[s][popcnt(s)];
return f;
}
#line 3 "setfunc/subset_convolution.hpp"
template <typename T, int LIM = 20>
vc<T> subset_convolution_square(const vc<T>& A) {
auto RA = ranked_zeta<T, LIM>(A);
int n = topbit(len(RA));
FOR(s, len(RA)) {
auto& f = RA[s];
FOR_R(d, n + 1) {
T x = 0;
FOR(i, d + 1) x += f[i] * f[d - i];
f[d] = x;
}
}
return ranked_mobius<T, LIM>(RA);
}
template <typename T, int LIM = 20>
vc<T> subset_convolution(const vc<T>& A, const vc<T>& B) {
if (A == B) return subset_convolution_square(A);
auto RA = ranked_zeta<T, LIM>(A);
auto RB = ranked_zeta<T, LIM>(B);
int n = topbit(len(RA));
FOR(s, len(RA)) {
auto &f = RA[s], &g = RB[s];
FOR_R(d, n + 1) {
T x = 0;
FOR(i, d + 1) x += f[i] * g[d - i];
f[d] = x;
}
}
return ranked_mobius<T, LIM>(RA);
}