This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "cp-algo/math/subset_convolution.hpp"#ifndef CP_ALGO_MATH_SUBSET_CONVOLUTION_HPP
#define CP_ALGO_MATH_SUBSET_CONVOLUTION_HPP
#include "../util/simd.hpp"
#include "../util/big_alloc.hpp"
#include "../util/bit.hpp"
#include "../util/checkpoint.hpp"
#include <array>
#include <ranges>
#include <algorithm>
#include <bit>
#include <cstring>
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
#ifndef CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN
#define CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN 20
#endif
const size_t max_logn = CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN;
enum transform_dir { forw, inv };
template<auto N, transform_dir direction>
inline void xor_transform(auto &&a) {
if constexpr (N >> max_logn) {
throw std::runtime_error("N too large for xor_transform");
} else if constexpr (N <= 32) {
for (size_t i = 1; i < N; i *= 2) {
for (size_t j = 0; j < N; j += 2 * i) {
for (size_t k = j; k < j + i; k++) {
for (size_t z = 0; z < max_logn; z++) {
auto x = a[k][z] + a[k + i][z];
auto y = a[k][z] - a[k + i][z];
a[k][z] = x;
a[k + i][z] = y;
}
}
}
}
} else {
auto add = [&](auto &a, auto &b) __attribute__((always_inline)) {
auto x = a + b, y = a - b;
a = x, b = y;
};
constexpr auto quar = N / 4;
for (size_t i = 0; i < (size_t)quar; i++) {
auto x0 = a[i + (size_t)quar * 0];
auto x1 = a[i + (size_t)quar * 1];
auto x2 = a[i + (size_t)quar * 2];
auto x3 = a[i + (size_t)quar * 3];
#pragma GCC unroll max_logn
for (size_t z = 0; z < max_logn; z++) {
add(x0[z], x2[z]);
add(x1[z], x3[z]);
}
#pragma GCC unroll max_logn
for (size_t z = 0; z < max_logn; z++) {
add(x0[z], x1[z]);
add(x2[z], x3[z]);
}
a[i + (size_t)quar * 0] = x0;
a[i + (size_t)quar * 1] = x1;
a[i + (size_t)quar * 2] = x2;
a[i + (size_t)quar * 3] = x3;
}
xor_transform<quar, direction>(&a[quar * 0]);
xor_transform<quar, direction>(&a[quar * 1]);
xor_transform<quar, direction>(&a[quar * 2]);
xor_transform<quar, direction>(&a[quar * 3]);
}
}
template<transform_dir direction>
inline void xor_transform(auto &&a, auto n) {
with_bit_floor(n, [&]<auto NN>() {
assert(NN == n);
xor_transform<NN, direction>(a);
});
}
template<transform_dir direction = forw>
inline void xor_transform(auto &&a) {
xor_transform<direction>(a, std::size(a));
}
// Generic rank vectors processor with variadic inputs
// Assumes output[0] = 0, caller is responsible for handling rank 0
// Returns the output array
auto on_rank_vectors(auto &&cb, auto const& ...inputs) {
static_assert(sizeof...(inputs) >= 1, "on_rank_vectors requires at least one input");
// Create tuple of input references once
auto input_tuple = std::forward_as_tuple(inputs...);
auto const& first_input = std::get<0>(input_tuple);
using base = std::decay_t<decltype(first_input[0])>;
big_vector<base> out(std::size(first_input));
auto N = std::size(first_input);
constexpr size_t K = 4;
N = std::max(N, 2 * K);
const size_t n = std::bit_width(N) - 1;
const size_t T = std::min<size_t>(n - 3, 3);
const size_t bottoms = 1 << (n - T - 1);
const auto M = std::size(first_input);
// Create array buffers for each input
auto create_buffers = [bottoms]<typename... Args>(const Args&...) {
return std::make_tuple(
big_vector<std::array<typename std::decay_t<Args>::value_type, max_logn>>(bottoms)...
);
};
auto buffers = std::apply(create_buffers, input_tuple);
checkpoint("alloc buffers");
big_vector<uint32_t> counts(2 * bottoms);
for(size_t i = 1; i < 2 * bottoms; i++) {
counts[i] = (uint32_t)std::popcount(i);
}
checkpoint("prepare");
for(size_t top = 0; top < N / 2; top += bottoms) {
// Clear all buffers
std::apply([bottoms](auto&... bufs) {
(..., memset(bufs.data(), 0, sizeof(bufs[0]) * bottoms));
}, buffers);
checkpoint("memset");
// Initialize buffers from inputs
std::apply([&](auto const&... inps) {
std::apply([&](auto&... bufs) {
auto init_one = [&](auto const& inp, auto& buf) {
for(size_t i = 0; i < M; i += 2 * bottoms) {
bool parity = __builtin_parity(uint32_t((i >> 1) & top));
size_t limit = std::min(M, i + 2 * bottoms) - i;
uint32_t count = (uint32_t)std::popcount(i) - 1;
for(size_t bottom = (i == 0); bottom < limit; bottom++) {
if (parity) {
buf[bottom >> 1][count + counts[bottom]] -= inp[i + bottom];
} else {
buf[bottom >> 1][count + counts[bottom]] += inp[i + bottom];
}
}
}
};
(init_one(inps, bufs), ...);
}, buffers);
}, input_tuple);
checkpoint("init");
std::apply([](auto&... bufs) {
(..., xor_transform(bufs));
}, buffers);
checkpoint("transform");
assert(bottoms % K == 0);
for(size_t i = 0; i < bottoms; i += K) {
std::apply([&](auto&... bufs) {
auto extract_one = [&](auto& buf) {
std::array<u64x4, max_logn> aa;
for(size_t j = 0; j < max_logn; j++) {
for(size_t z = 0; z < K; z++) {
aa[j][z] = buf[i + z][j].getr();
}
}
return aa;
};
auto aa_tuple = std::make_tuple(extract_one(bufs)...);
std::apply(cb, aa_tuple);
// Write results back: only first array needs to be written
auto& first_buf = std::get<0>(std::forward_as_tuple(bufs...));
const auto& first_aa = std::get<0>(aa_tuple);
for(size_t j = 0; j < max_logn; j++) {
for(size_t z = 0; z < K; z++) {
first_buf[i + z][j].setr((uint32_t)first_aa[j][z]);
}
}
}, buffers);
}
checkpoint("dot");
auto& first_buf = std::get<0>(buffers);
xor_transform<inv>(first_buf);
checkpoint("transform");
// Gather results from first buffer
for(size_t i = 0; i < M; i += 2 * bottoms) {
bool parity = __builtin_parity(uint32_t((i >> 1) & top));
size_t limit = std::min(M, i + 2 * bottoms) - i;
uint32_t count = (uint32_t)std::popcount(i) - 1;
for(size_t bottom = (i == 0); bottom < limit; bottom++) {
if (parity) {
out[i + bottom] -= first_buf[bottom >> 1][count + counts[bottom]];
} else {
out[i + bottom] += first_buf[bottom >> 1][count + counts[bottom]];
}
}
}
checkpoint("gather");
}
const base ni = base(N / 2).inv();
for(auto& x : out) {x *= ni;}
return out;
}
template<typename base>
big_vector<base> subset_convolution(std::span<base> f, std::span<base> g) {
big_vector<base> outpa;
constexpr size_t lgn = max_logn;
outpa = on_rank_vectors([](auto &a, auto const& b) {
std::decay_t<decltype(a)> res = {};
const auto mod = base::mod();
const auto imod = math::inv2(-mod);
const auto r4 = u64x4() + uint64_t(-1) % mod + 1;
auto add = [&](size_t i) {
if constexpr (lgn) for(size_t j = 0; i + j + 1 < lgn; j++) {
res[i + j + 1] += (u64x4)_mm256_mul_epu32(__m256i(a[i]), __m256i(b[j]));
}
if constexpr (lgn >= 20) if (i == 15) {
for(size_t k = 0; k < lgn; k++) {
res[k] = res[k] >= base::modmod8() ? res[k] - base::modmod8() : res[k];
}
}
};
if constexpr (lgn) for(size_t i = 0; i < lgn; i++) { add(i); }
if constexpr (lgn) if constexpr (lgn) for(size_t k = 0; k < lgn; k++) {
res[k] = montgomery_reduce(res[k], mod, imod);
res[k] = montgomery_mul(res[k], r4, mod, imod);
a[k] = res[k] >= mod ? res[k] - mod : res[k];
}
}, f, g);
outpa[0] = f[0] * g[0];
for(size_t i = 1; i < std::size(f); i++) {
outpa[i] += f[i] * g[0] + f[0] * g[i];
}
checkpoint("fix 0");
return outpa;
}
template<typename base>
big_vector<base> subset_exp(std::span<base> g) {
if (size(g) == 1) {
return big_vector<base>{1};
}
size_t N = std::size(g);
auto out0 = subset_exp(std::span(g).first(N / 2));
auto out1 = subset_convolution<base>(out0, std::span(g).last(N / 2));
out0.insert(end(out0), begin(out1), end(out1));
cp_algo::checkpoint("extend out");
return out0;
}
template<typename base>
big_vector<big_vector<base>> subset_compose(std::span<base> f, std::span<base> g, size_t n) {
if (size(g) == 1) {
size_t M = size(f);
big_vector res(n, big_vector<base>{0});
big_vector<base> pw(std::max(n, M));
pw[0] = 1;
for (size_t j = 1; j < M; j++) {
pw[j] = pw[j - 1] * g[0];
}
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < M; j++) {
res[i][0] += pw[j] * f[j];
}
for (size_t j = M; j > i; j--) {
pw[j] = pw[j - 1] * base(j);
}
pw[i] = 0;
}
cp_algo::checkpoint("base case");
return res;
}
size_t N = std::size(g);
auto deeper = subset_compose(f, std::span(g).first(N / 2), n + 1);
for(size_t i = 0; i + 1 < size(deeper); i++) {
auto next = subset_convolution<base>(deeper[i + 1], std::span(g).last(N / 2));
deeper[i].insert(end(deeper[i]), begin(next), end(next));
}
deeper.pop_back();
cp_algo::checkpoint("combine");
return deeper;
}
template<typename base>
big_vector<base> subset_compose(std::span<base> f, std::span<base> g) {
return subset_compose(f, g, 1)[0];
}
// Transpose of f -> f * g = h
template<typename base>
big_vector<base> subset_conv_transpose(std::span<base> h, std::span<base> g) {
std::ranges::reverse(h);
auto res = subset_convolution<base>(h, g);
std::ranges::reverse(h);
std::ranges::reverse(res);
return res;
}
template<typename base>
big_vector<base> subset_power_projection(big_vector<big_vector<base>> &&fg, std::span<base> g, size_t M) {
if (size(g) == 1) {
size_t n = size(fg);
big_vector<base> res(M);
big_vector<base> pw(std::max(n, M));
pw[0] = 1;
for (size_t j = 1; j < M; j++) {
pw[j] = pw[j - 1] * g[0];
}
for (size_t i = 0; i < size(fg); i++) {
for (size_t j = 0; j < M; j++) {
res[j] += pw[j] * fg[i][0];
}
for (size_t j = M; j > i; j--) {
pw[j] = pw[j - 1] * base(j);
}
pw[i] = 0;
}
cp_algo::checkpoint("base case");
return res;
}
size_t N = std::size(g);
fg.emplace_back(N / 2);
for(auto&& [i, h]: fg | std::views::enumerate | std::views::reverse | std::views::drop(1)) {
auto prev = subset_conv_transpose<base>(std::span(h).last(N / 2), std::span(g).last(N / 2));
for (size_t j = 0; j < N / 2; j++) {
fg[i + 1][j] += prev[j];
}
fg[i + 1].resize(N / 2);
}
fg[0].resize(N / 2);
cp_algo::checkpoint("decombine");
return subset_power_projection(std::move(fg), std::span(g).first(N / 2), M);
}
template<typename base>
big_vector<base> subset_power_projection(std::span<base> g, std::span<base> w, size_t M) {
return subset_power_projection({{begin(w), end(w)}}, g, M);
}
}
#pragma GCC pop_options
#endif // CP_ALGO_MATH_SUBSET_CONVOLUTION_HPP
#line 1 "cp-algo/math/subset_convolution.hpp"
#line 1 "cp-algo/util/simd.hpp"
#include <experimental/simd>
#include <cstdint>
#include <cstddef>
#include <memory>
#if defined(__x86_64__) && !defined(CP_ALGO_DISABLE_AVX2)
#define CP_ALGO_SIMD_AVX2_TARGET _Pragma("GCC target(\"avx2\")")
#else
#define CP_ALGO_SIMD_AVX2_TARGET
#endif
#define CP_ALGO_SIMD_PRAGMA_PUSH \
_Pragma("GCC push_options") \
CP_ALGO_SIMD_AVX2_TARGET
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo {
template<typename T, size_t len>
using simd [[gnu::vector_size(len * sizeof(T))]] = T;
using u64x8 = simd<uint64_t, 8>;
using u32x16 = simd<uint32_t, 16>;
using i64x4 = simd<int64_t, 4>;
using u64x4 = simd<uint64_t, 4>;
using u32x8 = simd<uint32_t, 8>;
using u16x16 = simd<uint16_t, 16>;
using i32x4 = simd<int32_t, 4>;
using u32x4 = simd<uint32_t, 4>;
using u16x8 = simd<uint16_t, 8>;
using u16x4 = simd<uint16_t, 4>;
using i16x4 = simd<int16_t, 4>;
using u8x32 = simd<uint8_t, 32>;
using u8x8 = simd<uint8_t, 8>;
using u8x4 = simd<uint8_t, 4>;
using dx4 = simd<double, 4>;
inline dx4 abs(dx4 a) {
return dx4{
std::abs(a[0]),
std::abs(a[1]),
std::abs(a[2]),
std::abs(a[3])
};
}
// https://stackoverflow.com/a/77376595
// works for ints in (-2^51, 2^51)
static constexpr dx4 magic = dx4() + (3ULL << 51);
inline i64x4 lround(dx4 x) {
return i64x4(x + magic) - i64x4(magic);
}
inline dx4 to_double(i64x4 x) {
return dx4(x + i64x4(magic)) - magic;
}
inline dx4 round(dx4 a) {
return dx4{
std::nearbyint(a[0]),
std::nearbyint(a[1]),
std::nearbyint(a[2]),
std::nearbyint(a[3])
};
}
inline u64x4 low32(u64x4 x) {
return x & uint32_t(-1);
}
inline auto swap_bytes(auto x) {
return decltype(x)(__builtin_shufflevector(u32x8(x), u32x8(x), 1, 0, 3, 2, 5, 4, 7, 6));
}
inline u64x4 montgomery_reduce(u64x4 x, uint32_t mod, uint32_t imod) {
#ifdef __AVX2__
auto x_ninv = u64x4(_mm256_mul_epu32(__m256i(x), __m256i() + imod));
x += u64x4(_mm256_mul_epu32(__m256i(x_ninv), __m256i() + mod));
#else
auto x_ninv = u64x4(u32x8(low32(x)) * imod);
x += x_ninv * uint64_t(mod);
#endif
return swap_bytes(x);
}
inline u64x4 montgomery_mul(u64x4 x, u64x4 y, uint32_t mod, uint32_t imod) {
#ifdef __AVX2__
return montgomery_reduce(u64x4(_mm256_mul_epu32(__m256i(x), __m256i(y))), mod, imod);
#else
return montgomery_reduce(x * y, mod, imod);
#endif
}
inline u32x8 montgomery_mul(u32x8 x, u32x8 y, uint32_t mod, uint32_t imod) {
return u32x8(montgomery_mul(u64x4(x), u64x4(y), mod, imod)) |
u32x8(swap_bytes(montgomery_mul(u64x4(swap_bytes(x)), u64x4(swap_bytes(y)), mod, imod)));
}
inline dx4 rotate_right(dx4 x) {
static constexpr u64x4 shuffler = {3, 0, 1, 2};
return __builtin_shuffle(x, shuffler);
}
template<std::size_t Align = 32>
inline bool is_aligned(const auto* p) noexcept {
return (reinterpret_cast<std::uintptr_t>(p) % Align) == 0;
}
template<class Target>
inline Target& vector_cast(auto &&p) {
return *reinterpret_cast<Target*>(std::assume_aligned<alignof(Target)>(&p));
}
}
#pragma GCC pop_options
#line 1 "cp-algo/util/big_alloc.hpp"
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#line 12 "cp-algo/util/big_alloc.hpp"
#include <iostream>
#include <generator>
#include <forward_list>
// Single macro to detect POSIX platforms (Linux, Unix, macOS)
#if defined(__linux__) || defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
# define CP_ALGO_USE_MMAP 1
# include <sys/mman.h>
#else
# define CP_ALGO_USE_MMAP 0
#endif
namespace cp_algo {
template <typename T, size_t Align = 32>
class big_alloc {
static_assert( Align >= alignof(void*), "Align must be at least pointer-size");
static_assert(std::popcount(Align) == 1, "Align must be a power of two");
public:
using value_type = T;
template <class U> struct rebind { using other = big_alloc<U, Align>; };
constexpr bool operator==(const big_alloc&) const = default;
constexpr bool operator!=(const big_alloc&) const = default;
big_alloc() noexcept = default;
template <typename U, std::size_t A>
big_alloc(const big_alloc<U, A>&) noexcept {}
[[nodiscard]] T* allocate(std::size_t n) {
std::size_t padded = round_up(n * sizeof(T));
std::size_t align = std::max<std::size_t>(alignof(T), Align);
#if CP_ALGO_USE_MMAP
if (padded >= MEGABYTE) {
void* raw = mmap(nullptr, padded,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
madvise(raw, padded, MADV_HUGEPAGE);
madvise(raw, padded, MADV_POPULATE_WRITE);
return static_cast<T*>(raw);
}
#endif
return static_cast<T*>(::operator new(padded, std::align_val_t(align)));
}
void deallocate(T* p, std::size_t n) noexcept {
if (!p) return;
std::size_t padded = round_up(n * sizeof(T));
std::size_t align = std::max<std::size_t>(alignof(T), Align);
#if CP_ALGO_USE_MMAP
if (padded >= MEGABYTE) { munmap(p, padded); return; }
#endif
::operator delete(p, padded, std::align_val_t(align));
}
private:
static constexpr std::size_t MEGABYTE = 1 << 20;
static constexpr std::size_t round_up(std::size_t x) noexcept {
return (x + Align - 1) / Align * Align;
}
};
template<typename T> using big_vector = std::vector<T, big_alloc<T>>;
template<typename T> using big_basic_string = std::basic_string<T, std::char_traits<T>, big_alloc<T>>;
template<typename T> using big_deque = std::deque<T, big_alloc<T>>;
template<typename T> using big_stack = std::stack<T, big_deque<T>>;
template<typename T> using big_queue = std::queue<T, big_deque<T>>;
template<typename T> using big_priority_queue = std::priority_queue<T, big_vector<T>>;
template<typename T> using big_forward_list = std::forward_list<T, big_alloc<T>>;
using big_string = big_basic_string<char>;
template<typename Key, typename Value, typename Compare = std::less<Key>>
using big_map = std::map<Key, Value, Compare, big_alloc<std::pair<const Key, Value>>>;
template<typename T, typename Compare = std::less<T>>
using big_multiset = std::multiset<T, Compare, big_alloc<T>>;
template<typename T, typename Compare = std::less<T>>
using big_set = std::set<T, Compare, big_alloc<T>>;
template<typename Ref, typename V = void>
using big_generator = std::generator<Ref, V, big_alloc<std::byte>>;
}
// Deduction guide to make elements_of with big_generator default to big_alloc
namespace std::ranges {
template<typename Ref, typename V>
elements_of(cp_algo::big_generator<Ref, V>&&) -> elements_of<cp_algo::big_generator<Ref, V>&&, cp_algo::big_alloc<std::byte>>;
}
#line 1 "cp-algo/util/bit.hpp"
#line 6 "cp-algo/util/bit.hpp"
#include <array>
#include <bit>
#if defined(__x86_64__) && !defined(CP_ALGO_DISABLE_AVX2)
#define CP_ALGO_BIT_OPS_TARGET _Pragma("GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")")
#else
#define CP_ALGO_BIT_OPS_TARGET _Pragma("GCC target(\"bmi,bmi2,lzcnt,popcnt\")")
#endif
#define CP_ALGO_BIT_PRAGMA_PUSH \
_Pragma("GCC push_options") \
CP_ALGO_BIT_OPS_TARGET
CP_ALGO_BIT_PRAGMA_PUSH
namespace cp_algo {
template<typename Uint>
constexpr size_t bit_width = sizeof(Uint) * 8;
// n < 64
uint64_t mask(size_t n) {
return (1ULL << n) - 1;
}
size_t order_of_bit(auto x, size_t k) {
return k ? std::popcount(x << (bit_width<decltype(x)> - k)) : 0;
}
inline size_t kth_set_bit(uint64_t x, size_t k) {
return std::countr_zero(_pdep_u64(1ULL << k, x));
}
template<int fl = 0>
void with_bit_floor(size_t n, auto &&callback) {
if constexpr (fl >= 63) {
return;
} else if (n >> (fl + 1)) {
with_bit_floor<fl + 1>(n, callback);
} else {
callback.template operator()<1ULL << fl>();
}
}
void with_bit_ceil(size_t n, auto &&callback) {
with_bit_floor(n, [&]<size_t N>() {
if(N == n) {
callback.template operator()<N>();
} else {
callback.template operator()<N << 1>();
}
});
}
inline uint32_t read_bits(char const* p) {
return _mm256_movemask_epi8(__m256i(vector_cast<u8x32 const>(p[0]) + (127 - '0')));
}
inline uint64_t read_bits64(char const* p) {
return read_bits(p) | (uint64_t(read_bits(p + 32)) << 32);
}
inline void write_bits(char *p, uint32_t bits) {
static constexpr u8x32 shuffler = {
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3
};
auto shuffled = u8x32(_mm256_shuffle_epi8(__m256i() + bits, __m256i(shuffler)));
static constexpr u8x32 mask = {
1, 2, 4, 8, 16, 32, 64, 128,
1, 2, 4, 8, 16, 32, 64, 128,
1, 2, 4, 8, 16, 32, 64, 128,
1, 2, 4, 8, 16, 32, 64, 128
};
for(int z = 0; z < 32; z++) {
p[z] = shuffled[z] & mask[z] ? '1' : '0';
}
}
inline void write_bits64(char *p, uint64_t bits) {
write_bits(p, uint32_t(bits));
write_bits(p + 32, uint32_t(bits >> 32));
}
}
#pragma GCC pop_options
#line 1 "cp-algo/util/checkpoint.hpp"
#line 5 "cp-algo/util/checkpoint.hpp"
#include <chrono>
#line 8 "cp-algo/util/checkpoint.hpp"
namespace cp_algo {
#ifdef CP_ALGO_CHECKPOINT
big_map<big_string, double> checkpoints;
double last;
#endif
template<bool final = false>
void checkpoint([[maybe_unused]] auto const& _msg) {
#ifdef CP_ALGO_CHECKPOINT
big_string msg = _msg;
double now = (double)clock() / CLOCKS_PER_SEC;
double delta = now - last;
last = now;
if(msg.size() && !final) {
checkpoints[msg] += delta;
}
if(final) {
for(auto const& [key, value] : checkpoints) {
std::cerr << key << ": " << value * 1000 << " ms\n";
}
std::cerr << "Total: " << now * 1000 << " ms\n";
}
#endif
}
template<bool final = false>
void checkpoint() {
checkpoint<final>("");
}
}
#line 8 "cp-algo/math/subset_convolution.hpp"
#include <ranges>
#include <algorithm>
#line 11 "cp-algo/math/subset_convolution.hpp"
#include <cstring>
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
#ifndef CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN
#define CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN 20
#endif
const size_t max_logn = CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN;
enum transform_dir { forw, inv };
template<auto N, transform_dir direction>
inline void xor_transform(auto &&a) {
if constexpr (N >> max_logn) {
throw std::runtime_error("N too large for xor_transform");
} else if constexpr (N <= 32) {
for (size_t i = 1; i < N; i *= 2) {
for (size_t j = 0; j < N; j += 2 * i) {
for (size_t k = j; k < j + i; k++) {
for (size_t z = 0; z < max_logn; z++) {
auto x = a[k][z] + a[k + i][z];
auto y = a[k][z] - a[k + i][z];
a[k][z] = x;
a[k + i][z] = y;
}
}
}
}
} else {
auto add = [&](auto &a, auto &b) __attribute__((always_inline)) {
auto x = a + b, y = a - b;
a = x, b = y;
};
constexpr auto quar = N / 4;
for (size_t i = 0; i < (size_t)quar; i++) {
auto x0 = a[i + (size_t)quar * 0];
auto x1 = a[i + (size_t)quar * 1];
auto x2 = a[i + (size_t)quar * 2];
auto x3 = a[i + (size_t)quar * 3];
#pragma GCC unroll max_logn
for (size_t z = 0; z < max_logn; z++) {
add(x0[z], x2[z]);
add(x1[z], x3[z]);
}
#pragma GCC unroll max_logn
for (size_t z = 0; z < max_logn; z++) {
add(x0[z], x1[z]);
add(x2[z], x3[z]);
}
a[i + (size_t)quar * 0] = x0;
a[i + (size_t)quar * 1] = x1;
a[i + (size_t)quar * 2] = x2;
a[i + (size_t)quar * 3] = x3;
}
xor_transform<quar, direction>(&a[quar * 0]);
xor_transform<quar, direction>(&a[quar * 1]);
xor_transform<quar, direction>(&a[quar * 2]);
xor_transform<quar, direction>(&a[quar * 3]);
}
}
template<transform_dir direction>
inline void xor_transform(auto &&a, auto n) {
with_bit_floor(n, [&]<auto NN>() {
assert(NN == n);
xor_transform<NN, direction>(a);
});
}
template<transform_dir direction = forw>
inline void xor_transform(auto &&a) {
xor_transform<direction>(a, std::size(a));
}
// Generic rank vectors processor with variadic inputs
// Assumes output[0] = 0, caller is responsible for handling rank 0
// Returns the output array
auto on_rank_vectors(auto &&cb, auto const& ...inputs) {
static_assert(sizeof...(inputs) >= 1, "on_rank_vectors requires at least one input");
// Create tuple of input references once
auto input_tuple = std::forward_as_tuple(inputs...);
auto const& first_input = std::get<0>(input_tuple);
using base = std::decay_t<decltype(first_input[0])>;
big_vector<base> out(std::size(first_input));
auto N = std::size(first_input);
constexpr size_t K = 4;
N = std::max(N, 2 * K);
const size_t n = std::bit_width(N) - 1;
const size_t T = std::min<size_t>(n - 3, 3);
const size_t bottoms = 1 << (n - T - 1);
const auto M = std::size(first_input);
// Create array buffers for each input
auto create_buffers = [bottoms]<typename... Args>(const Args&...) {
return std::make_tuple(
big_vector<std::array<typename std::decay_t<Args>::value_type, max_logn>>(bottoms)...
);
};
auto buffers = std::apply(create_buffers, input_tuple);
checkpoint("alloc buffers");
big_vector<uint32_t> counts(2 * bottoms);
for(size_t i = 1; i < 2 * bottoms; i++) {
counts[i] = (uint32_t)std::popcount(i);
}
checkpoint("prepare");
for(size_t top = 0; top < N / 2; top += bottoms) {
// Clear all buffers
std::apply([bottoms](auto&... bufs) {
(..., memset(bufs.data(), 0, sizeof(bufs[0]) * bottoms));
}, buffers);
checkpoint("memset");
// Initialize buffers from inputs
std::apply([&](auto const&... inps) {
std::apply([&](auto&... bufs) {
auto init_one = [&](auto const& inp, auto& buf) {
for(size_t i = 0; i < M; i += 2 * bottoms) {
bool parity = __builtin_parity(uint32_t((i >> 1) & top));
size_t limit = std::min(M, i + 2 * bottoms) - i;
uint32_t count = (uint32_t)std::popcount(i) - 1;
for(size_t bottom = (i == 0); bottom < limit; bottom++) {
if (parity) {
buf[bottom >> 1][count + counts[bottom]] -= inp[i + bottom];
} else {
buf[bottom >> 1][count + counts[bottom]] += inp[i + bottom];
}
}
}
};
(init_one(inps, bufs), ...);
}, buffers);
}, input_tuple);
checkpoint("init");
std::apply([](auto&... bufs) {
(..., xor_transform(bufs));
}, buffers);
checkpoint("transform");
assert(bottoms % K == 0);
for(size_t i = 0; i < bottoms; i += K) {
std::apply([&](auto&... bufs) {
auto extract_one = [&](auto& buf) {
std::array<u64x4, max_logn> aa;
for(size_t j = 0; j < max_logn; j++) {
for(size_t z = 0; z < K; z++) {
aa[j][z] = buf[i + z][j].getr();
}
}
return aa;
};
auto aa_tuple = std::make_tuple(extract_one(bufs)...);
std::apply(cb, aa_tuple);
// Write results back: only first array needs to be written
auto& first_buf = std::get<0>(std::forward_as_tuple(bufs...));
const auto& first_aa = std::get<0>(aa_tuple);
for(size_t j = 0; j < max_logn; j++) {
for(size_t z = 0; z < K; z++) {
first_buf[i + z][j].setr((uint32_t)first_aa[j][z]);
}
}
}, buffers);
}
checkpoint("dot");
auto& first_buf = std::get<0>(buffers);
xor_transform<inv>(first_buf);
checkpoint("transform");
// Gather results from first buffer
for(size_t i = 0; i < M; i += 2 * bottoms) {
bool parity = __builtin_parity(uint32_t((i >> 1) & top));
size_t limit = std::min(M, i + 2 * bottoms) - i;
uint32_t count = (uint32_t)std::popcount(i) - 1;
for(size_t bottom = (i == 0); bottom < limit; bottom++) {
if (parity) {
out[i + bottom] -= first_buf[bottom >> 1][count + counts[bottom]];
} else {
out[i + bottom] += first_buf[bottom >> 1][count + counts[bottom]];
}
}
}
checkpoint("gather");
}
const base ni = base(N / 2).inv();
for(auto& x : out) {x *= ni;}
return out;
}
template<typename base>
big_vector<base> subset_convolution(std::span<base> f, std::span<base> g) {
big_vector<base> outpa;
constexpr size_t lgn = max_logn;
outpa = on_rank_vectors([](auto &a, auto const& b) {
std::decay_t<decltype(a)> res = {};
const auto mod = base::mod();
const auto imod = math::inv2(-mod);
const auto r4 = u64x4() + uint64_t(-1) % mod + 1;
auto add = [&](size_t i) {
if constexpr (lgn) for(size_t j = 0; i + j + 1 < lgn; j++) {
res[i + j + 1] += (u64x4)_mm256_mul_epu32(__m256i(a[i]), __m256i(b[j]));
}
if constexpr (lgn >= 20) if (i == 15) {
for(size_t k = 0; k < lgn; k++) {
res[k] = res[k] >= base::modmod8() ? res[k] - base::modmod8() : res[k];
}
}
};
if constexpr (lgn) for(size_t i = 0; i < lgn; i++) { add(i); }
if constexpr (lgn) if constexpr (lgn) for(size_t k = 0; k < lgn; k++) {
res[k] = montgomery_reduce(res[k], mod, imod);
res[k] = montgomery_mul(res[k], r4, mod, imod);
a[k] = res[k] >= mod ? res[k] - mod : res[k];
}
}, f, g);
outpa[0] = f[0] * g[0];
for(size_t i = 1; i < std::size(f); i++) {
outpa[i] += f[i] * g[0] + f[0] * g[i];
}
checkpoint("fix 0");
return outpa;
}
template<typename base>
big_vector<base> subset_exp(std::span<base> g) {
if (size(g) == 1) {
return big_vector<base>{1};
}
size_t N = std::size(g);
auto out0 = subset_exp(std::span(g).first(N / 2));
auto out1 = subset_convolution<base>(out0, std::span(g).last(N / 2));
out0.insert(end(out0), begin(out1), end(out1));
cp_algo::checkpoint("extend out");
return out0;
}
template<typename base>
big_vector<big_vector<base>> subset_compose(std::span<base> f, std::span<base> g, size_t n) {
if (size(g) == 1) {
size_t M = size(f);
big_vector res(n, big_vector<base>{0});
big_vector<base> pw(std::max(n, M));
pw[0] = 1;
for (size_t j = 1; j < M; j++) {
pw[j] = pw[j - 1] * g[0];
}
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < M; j++) {
res[i][0] += pw[j] * f[j];
}
for (size_t j = M; j > i; j--) {
pw[j] = pw[j - 1] * base(j);
}
pw[i] = 0;
}
cp_algo::checkpoint("base case");
return res;
}
size_t N = std::size(g);
auto deeper = subset_compose(f, std::span(g).first(N / 2), n + 1);
for(size_t i = 0; i + 1 < size(deeper); i++) {
auto next = subset_convolution<base>(deeper[i + 1], std::span(g).last(N / 2));
deeper[i].insert(end(deeper[i]), begin(next), end(next));
}
deeper.pop_back();
cp_algo::checkpoint("combine");
return deeper;
}
template<typename base>
big_vector<base> subset_compose(std::span<base> f, std::span<base> g) {
return subset_compose(f, g, 1)[0];
}
// Transpose of f -> f * g = h
template<typename base>
big_vector<base> subset_conv_transpose(std::span<base> h, std::span<base> g) {
std::ranges::reverse(h);
auto res = subset_convolution<base>(h, g);
std::ranges::reverse(h);
std::ranges::reverse(res);
return res;
}
template<typename base>
big_vector<base> subset_power_projection(big_vector<big_vector<base>> &&fg, std::span<base> g, size_t M) {
if (size(g) == 1) {
size_t n = size(fg);
big_vector<base> res(M);
big_vector<base> pw(std::max(n, M));
pw[0] = 1;
for (size_t j = 1; j < M; j++) {
pw[j] = pw[j - 1] * g[0];
}
for (size_t i = 0; i < size(fg); i++) {
for (size_t j = 0; j < M; j++) {
res[j] += pw[j] * fg[i][0];
}
for (size_t j = M; j > i; j--) {
pw[j] = pw[j - 1] * base(j);
}
pw[i] = 0;
}
cp_algo::checkpoint("base case");
return res;
}
size_t N = std::size(g);
fg.emplace_back(N / 2);
for(auto&& [i, h]: fg | std::views::enumerate | std::views::reverse | std::views::drop(1)) {
auto prev = subset_conv_transpose<base>(std::span(h).last(N / 2), std::span(g).last(N / 2));
for (size_t j = 0; j < N / 2; j++) {
fg[i + 1][j] += prev[j];
}
fg[i + 1].resize(N / 2);
}
fg[0].resize(N / 2);
cp_algo::checkpoint("decombine");
return subset_power_projection(std::move(fg), std::span(g).first(N / 2), M);
}
template<typename base>
big_vector<base> subset_power_projection(std::span<base> g, std::span<base> w, size_t M) {
return subset_power_projection({{begin(w), end(w)}}, g, M);
}
}
#pragma GCC pop_options
#ifndef CP_ALGO_MATH_SUBSET_CONVOLUTION_HPP
#define CP_ALGO_MATH_SUBSET_CONVOLUTION_HPP
#include "../util/simd.hpp"
#include "../util/big_alloc.hpp"
#include "../util/bit.hpp"
#include "../util/checkpoint.hpp"
#include <array>
#include <ranges>
#include <algorithm>
#include <bit>
#include <cstring>
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{
#ifndef CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN
#define CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN 20
#endif
const size_t max_logn=CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN;enum transform_dir{forw,inv};template<auto N,transform_dir direction>inline void xor_transform(auto&&a){if constexpr(N>>max_logn){throw std::runtime_error("N too large for xor_transform");}else if constexpr(N<=32){for(size_t i=1;i<N;i*=2){for(size_t j=0;j<N;j+=2*i){for(size_t k=j;k<j+i;k++){for(size_t z=0;z<max_logn;z++){auto x=a[k][z]+a[k+i][z];auto y=a[k][z]-a[k+i][z];a[k][z]=x;a[k+i][z]=y;}}}}}else{auto add=[&](auto&a,auto&b)__attribute__((always_inline)){auto x=a+b,y=a-b;a=x,b=y;};constexpr auto quar=N/4;for(size_t i=0;i<(size_t)quar;i++){auto x0=a[i+(size_t)quar*0];auto x1=a[i+(size_t)quar*1];auto x2=a[i+(size_t)quar*2];auto x3=a[i+(size_t)quar*3];
#pragma GCC unroll max_logn
for(size_t z=0;z<max_logn;z++){add(x0[z],x2[z]);add(x1[z],x3[z]);}
#pragma GCC unroll max_logn
for(size_t z=0;z<max_logn;z++){add(x0[z],x1[z]);add(x2[z],x3[z]);}a[i+(size_t)quar*0]=x0;a[i+(size_t)quar*1]=x1;a[i+(size_t)quar*2]=x2;a[i+(size_t)quar*3]=x3;}xor_transform<quar,direction>(&a[quar*0]);xor_transform<quar,direction>(&a[quar*1]);xor_transform<quar,direction>(&a[quar*2]);xor_transform<quar,direction>(&a[quar*3]);}}template<transform_dir direction>inline void xor_transform(auto&&a,auto n){with_bit_floor(n,[&]<auto NN>(){assert(NN==n);xor_transform<NN,direction>(a);});}template<transform_dir direction=forw>inline void xor_transform(auto&&a){xor_transform<direction>(a,std::size(a));}auto on_rank_vectors(auto&&cb,auto const&...inputs){static_assert(sizeof...(inputs)>=1,"on_rank_vectors requires at least one input");auto input_tuple=std::forward_as_tuple(inputs...);auto const&first_input=std::get<0>(input_tuple);using base=std::decay_t<decltype(first_input[0])>;big_vector<base>out(std::size(first_input));auto N=std::size(first_input);constexpr size_t K=4;N=std::max(N,2*K);const size_t n=std::bit_width(N)-1;const size_t T=std::min<size_t>(n-3,3);const size_t bottoms=1<<(n-T-1);const auto M=std::size(first_input);auto create_buffers=[bottoms]<typename... Args>(const Args&...){return std::make_tuple(big_vector<std::array<typename std::decay_t<Args>::value_type,max_logn>>(bottoms)...);};auto buffers=std::apply(create_buffers,input_tuple);checkpoint("alloc buffers");big_vector<uint32_t>counts(2*bottoms);for(size_t i=1;i<2*bottoms;i++){counts[i]=(uint32_t)std::popcount(i);}checkpoint("prepare");for(size_t top=0;top<N/2;top+=bottoms){std::apply([bottoms](auto&... bufs){(...,memset(bufs.data(),0,sizeof(bufs[0])*bottoms));},buffers);checkpoint("memset");std::apply([&](auto const&... inps){std::apply([&](auto&... bufs){auto init_one=[&](auto const&inp,auto&buf){for(size_t i=0;i<M;i+=2*bottoms){bool parity=__builtin_parity(uint32_t((i>>1)&top));size_t limit=std::min(M,i+2*bottoms)-i;uint32_t count=(uint32_t)std::popcount(i)-1;for(size_t bottom=(i==0);bottom<limit;bottom++){if(parity){buf[bottom>>1][count+counts[bottom]]-=inp[i+bottom];}else{buf[bottom>>1][count+counts[bottom]]+=inp[i+bottom];}}}};(init_one(inps,bufs),...);},buffers);},input_tuple);checkpoint("init");std::apply([](auto&... bufs){(...,xor_transform(bufs));},buffers);checkpoint("transform");assert(bottoms%K==0);for(size_t i=0;i<bottoms;i+=K){std::apply([&](auto&... bufs){auto extract_one=[&](auto&buf){std::array<u64x4,max_logn>aa;for(size_t j=0;j<max_logn;j++){for(size_t z=0;z<K;z++){aa[j][z]=buf[i+z][j].getr();}}return aa;};auto aa_tuple=std::make_tuple(extract_one(bufs)...);std::apply(cb,aa_tuple);auto&first_buf=std::get<0>(std::forward_as_tuple(bufs...));const auto&first_aa=std::get<0>(aa_tuple);for(size_t j=0;j<max_logn;j++){for(size_t z=0;z<K;z++){first_buf[i+z][j].setr((uint32_t)first_aa[j][z]);}}},buffers);}checkpoint("dot");auto&first_buf=std::get<0>(buffers);xor_transform<inv>(first_buf);checkpoint("transform");for(size_t i=0;i<M;i+=2*bottoms){bool parity=__builtin_parity(uint32_t((i>>1)&top));size_t limit=std::min(M,i+2*bottoms)-i;uint32_t count=(uint32_t)std::popcount(i)-1;for(size_t bottom=(i==0);bottom<limit;bottom++){if(parity){out[i+bottom]-=first_buf[bottom>>1][count+counts[bottom]];}else{out[i+bottom]+=first_buf[bottom>>1][count+counts[bottom]];}}}checkpoint("gather");}const base ni=base(N/2).inv();for(auto&x:out){x*=ni;}return out;}template<typename base>big_vector<base>subset_convolution(std::span<base>f,std::span<base>g){big_vector<base>outpa;constexpr size_t lgn=max_logn;outpa=on_rank_vectors([](auto&a,auto const&b){std::decay_t<decltype(a)>res={};const auto mod=base::mod();const auto imod=math::inv2(-mod);const auto r4=u64x4()+uint64_t(-1)%mod+1;auto add=[&](size_t i){if constexpr(lgn)for(size_t j=0;i+j+1<lgn;j++){res[i+j+1]+=(u64x4)_mm256_mul_epu32(__m256i(a[i]),__m256i(b[j]));}if constexpr(lgn>=20)if(i==15){for(size_t k=0;k<lgn;k++){res[k]=res[k]>=base::modmod8()?res[k]-base::modmod8():res[k];}}};if constexpr(lgn)for(size_t i=0;i<lgn;i++){add(i);}if constexpr(lgn)if constexpr(lgn)for(size_t k=0;k<lgn;k++){res[k]=montgomery_reduce(res[k],mod,imod);res[k]=montgomery_mul(res[k],r4,mod,imod);a[k]=res[k]>=mod?res[k]-mod:res[k];}},f,g);outpa[0]=f[0]*g[0];for(size_t i=1;i<std::size(f);i++){outpa[i]+=f[i]*g[0]+f[0]*g[i];}checkpoint("fix 0");return outpa;}template<typename base>big_vector<base>subset_exp(std::span<base>g){if(size(g)==1){return big_vector<base>{1};}size_t N=std::size(g);auto out0=subset_exp(std::span(g).first(N/2));auto out1=subset_convolution<base>(out0,std::span(g).last(N/2));out0.insert(end(out0),begin(out1),end(out1));cp_algo::checkpoint("extend out");return out0;}template<typename base>big_vector<big_vector<base>>subset_compose(std::span<base>f,std::span<base>g,size_t n){if(size(g)==1){size_t M=size(f);big_vector res(n,big_vector<base>{0});big_vector<base>pw(std::max(n,M));pw[0]=1;for(size_t j=1;j<M;j++){pw[j]=pw[j-1]*g[0];}for(size_t i=0;i<n;i++){for(size_t j=0;j<M;j++){res[i][0]+=pw[j]*f[j];}for(size_t j=M;j>i;j--){pw[j]=pw[j-1]*base(j);}pw[i]=0;}cp_algo::checkpoint("base case");return res;}size_t N=std::size(g);auto deeper=subset_compose(f,std::span(g).first(N/2),n+1);for(size_t i=0;i+1<size(deeper);i++){auto next=subset_convolution<base>(deeper[i+1],std::span(g).last(N/2));deeper[i].insert(end(deeper[i]),begin(next),end(next));}deeper.pop_back();cp_algo::checkpoint("combine");return deeper;}template<typename base>big_vector<base>subset_compose(std::span<base>f,std::span<base>g){return subset_compose(f,g,1)[0];}template<typename base>big_vector<base>subset_conv_transpose(std::span<base>h,std::span<base>g){std::ranges::reverse(h);auto res=subset_convolution<base>(h,g);std::ranges::reverse(h);std::ranges::reverse(res);return res;}template<typename base>big_vector<base>subset_power_projection(big_vector<big_vector<base>>&&fg,std::span<base>g,size_t M){if(size(g)==1){size_t n=size(fg);big_vector<base>res(M);big_vector<base>pw(std::max(n,M));pw[0]=1;for(size_t j=1;j<M;j++){pw[j]=pw[j-1]*g[0];}for(size_t i=0;i<size(fg);i++){for(size_t j=0;j<M;j++){res[j]+=pw[j]*fg[i][0];}for(size_t j=M;j>i;j--){pw[j]=pw[j-1]*base(j);}pw[i]=0;}cp_algo::checkpoint("base case");return res;}size_t N=std::size(g);fg.emplace_back(N/2);for(auto&&[i,h]:fg|std::views::enumerate|std::views::reverse|std::views::drop(1)){auto prev=subset_conv_transpose<base>(std::span(h).last(N/2),std::span(g).last(N/2));for(size_t j=0;j<N/2;j++){fg[i+1][j]+=prev[j];}fg[i+1].resize(N/2);}fg[0].resize(N/2);cp_algo::checkpoint("decombine");return subset_power_projection(std::move(fg),std::span(g).first(N/2),M);}template<typename base>big_vector<base>subset_power_projection(std::span<base>g,std::span<base>w,size_t M){return subset_power_projection({{begin(w),end(w)}},g,M);}}
#pragma GCC pop_options
#endif
#line 1 "cp-algo/math/subset_convolution.hpp"
#line 1 "cp-algo/util/simd.hpp"
#include <experimental/simd>
#include <cstdint>
#include <cstddef>
#include <memory>
#if defined(__x86_64__) && !defined(CP_ALGO_DISABLE_AVX2)
#define CP_ALGO_SIMD_AVX2_TARGET _Pragma("GCC target(\"avx2\")")
#else
#define CP_ALGO_SIMD_AVX2_TARGET
#endif
#define CP_ALGO_SIMD_PRAGMA_PUSH _Pragma("GCC push_options") CP_ALGO_SIMD_AVX2_TARGET
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo{template<typename T,size_t len>using simd[[gnu::vector_size(len*sizeof(T))]]=T;using u64x8=simd<uint64_t,8>;using u32x16=simd<uint32_t,16>;using i64x4=simd<int64_t,4>;using u64x4=simd<uint64_t,4>;using u32x8=simd<uint32_t,8>;using u16x16=simd<uint16_t,16>;using i32x4=simd<int32_t,4>;using u32x4=simd<uint32_t,4>;using u16x8=simd<uint16_t,8>;using u16x4=simd<uint16_t,4>;using i16x4=simd<int16_t,4>;using u8x32=simd<uint8_t,32>;using u8x8=simd<uint8_t,8>;using u8x4=simd<uint8_t,4>;using dx4=simd<double,4>;inline dx4 abs(dx4 a){return dx4{std::abs(a[0]),std::abs(a[1]),std::abs(a[2]),std::abs(a[3])};}static constexpr dx4 magic=dx4()+(3ULL<<51);inline i64x4 lround(dx4 x){return i64x4(x+magic)-i64x4(magic);}inline dx4 to_double(i64x4 x){return dx4(x+i64x4(magic))-magic;}inline dx4 round(dx4 a){return dx4{std::nearbyint(a[0]),std::nearbyint(a[1]),std::nearbyint(a[2]),std::nearbyint(a[3])};}inline u64x4 low32(u64x4 x){return x&uint32_t(-1);}inline auto swap_bytes(auto x){return decltype(x)(__builtin_shufflevector(u32x8(x),u32x8(x),1,0,3,2,5,4,7,6));}inline u64x4 montgomery_reduce(u64x4 x,uint32_t mod,uint32_t imod){
#ifdef __AVX2__
auto x_ninv=u64x4(_mm256_mul_epu32(__m256i(x),__m256i()+imod));x+=u64x4(_mm256_mul_epu32(__m256i(x_ninv),__m256i()+mod));
#else
auto x_ninv=u64x4(u32x8(low32(x))*imod);x+=x_ninv*uint64_t(mod);
#endif
return swap_bytes(x);}inline u64x4 montgomery_mul(u64x4 x,u64x4 y,uint32_t mod,uint32_t imod){
#ifdef __AVX2__
return montgomery_reduce(u64x4(_mm256_mul_epu32(__m256i(x),__m256i(y))),mod,imod);
#else
return montgomery_reduce(x*y,mod,imod);
#endif
}inline u32x8 montgomery_mul(u32x8 x,u32x8 y,uint32_t mod,uint32_t imod){return u32x8(montgomery_mul(u64x4(x),u64x4(y),mod,imod))|u32x8(swap_bytes(montgomery_mul(u64x4(swap_bytes(x)),u64x4(swap_bytes(y)),mod,imod)));}inline dx4 rotate_right(dx4 x){static constexpr u64x4 shuffler={3,0,1,2};return __builtin_shuffle(x,shuffler);}template<std::size_t Align=32>inline bool is_aligned(const auto*p)noexcept{return(reinterpret_cast<std::uintptr_t>(p)%Align)==0;}template<class Target>inline Target&vector_cast(auto&&p){return*reinterpret_cast<Target*>(std::assume_aligned<alignof(Target)>(&p));}}
#pragma GCC pop_options
#line 1 "cp-algo/util/big_alloc.hpp"
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#line 12 "cp-algo/util/big_alloc.hpp"
#include <iostream>
#include <generator>
#include <forward_list>
#if defined(__linux__) || defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
# define CP_ALGO_USE_MMAP 1
# include <sys/mman.h>
#else
# define CP_ALGO_USE_MMAP 0
#endif
namespace cp_algo{template<typename T,size_t Align=32>class big_alloc{static_assert(Align>=alignof(void*),"Align must be at least pointer-size");static_assert(std::popcount(Align)==1,"Align must be a power of two");public:using value_type=T;template<class U>struct rebind{using other=big_alloc<U,Align>;};constexpr bool operator==(const big_alloc&)const=default;constexpr bool operator!=(const big_alloc&)const=default;big_alloc()noexcept=default;template<typename U,std::size_t A>big_alloc(const big_alloc<U,A>&)noexcept{}[[nodiscard]]T*allocate(std::size_t n){std::size_t padded=round_up(n*sizeof(T));std::size_t align=std::max<std::size_t>(alignof(T),Align);
#if CP_ALGO_USE_MMAP
if(padded>=MEGABYTE){void*raw=mmap(nullptr,padded,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0);madvise(raw,padded,MADV_HUGEPAGE);madvise(raw,padded,MADV_POPULATE_WRITE);return static_cast<T*>(raw);}
#endif
return static_cast<T*>(::operator new(padded,std::align_val_t(align)));}void deallocate(T*p,std::size_t n)noexcept{if(!p)return;std::size_t padded=round_up(n*sizeof(T));std::size_t align=std::max<std::size_t>(alignof(T),Align);
#if CP_ALGO_USE_MMAP
if(padded>=MEGABYTE){munmap(p,padded);return;}
#endif
::operator delete(p,padded,std::align_val_t(align));}private:static constexpr std::size_t MEGABYTE=1<<20;static constexpr std::size_t round_up(std::size_t x)noexcept{return(x+Align-1)/Align*Align;}};template<typename T>using big_vector=std::vector<T,big_alloc<T>>;template<typename T>using big_basic_string=std::basic_string<T,std::char_traits<T>,big_alloc<T>>;template<typename T>using big_deque=std::deque<T,big_alloc<T>>;template<typename T>using big_stack=std::stack<T,big_deque<T>>;template<typename T>using big_queue=std::queue<T,big_deque<T>>;template<typename T>using big_priority_queue=std::priority_queue<T,big_vector<T>>;template<typename T>using big_forward_list=std::forward_list<T,big_alloc<T>>;using big_string=big_basic_string<char>;template<typename Key,typename Value,typename Compare=std::less<Key>>using big_map=std::map<Key,Value,Compare,big_alloc<std::pair<const Key,Value>>>;template<typename T,typename Compare=std::less<T>>using big_multiset=std::multiset<T,Compare,big_alloc<T>>;template<typename T,typename Compare=std::less<T>>using big_set=std::set<T,Compare,big_alloc<T>>;template<typename Ref,typename V=void>using big_generator=std::generator<Ref,V,big_alloc<std::byte>>;}namespace std::ranges{template<typename Ref,typename V>elements_of(cp_algo::big_generator<Ref,V>&&)->elements_of<cp_algo::big_generator<Ref,V>&&,cp_algo::big_alloc<std::byte>>;}
#line 1 "cp-algo/util/bit.hpp"
#line 6 "cp-algo/util/bit.hpp"
#include <array>
#include <bit>
#if defined(__x86_64__) && !defined(CP_ALGO_DISABLE_AVX2)
#define CP_ALGO_BIT_OPS_TARGET _Pragma("GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")")
#else
#define CP_ALGO_BIT_OPS_TARGET _Pragma("GCC target(\"bmi,bmi2,lzcnt,popcnt\")")
#endif
#define CP_ALGO_BIT_PRAGMA_PUSH _Pragma("GCC push_options") CP_ALGO_BIT_OPS_TARGET
CP_ALGO_BIT_PRAGMA_PUSH
namespace cp_algo{template<typename Uint>constexpr size_t bit_width=sizeof(Uint)*8;uint64_t mask(size_t n){return(1ULL<<n)-1;}size_t order_of_bit(auto x,size_t k){return k?std::popcount(x<<(bit_width<decltype(x)>-k)):0;}inline size_t kth_set_bit(uint64_t x,size_t k){return std::countr_zero(_pdep_u64(1ULL<<k,x));}template<int fl=0>void with_bit_floor(size_t n,auto&&callback){if constexpr(fl>=63){return;}else if(n>>(fl+1)){with_bit_floor<fl+1>(n,callback);}else{callback.template operator()<1ULL<<fl>();}}void with_bit_ceil(size_t n,auto&&callback){with_bit_floor(n,[&]<size_t N>(){if(N==n){callback.template operator()<N>();}else{callback.template operator()<N<<1>();}});}inline uint32_t read_bits(char const*p){return _mm256_movemask_epi8(__m256i(vector_cast<u8x32 const>(p[0])+(127-'0')));}inline uint64_t read_bits64(char const*p){return read_bits(p)|(uint64_t(read_bits(p+32))<<32);}inline void write_bits(char*p,uint32_t bits){static constexpr u8x32 shuffler={0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3};auto shuffled=u8x32(_mm256_shuffle_epi8(__m256i()+bits,__m256i(shuffler)));static constexpr u8x32 mask={1,2,4,8,16,32,64,128,1,2,4,8,16,32,64,128,1,2,4,8,16,32,64,128,1,2,4,8,16,32,64,128};for(int z=0;z<32;z++){p[z]=shuffled[z]&mask[z]?'1':'0';}}inline void write_bits64(char*p,uint64_t bits){write_bits(p,uint32_t(bits));write_bits(p+32,uint32_t(bits>>32));}}
#pragma GCC pop_options
#line 1 "cp-algo/util/checkpoint.hpp"
#line 5 "cp-algo/util/checkpoint.hpp"
#include <chrono>
#line 8 "cp-algo/util/checkpoint.hpp"
namespace cp_algo{
#ifdef CP_ALGO_CHECKPOINT
big_map<big_string,double>checkpoints;double last;
#endif
template<bool final=false>void checkpoint([[maybe_unused]]auto const&_msg){
#ifdef CP_ALGO_CHECKPOINT
big_string msg=_msg;double now=(double)clock()/CLOCKS_PER_SEC;double delta=now-last;last=now;if(msg.size()&&!final){checkpoints[msg]+=delta;}if(final){for(auto const&[key,value]:checkpoints){std::cerr<<key<<": "<<value*1000<<" ms\n";}std::cerr<<"Total: "<<now*1000<<" ms\n";}
#endif
}template<bool final=false>void checkpoint(){checkpoint<final>("");}}
#line 8 "cp-algo/math/subset_convolution.hpp"
#include <ranges>
#include <algorithm>
#line 11 "cp-algo/math/subset_convolution.hpp"
#include <cstring>
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{
#ifndef CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN
#define CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN 20
#endif
const size_t max_logn=CP_ALGO_SUBSET_CONVOLUTION_MAX_LOGN;enum transform_dir{forw,inv};template<auto N,transform_dir direction>inline void xor_transform(auto&&a){if constexpr(N>>max_logn){throw std::runtime_error("N too large for xor_transform");}else if constexpr(N<=32){for(size_t i=1;i<N;i*=2){for(size_t j=0;j<N;j+=2*i){for(size_t k=j;k<j+i;k++){for(size_t z=0;z<max_logn;z++){auto x=a[k][z]+a[k+i][z];auto y=a[k][z]-a[k+i][z];a[k][z]=x;a[k+i][z]=y;}}}}}else{auto add=[&](auto&a,auto&b)__attribute__((always_inline)){auto x=a+b,y=a-b;a=x,b=y;};constexpr auto quar=N/4;for(size_t i=0;i<(size_t)quar;i++){auto x0=a[i+(size_t)quar*0];auto x1=a[i+(size_t)quar*1];auto x2=a[i+(size_t)quar*2];auto x3=a[i+(size_t)quar*3];
#pragma GCC unroll max_logn
for(size_t z=0;z<max_logn;z++){add(x0[z],x2[z]);add(x1[z],x3[z]);}
#pragma GCC unroll max_logn
for(size_t z=0;z<max_logn;z++){add(x0[z],x1[z]);add(x2[z],x3[z]);}a[i+(size_t)quar*0]=x0;a[i+(size_t)quar*1]=x1;a[i+(size_t)quar*2]=x2;a[i+(size_t)quar*3]=x3;}xor_transform<quar,direction>(&a[quar*0]);xor_transform<quar,direction>(&a[quar*1]);xor_transform<quar,direction>(&a[quar*2]);xor_transform<quar,direction>(&a[quar*3]);}}template<transform_dir direction>inline void xor_transform(auto&&a,auto n){with_bit_floor(n,[&]<auto NN>(){assert(NN==n);xor_transform<NN,direction>(a);});}template<transform_dir direction=forw>inline void xor_transform(auto&&a){xor_transform<direction>(a,std::size(a));}auto on_rank_vectors(auto&&cb,auto const&...inputs){static_assert(sizeof...(inputs)>=1,"on_rank_vectors requires at least one input");auto input_tuple=std::forward_as_tuple(inputs...);auto const&first_input=std::get<0>(input_tuple);using base=std::decay_t<decltype(first_input[0])>;big_vector<base>out(std::size(first_input));auto N=std::size(first_input);constexpr size_t K=4;N=std::max(N,2*K);const size_t n=std::bit_width(N)-1;const size_t T=std::min<size_t>(n-3,3);const size_t bottoms=1<<(n-T-1);const auto M=std::size(first_input);auto create_buffers=[bottoms]<typename... Args>(const Args&...){return std::make_tuple(big_vector<std::array<typename std::decay_t<Args>::value_type,max_logn>>(bottoms)...);};auto buffers=std::apply(create_buffers,input_tuple);checkpoint("alloc buffers");big_vector<uint32_t>counts(2*bottoms);for(size_t i=1;i<2*bottoms;i++){counts[i]=(uint32_t)std::popcount(i);}checkpoint("prepare");for(size_t top=0;top<N/2;top+=bottoms){std::apply([bottoms](auto&... bufs){(...,memset(bufs.data(),0,sizeof(bufs[0])*bottoms));},buffers);checkpoint("memset");std::apply([&](auto const&... inps){std::apply([&](auto&... bufs){auto init_one=[&](auto const&inp,auto&buf){for(size_t i=0;i<M;i+=2*bottoms){bool parity=__builtin_parity(uint32_t((i>>1)&top));size_t limit=std::min(M,i+2*bottoms)-i;uint32_t count=(uint32_t)std::popcount(i)-1;for(size_t bottom=(i==0);bottom<limit;bottom++){if(parity){buf[bottom>>1][count+counts[bottom]]-=inp[i+bottom];}else{buf[bottom>>1][count+counts[bottom]]+=inp[i+bottom];}}}};(init_one(inps,bufs),...);},buffers);},input_tuple);checkpoint("init");std::apply([](auto&... bufs){(...,xor_transform(bufs));},buffers);checkpoint("transform");assert(bottoms%K==0);for(size_t i=0;i<bottoms;i+=K){std::apply([&](auto&... bufs){auto extract_one=[&](auto&buf){std::array<u64x4,max_logn>aa;for(size_t j=0;j<max_logn;j++){for(size_t z=0;z<K;z++){aa[j][z]=buf[i+z][j].getr();}}return aa;};auto aa_tuple=std::make_tuple(extract_one(bufs)...);std::apply(cb,aa_tuple);auto&first_buf=std::get<0>(std::forward_as_tuple(bufs...));const auto&first_aa=std::get<0>(aa_tuple);for(size_t j=0;j<max_logn;j++){for(size_t z=0;z<K;z++){first_buf[i+z][j].setr((uint32_t)first_aa[j][z]);}}},buffers);}checkpoint("dot");auto&first_buf=std::get<0>(buffers);xor_transform<inv>(first_buf);checkpoint("transform");for(size_t i=0;i<M;i+=2*bottoms){bool parity=__builtin_parity(uint32_t((i>>1)&top));size_t limit=std::min(M,i+2*bottoms)-i;uint32_t count=(uint32_t)std::popcount(i)-1;for(size_t bottom=(i==0);bottom<limit;bottom++){if(parity){out[i+bottom]-=first_buf[bottom>>1][count+counts[bottom]];}else{out[i+bottom]+=first_buf[bottom>>1][count+counts[bottom]];}}}checkpoint("gather");}const base ni=base(N/2).inv();for(auto&x:out){x*=ni;}return out;}template<typename base>big_vector<base>subset_convolution(std::span<base>f,std::span<base>g){big_vector<base>outpa;constexpr size_t lgn=max_logn;outpa=on_rank_vectors([](auto&a,auto const&b){std::decay_t<decltype(a)>res={};const auto mod=base::mod();const auto imod=math::inv2(-mod);const auto r4=u64x4()+uint64_t(-1)%mod+1;auto add=[&](size_t i){if constexpr(lgn)for(size_t j=0;i+j+1<lgn;j++){res[i+j+1]+=(u64x4)_mm256_mul_epu32(__m256i(a[i]),__m256i(b[j]));}if constexpr(lgn>=20)if(i==15){for(size_t k=0;k<lgn;k++){res[k]=res[k]>=base::modmod8()?res[k]-base::modmod8():res[k];}}};if constexpr(lgn)for(size_t i=0;i<lgn;i++){add(i);}if constexpr(lgn)if constexpr(lgn)for(size_t k=0;k<lgn;k++){res[k]=montgomery_reduce(res[k],mod,imod);res[k]=montgomery_mul(res[k],r4,mod,imod);a[k]=res[k]>=mod?res[k]-mod:res[k];}},f,g);outpa[0]=f[0]*g[0];for(size_t i=1;i<std::size(f);i++){outpa[i]+=f[i]*g[0]+f[0]*g[i];}checkpoint("fix 0");return outpa;}template<typename base>big_vector<base>subset_exp(std::span<base>g){if(size(g)==1){return big_vector<base>{1};}size_t N=std::size(g);auto out0=subset_exp(std::span(g).first(N/2));auto out1=subset_convolution<base>(out0,std::span(g).last(N/2));out0.insert(end(out0),begin(out1),end(out1));cp_algo::checkpoint("extend out");return out0;}template<typename base>big_vector<big_vector<base>>subset_compose(std::span<base>f,std::span<base>g,size_t n){if(size(g)==1){size_t M=size(f);big_vector res(n,big_vector<base>{0});big_vector<base>pw(std::max(n,M));pw[0]=1;for(size_t j=1;j<M;j++){pw[j]=pw[j-1]*g[0];}for(size_t i=0;i<n;i++){for(size_t j=0;j<M;j++){res[i][0]+=pw[j]*f[j];}for(size_t j=M;j>i;j--){pw[j]=pw[j-1]*base(j);}pw[i]=0;}cp_algo::checkpoint("base case");return res;}size_t N=std::size(g);auto deeper=subset_compose(f,std::span(g).first(N/2),n+1);for(size_t i=0;i+1<size(deeper);i++){auto next=subset_convolution<base>(deeper[i+1],std::span(g).last(N/2));deeper[i].insert(end(deeper[i]),begin(next),end(next));}deeper.pop_back();cp_algo::checkpoint("combine");return deeper;}template<typename base>big_vector<base>subset_compose(std::span<base>f,std::span<base>g){return subset_compose(f,g,1)[0];}template<typename base>big_vector<base>subset_conv_transpose(std::span<base>h,std::span<base>g){std::ranges::reverse(h);auto res=subset_convolution<base>(h,g);std::ranges::reverse(h);std::ranges::reverse(res);return res;}template<typename base>big_vector<base>subset_power_projection(big_vector<big_vector<base>>&&fg,std::span<base>g,size_t M){if(size(g)==1){size_t n=size(fg);big_vector<base>res(M);big_vector<base>pw(std::max(n,M));pw[0]=1;for(size_t j=1;j<M;j++){pw[j]=pw[j-1]*g[0];}for(size_t i=0;i<size(fg);i++){for(size_t j=0;j<M;j++){res[j]+=pw[j]*fg[i][0];}for(size_t j=M;j>i;j--){pw[j]=pw[j-1]*base(j);}pw[i]=0;}cp_algo::checkpoint("base case");return res;}size_t N=std::size(g);fg.emplace_back(N/2);for(auto&&[i,h]:fg|std::views::enumerate|std::views::reverse|std::views::drop(1)){auto prev=subset_conv_transpose<base>(std::span(h).last(N/2),std::span(g).last(N/2));for(size_t j=0;j<N/2;j++){fg[i+1][j]+=prev[j];}fg[i+1].resize(N/2);}fg[0].resize(N/2);cp_algo::checkpoint("decombine");return subset_power_projection(std::move(fg),std::span(g).first(N/2),M);}template<typename base>big_vector<base>subset_power_projection(std::span<base>g,std::span<base>w,size_t M){return subset_power_projection({{begin(w),end(w)}},g,M);}}
#pragma GCC pop_options