This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "cp-algo/math/poly.hpp"#ifndef CP_ALGO_MATH_POLY_HPP
#define CP_ALGO_MATH_POLY_HPP
#include "poly/base.hpp"
#include "poly/div.hpp"
#include "poly/series.hpp"
#include "poly/series/sqrt.hpp"
#include "poly/sparse.hpp"
#include "poly/euclid.hpp"
#include "poly/recurrence.hpp"
#include "poly/transform.hpp"
#include "poly/chirpz.hpp"
#include "poly/eval.hpp"
#include "poly/powmod.hpp"
#include "poly/compose.hpp"
#endif // CP_ALGO_MATH_POLY_HPP
#line 1 "cp-algo/math/poly.hpp"
#line 1 "cp-algo/math/poly/base.hpp"
#line 1 "cp-algo/math/fft.hpp"
#line 1 "cp-algo/math/ring.hpp"
#line 1 "cp-algo/number_theory/discrete_sqrt.hpp"
#line 1 "cp-algo/number_theory/modint.hpp"
#line 1 "cp-algo/math/common.hpp"
#include <functional>
#include <cstdint>
#include <cassert>
#include <bit>
#include <vector>
#include <algorithm>
namespace cp_algo::math {
#ifdef CP_ALGO_MAXN
const int maxn = CP_ALGO_MAXN;
#else
const int maxn = 1 << 19;
#endif
const int magic = 64; // threshold for sizes to run the naive algo
// Nonnegative 64-bit exponents, with an associative operation and its identity.
// Windows >1 precompute odd powers only when that saves operations.
template<int window = 1>
auto bpow(auto const& x, auto n, auto const& one, auto op) {
static_assert(window >= 1 && window <= 6);
if constexpr(window > 1) {
if(n == 0) {return one;}
int bits = std::bit_width(uint64_t(n));
auto low_bit = [&](int high) {
int low = std::max(0, high - window + 1);
while(!((n >> low) & 1)) {low++;}
return low;
};
int first = low_bit(bits - 1);
int cost = (1 << (window - 1)) + first;
for(int j = first - 1; j >= 0;) {
if(!((n >> j) & 1)) {j--;}
else {cost++; j = low_bit(j) - 1;}
}
// Do not pay for the table when binary powering uses fewer operations.
if(cost >= bits + std::popcount(uint64_t(n)) - 2) {return bpow<1>(x, n, one, op);}
using T = std::decay_t<decltype(x)>;
std::vector<T> odd;
odd.reserve(1 << (window - 1));
odd.push_back(x);
auto square = op(x, x);
while(odd.size() < size_t(1 << (window - 1))) {odd.push_back(op(odd.back(), square));}
auto ans = odd[(n >> first) / 2];
for(int j = first - 1; j >= 0;) {
if(!((n >> j) & 1)) {ans = op(ans, ans); j--;}
else {
int low = low_bit(j), length = j - low + 1;
auto digit = (n >> low) & ((1u << length) - 1);
for(int i = 0; i < length; i++) {ans = op(ans, ans);}
ans = op(ans, odd[digit / 2]);
j = low - 1;
}
}
return ans;
} else {
if (n == 0) {
return one;
}
auto ans = x;
for(int j = std::bit_width<uint64_t>(n) - 2; ~j; j--) {
ans = op(ans, ans);
if((n >> j) & 1) {
ans = op(ans, x);
}
}
return ans;
}
}
template<int window = 1>
auto bpow(auto x, auto n, auto ans) {
return bpow<window>(x, n, ans, std::multiplies{});
}
template<typename T>
T bpow(T const& x, auto n) {
return bpow(x, n, T(1));
}
inline constexpr auto inv2(auto x) {
assert(x % 2);
std::make_unsigned_t<decltype(x)> y = 1;
while(y * x != 1) {
y *= 2 - x * y;
}
return y;
}
}
#line 4 "cp-algo/number_theory/modint.hpp"
#include <iostream>
#line 6 "cp-algo/number_theory/modint.hpp"
namespace cp_algo::math {
template<typename modint, typename _Int>
struct modint_base {
using Int = _Int;
using UInt = std::make_unsigned_t<Int>;
static constexpr size_t bits = sizeof(Int) * 8;
using Int2 = std::conditional_t<bits <= 32, int64_t, __int128_t>;
using UInt2 = std::conditional_t<bits <= 32, uint64_t, __uint128_t>;
constexpr static Int mod() {
return modint::mod();
}
constexpr static UInt remod() {
return modint::remod();
}
constexpr static UInt2 modmod() {
return UInt2(mod()) * mod();
}
constexpr modint_base() = default;
constexpr modint_base(Int2 rr) {
to_modint().setr(UInt((rr + modmod()) % mod()));
}
constexpr modint inv() const {
return bpow(to_modint(), mod() - 2);
}
modint operator-() const {
modint neg;
neg.r = std::min(-r, remod() - r);
return neg;
}
modint& operator/=(const modint& t) {
return to_modint() *= t.inv();
}
modint& operator*=(const modint& t) {
r = UInt(UInt2(r) * t.r % mod());
return to_modint();
}
modint& operator+=(const modint& t) {
r += t.r;
r = std::min(r, r - remod());
return to_modint();
}
modint& operator-=(const modint& t) {
r -= t.r;
r = std::min(r, r + remod());
return to_modint();
}
modint operator+(const modint& t) const { return modint(to_modint()) += t; }
modint operator-(const modint& t) const { return modint(to_modint()) -= t; }
modint operator*(const modint& t) const { return modint(to_modint()) *= t; }
modint operator/(const modint& t) const { return modint(to_modint()) /= t; }
// Why <=> doesn't work?..
auto operator==(const modint& t) const { return to_modint().getr() == t.getr(); }
auto operator!=(const modint& t) const { return to_modint().getr() != t.getr(); }
auto operator<=(const modint& t) const { return to_modint().getr() <= t.getr(); }
auto operator>=(const modint& t) const { return to_modint().getr() >= t.getr(); }
auto operator<(const modint& t) const { return to_modint().getr() < t.getr(); }
auto operator>(const modint& t) const { return to_modint().getr() > t.getr(); }
Int rem() const {
UInt R = to_modint().getr();
return R - (R > (UInt)mod() / 2) * mod();
}
constexpr void setr(UInt rr) {
r = rr;
}
constexpr UInt getr() const {
return r;
}
// Only use these if you really know what you're doing!
static uint64_t modmod8() { return uint64_t(8 * modmod()); }
void add_unsafe(UInt t) { r += t; }
void pseudonormalize() { r = std::min(r, r - modmod8()); }
modint const& normalize() {
if(r >= (UInt)mod()) {
r %= mod();
}
return to_modint();
}
void setr_direct(UInt rr) { r = rr; }
UInt getr_direct() const { return r; }
protected:
UInt r;
private:
constexpr modint& to_modint() { return static_cast<modint&>(*this); }
constexpr modint const& to_modint() const { return static_cast<modint const&>(*this); }
};
template<typename modint>
concept modint_type = std::is_base_of_v<modint_base<modint, typename modint::Int>, modint>;
template<modint_type modint>
decltype(std::cin)& operator>>(decltype(std::cin)& in, modint& x) {
typename modint::UInt r;
auto &res = in >> r;
x.setr(r);
return res;
}
template<modint_type modint>
decltype(std::cout)& operator<<(decltype(std::cout)& out, modint const& x) {
return out << x.getr();
}
template<auto m>
struct modint: modint_base<modint<m>, decltype(m)> {
using Base = modint_base<modint<m>, decltype(m)>;
using Base::Base;
static constexpr Base::Int mod() { return m; }
static constexpr Base::UInt remod() { return m; }
auto getr() const { return Base::r; }
};
// Odd moduli up to a quarter of the unsigned word keep Montgomery residues lazily in
// [0, 2 mod): remod() = 2 mod, and both 4 mod and ab + q * mod fit. Any other modulus keeps
// fully reduced residues, remod() = mod, which is what the sums of modint_base need then:
// an even one without the Montgomery form, a wide odd one with a reduction that subtracts
// high words instead of adding double words that would overflow.
template<typename Int = int>
struct dynamic_modint: modint_base<dynamic_modint<Int>, Int> {
using Base = modint_base<dynamic_modint<Int>, Int>;
using Base::Base;
// Out of line, so that the hot path stays as small as it was.
[[gnu::noinline, gnu::cold]] static Base::UInt m_reduce_reduced(Base::UInt2 ab) {
if(mod() % 2 == 0) {
return typename Base::UInt(ab % mod());
}
// q * mod has the low word of ab, so the difference of the high words is exact.
typename Base::UInt q = -(typename Base::UInt(ab) * inverse);
auto high = typename Base::UInt(ab >> Base::bits);
auto low = typename Base::UInt(typename Base::UInt2(q) * typename Base::UInt(mod()) >> Base::bits);
return high >= low ? high - low : high - low + mod();
}
static Base::UInt m_reduce(Base::UInt2 ab) {
if(imod() == 0) [[unlikely]] {
return m_reduce_reduced(ab);
}
typename Base::UInt2 m = typename Base::UInt(ab) * imod();
return typename Base::UInt((ab + m * mod()) >> Base::bits);
}
static Base::UInt m_transform(Base::UInt a) {
if(mod() % 2 == 0) [[unlikely]] {
return a;
} else {
return m_reduce(a * pw128());
}
}
dynamic_modint& operator*=(const dynamic_modint& t) {
Base::r = m_reduce(typename Base::UInt2(Base::r) * t.r);
return *this;
}
void setr(Base::UInt rr) {
Base::r = m_transform(rr);
}
Base::UInt getr() const {
typename Base::UInt res = m_reduce(Base::r);
return std::min(res, res - mod());
}
static Int mod() { return m; }
static Base::UInt remod() { return rm; }
static Base::UInt imod() { return im; }
static Base::UInt2 pw128() { return r2; }
static void switch_mod(Int nm) {
m = nm;
bool lazy = m % 2 && typename Base::UInt(m) <= typename Base::UInt(-1) / 4;
rm = typename Base::UInt(m) * (lazy ? 2 : 1);
inverse = m % 2 ? inv2(-m) : 0;
im = lazy ? inverse : 0;
r2 = static_cast<Base::UInt>(static_cast<Base::UInt2>(-1) % m + 1);
}
// Wrapper for temp switching
auto static with_mod(Int tmp, auto callback) {
struct scoped {
Int prev = mod();
~scoped() { switch_mod(prev); }
} _;
switch_mod(tmp);
return callback();
}
private:
static inline thread_local Int m = 1;
// im: -1 / mod modulo 2^bits for lazy residues and 0 for reduced ones; inverse: the same
// for every odd mod; rm: the value of remod().
static inline thread_local Base::UInt im = -1, r2 = 0, inverse = -1, rm = 2;
};
}
#line 1 "cp-algo/random/rng.hpp"
#include <chrono>
#include <random>
namespace cp_algo::random {
std::mt19937_64 gen(
std::chrono::steady_clock::now().time_since_epoch().count()
);
uint64_t rng() {
return gen();
}
}
#line 1 "cp-algo/math/affine.hpp"
#include <optional>
#include <utility>
#line 6 "cp-algo/math/affine.hpp"
#include <tuple>
namespace cp_algo::math {
// a * x + b
template<typename base>
struct lin {
base a = 1, b = 0;
std::optional<base> c;
lin() {}
lin(base b): a(0), b(b) {}
lin(base a, base b): a(a), b(b) {}
lin(base a, base b, base _c): a(a), b(b), c(_c) {}
// polynomial product modulo x^2 - c
lin operator * (const lin& t) {
assert(c && t.c && *c == *t.c);
return {a * t.b + b * t.a, b * t.b + a * t.a * (*c), *c};
}
// a * (t.a * x + t.b) + b
lin apply(lin const& t) const {
return {a * t.a, a * t.b + b};
}
void prepend(lin const& t) {
*this = t.apply(*this);
}
base eval(base x) const {
return a * x + b;
}
};
// (ax+b) / (cx+d)
template<typename base>
struct linfrac {
base a, b, c, d;
linfrac(): a(1), b(0), c(0), d(1) {} // x, identity for composition
linfrac(base a): a(a), b(1), c(1), d(0) {} // a + 1/x, for continued fractions
linfrac(base a, base b, base c, base d): a(a), b(b), c(c), d(d) {}
// composition of two linfracs
linfrac operator * (linfrac t) const {
return t.prepend(linfrac(*this));
}
linfrac operator-() const {
return {-a, -b, -c, -d};
}
linfrac adj() const {
return {d, -b, -c, a};
}
linfrac& prepend(linfrac const& t) {
t.apply(a, c);
t.apply(b, d);
return *this;
}
// apply linfrac to A/B
void apply(base &A, base &B) const {
std::tie(A, B) = std::pair{a * A + b * B, c * A + d * B};
}
};
}
#line 6 "cp-algo/number_theory/discrete_sqrt.hpp"
namespace cp_algo::math {
// https://en.wikipedia.org/wiki/Berlekamp-Rabin_algorithm
template<modint_type base>
std::optional<base> sqrt(base b) {
if(b == base(0)) {
return base(0);
} else if(bpow(b, (b.mod() - 1) / 2) != base(1)) {
return std::nullopt;
} else {
while(true) {
base z = random::rng();
if(z * z == b) {
return z;
}
lin<base> x(1, z, b); // x + z (mod x^2 - b)
x = bpow(x, (b.mod() - 1) / 2, lin<base>(0, 1, b));
if(x.a != base(0)) {
return x.a.inv();
}
}
}
}
}
#line 1 "cp-algo/number_theory/primality.hpp"
#line 6 "cp-algo/number_theory/primality.hpp"
namespace cp_algo::math {
// https://en.wikipedia.org/wiki/Miller–Rabin_primality_test
template<typename _Int>
bool is_prime(_Int m) {
using Int = std::make_signed_t<_Int>;
using UInt = std::make_unsigned_t<Int>;
if(m == 1 || m % 2 == 0) {
return m == 2;
}
// m - 1 = 2^s * d
int s = std::countr_zero(UInt(m - 1));
auto d = (m - 1) >> s;
using base = dynamic_modint<Int>;
auto test = [&](base x) {
x = bpow(x, d);
if(std::abs(x.rem()) <= 1) {
return true;
}
for(int i = 1; i < s && x != -1; i++) {
x *= x;
}
return x == -1;
};
return base::with_mod(m, [&]() {
#ifdef CP_ALGO_NUMBER_THEORY_PRIMALITY_BASES_HPP
uint16_t base2 = 7, base3 = 61;
if (m != uint32_t(m)) {
base2 = base_table1[uint32_t(m * 0xAD625B89) >> 18];
base3 = base_table2[base2 >> 13];
}
return test(2) && test(base2) && test(base3);
#else
return std::ranges::all_of(std::array{2, 325, 9375, 28178, 450775, 9780504, 1795265022}, test);
#endif
});
}
}
#line 1 "cp-algo/util/checkpoint.hpp"
#line 1 "cp-algo/util/big_alloc.hpp"
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#line 10 "cp-algo/util/big_alloc.hpp"
#include <string>
#include <cstddef>
#line 14 "cp-algo/util/big_alloc.hpp"
#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::has_single_bit(Align), "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;
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);
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>>;
}
#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 1 "cp-algo/math/cvector.hpp"
#line 1 "cp-algo/util/simd.hpp"
#include <experimental/simd>
#line 6 "cp-algo/util/simd.hpp"
#include <memory>
#if defined(__x86_64__) && !defined(CP_ALGO_DISABLE_AVX2)
#define CP_ALGO_SIMD_AVX2_TARGET _Pragma("GCC target(\"avx2,fma\")")
#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 u8x16 = simd<uint8_t, 16>;
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);
}
// x - mod where x >= mod, for x in [0, 2 mod). Unsigned, so it holds up to mod < 2^31;
// zero upper halves of 64-bit lanes stay zero.
inline u32x8 reduce_once(u32x8 x, uint32_t mod) {
auto y = x - mod;
return x < y ? x : y;
}
inline u64x4 reduce_once(u64x4 x, uint32_t mod) {
return u64x4(reduce_once(u32x8(x), mod));
}
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/complex.hpp"
#line 4 "cp-algo/util/complex.hpp"
#include <cmath>
#include <type_traits>
#line 7 "cp-algo/util/complex.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo {
// Custom implementation, since std::complex is UB on non-floating types
template<typename T>
struct complex {
using value_type = T;
T x, y;
inline constexpr complex(): x(), y() {}
inline constexpr complex(T const& x): x(x), y() {}
inline constexpr complex(T const& x, T const& y): x(x), y(y) {}
inline complex& operator *= (T const& t) {x *= t; y *= t; return *this;}
inline complex& operator /= (T const& t) {x /= t; y /= t; return *this;}
inline complex operator * (T const& t) const {return complex(*this) *= t;}
inline complex operator / (T const& t) const {return complex(*this) /= t;}
inline complex& operator += (complex const& t) {x += t.x; y += t.y; return *this;}
inline complex& operator -= (complex const& t) {x -= t.x; y -= t.y; return *this;}
inline complex operator * (complex const& t) const {return {x * t.x - y * t.y, x * t.y + y * t.x};}
inline complex operator / (complex const& t) const {return *this * t.conj() / t.norm();}
inline complex operator + (complex const& t) const {return complex(*this) += t;}
inline complex operator - (complex const& t) const {return complex(*this) -= t;}
inline complex& operator *= (complex const& t) {return *this = *this * t;}
inline complex& operator /= (complex const& t) {return *this = *this / t;}
inline complex operator - () const {return {-x, -y};}
inline complex conj() const {return {x, -y};}
inline T norm() const {return x * x + y * y;}
inline T abs() const {return std::sqrt(norm());}
inline T const real() const {return x;}
inline T const imag() const {return y;}
inline T& real() {return x;}
inline T& imag() {return y;}
inline static constexpr complex polar(T r, T theta) {return {T(r * cos(theta)), T(r * sin(theta))};}
inline auto operator <=> (complex const& t) const = default;
};
template<typename T> inline complex<T> conj(complex<T> const& x) {return x.conj();}
template<typename T> inline T norm(complex<T> const& x) {return x.norm();}
template<typename T> inline T abs(complex<T> const& x) {return x.abs();}
template<typename T> inline T& real(complex<T> &x) {return x.real();}
template<typename T> inline T& imag(complex<T> &x) {return x.imag();}
template<typename T> inline T const real(complex<T> const& x) {return x.real();}
template<typename T> inline T const imag(complex<T> const& x) {return x.imag();}
template<typename T>
inline constexpr complex<T> polar(T r, T theta) {
return complex<T>::polar(r, theta);
}
template<typename T>
inline std::ostream& operator << (std::ostream &out, complex<T> const& x) {
return out << x.real() << ' ' << x.imag();
}
}
#pragma GCC pop_options
#line 7 "cp-algo/math/cvector.hpp"
#include <immintrin.h>
#include <numbers>
#include <cstring>
#include <ranges>
#line 13 "cp-algo/math/cvector.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace stdx = std::experimental;
namespace cp_algo::math::fft {
static constexpr size_t flen = 4;
using ftype = double;
using vftype = dx4;
using point = complex<ftype>;
using vpoint = complex<vftype>;
static constexpr vftype vz = {};
vpoint vi(vpoint const& r) {
return {-imag(r), real(r)};
}
// Spectrum storage skips zero-filling on resize; every user writes before reading.
template<class T>
struct spectrum_alloc: big_alloc<T> {
using big_alloc<T>::big_alloc;
template<class U>
struct rebind {
using other = spectrum_alloc<U>;
};
template<class U>
requires(std::is_same_v<U, vpoint>)
void construct(U*) noexcept {}
};
using spectrum_vector = std::vector<vpoint, spectrum_alloc<vpoint>>;
struct cvector {
spectrum_vector r;
cvector(size_t n) {
n = std::max(flen, std::bit_ceil(n));
r.assign(n / flen, vpoint{});
prepare_roots(n / 16);
checkpoint("cvector create");
}
vpoint& at(size_t k) { return r[k / flen]; }
vpoint at(size_t k) const { return r[k / flen]; }
template<class pt = point>
inline void set(size_t k, pt const& t) {
if constexpr(std::is_same_v<pt, point>) {
real(r[k / flen])[k % flen] = real(t);
imag(r[k / flen])[k % flen] = imag(t);
} else {
at(k) = t;
}
}
template<class pt = point>
inline pt get(size_t k) const {
if constexpr(std::is_same_v<pt, point>) {
return {real(r[k / flen])[k % flen], imag(r[k / flen])[k % flen]};
} else {
return at(k);
}
}
size_t size() const {
return flen * r.size();
}
static constexpr size_t eval_arg(size_t n) {
if(n < pre_evals) {
return eval_args[n];
} else {
return eval_arg(n / 2) | (n & 1) << (std::bit_width(n) - 1);
}
}
static constexpr point eval_point(size_t n) {
if(n % 2) {
return -eval_point(n - 1);
} else if(n % 4) {
return eval_point(n - 2) * point(0, 1);
} else if(n / 4 < pre_evals) {
return evalp[n / 4];
} else if(n / 4 - pre_evals < extra.size()) {
return extra[n / 4 - pre_evals];
} else {
return polar<ftype>(1., std::numbers::pi / (ftype)std::bit_floor(n) * (ftype)eval_arg(n));
}
}
static constexpr std::array<point, 32> roots = []() {
std::array<point, 32> res;
for(size_t i = 2; i < 32; i++) {
res[i] = polar<ftype>(1., std::numbers::pi / (1ull << (i - 2)));
}
return res;
}();
static constexpr point root(size_t n) {
return roots[std::bit_width(n)];
}
template<int step>
static void exec_on_eval(size_t n, size_t k, auto &&callback) {
callback(k, root(4 * step * n) * eval_point(step * k));
}
template<int step>
static void exec_on_evals(size_t n, auto &&callback) {
point factor = root(4 * step * n);
if constexpr(step == 1 || step == 2 || step == 4) {
prepare_roots((step * n + 3) / 4);
size_t i = 0;
if constexpr(step == 1) {
for(; i + 4 <= n; i += 4) {
size_t k = i / 4;
point e = k < pre_evals ? evalp[k] : extra[k - pre_evals];
point v = factor * e;
callback(i, v);
callback(i + 1, -v);
point iv(-imag(v), real(v));
callback(i + 2, iv);
callback(i + 3, -iv);
}
} else if constexpr(step == 2) {
for(; i + 2 <= n; i += 2) {
size_t k = i / 2;
point e = k < pre_evals ? evalp[k] : extra[k - pre_evals];
point v = factor * e;
callback(i, v);
callback(i + 1, point(-imag(v), real(v)));
}
}
for(; i < n; i++) {
size_t index = step * i, k = index / 4;
point e = k < pre_evals ? evalp[k] : extra[k - pre_evals];
if(index & 2) {
e = e * point(0, 1);
}
if(index & 1) {
e = -e;
}
callback(i, factor * e);
}
} else {
for(size_t i = 0; i < n; i++) {
callback(i, factor * eval_point(step * i));
}
}
}
static void do_dot_iter(point rt, vpoint& Bv, vpoint const& Av, vpoint& res) {
res += Av * Bv;
real(Bv) = rotate_right(real(Bv));
imag(Bv) = rotate_right(imag(Bv));
auto x = real(Bv)[0], y = imag(Bv)[0];
real(Bv)[0] = x * real(rt) - y * imag(rt);
imag(Bv)[0] = x * imag(rt) + y * real(rt);
}
template<size_t fixed = 0>
void dot(cvector const& t) {
size_t n = fixed ? fixed : this->size();
exec_on_evals<1>(n / flen, [&] [[gnu::always_inline]] (size_t k, point rt) {
k *= flen;
auto [Ax, Ay] = at(k);
auto Cv = t.at(k);
vpoint vrt = {vz + real(rt), vz + imag(rt)};
auto Cr = Cv * vrt;
vpoint res = vz;
auto iter = [&]<int i> [[gnu::always_inline]] () {
auto wrap = [&](vftype original, vftype rotated) {
if constexpr(i == 0) {
return original;
} else {
return __builtin_shufflevector(rotated, original, 4 - i, 5 - i, 6 - i, 7 - i);
}
};
vpoint Cw = {wrap(real(Cv), real(Cr)), wrap(imag(Cv), imag(Cr))};
vpoint Av = {vz + Ax[i], vz + Ay[i]};
return Av * Cw;
};
auto p0 = iter.template operator()<0>(), p1 = iter.template operator()<1>();
auto p2 = iter.template operator()<2>(), p3 = iter.template operator()<3>();
res = (p0 + p1) + (p2 + p3);
set(k, res);
});
checkpoint("dot");
}
// normalize=false leaves the inverse-transform scale for the caller.
template<bool partial = true, bool normalize = true, size_t fixed = 0>
void ifft() {
size_t n = fixed ? fixed : size();
if constexpr(!partial) {
prepare_roots(n / 4);
point pi(0, 1);
exec_on_evals<4>(n / 4, [&] [[gnu::always_inline]] (size_t k, point rt) {
k *= 4;
point v1 = conj(rt);
point v2 = v1 * v1;
point v3 = v1 * v2;
auto A = get(k);
auto B = get(k + 1);
auto C = get(k + 2);
auto D = get(k + 3);
set(k, (A + B) + (C + D));
set(k + 2, ((A + B) - (C + D)) * v2);
set(k + 1, ((A - B) - pi * (C - D)) * v1);
set(k + 3, ((A - B) + pi * (C - D)) * v3);
});
}
bool parity = std::countr_zero(n) % 2;
if(parity) {
exec_on_evals<2>(n / (2 * flen), [&] [[gnu::always_inline]] (size_t k, point rt) {
k *= 2 * flen;
vpoint cvrt = {vz + real(rt), vz - imag(rt)};
auto B = at(k) - at(k + flen);
at(k) += at(k + flen);
at(k + flen) = B * cvrt;
});
}
transform<true, fixed>(n, parity);
checkpoint("ifft");
if constexpr(normalize) {
auto scale = vz + ftype(partial ? flen : 1) / ftype(n);
for(size_t k = 0; k < n; k += flen) {
set(k, get<vpoint>(k) * scale);
}
}
}
template<bool partial = true, size_t fixed = 0>
void fft() {
size_t n = fixed ? fixed : size();
prepare_roots(n / (partial ? 16 : 4));
bool parity = std::countr_zero(n) % 2;
transform<false, fixed>(n, parity);
if(parity) {
exec_on_evals<2>(n / (2 * flen), [&] [[gnu::always_inline]] (size_t k, point rt) {
k *= 2 * flen;
vpoint vrt = {vz + real(rt), vz + imag(rt)};
auto t = at(k + flen) * vrt;
at(k + flen) = at(k) - t;
at(k) += t;
});
}
if constexpr(!partial) {
prepare_roots(n / 4);
point pi(0, 1);
exec_on_evals<4>(n / 4, [&] [[gnu::always_inline]] (size_t k, point rt) {
k *= 4;
point v1 = rt;
point v2 = v1 * v1;
point v3 = v1 * v2;
auto A = get(k);
auto B = get(k + 1) * v1;
auto C = get(k + 2) * v2;
auto D = get(k + 3) * v3;
set(k, (A + C) + (B + D));
set(k + 1, (A + C) - (B + D));
set(k + 2, (A - C) + pi * (B - D));
set(k + 3, (A - C) - pi * (B - D));
});
}
checkpoint("fft");
}
static std::array<vpoint, 4> transpose(std::array<vpoint, 4> const& a) {
auto half = [](auto part) {
auto a0 = __m256d(part(0)), a1 = __m256d(part(1)), a2 = __m256d(part(2)), a3 = __m256d(part(3));
auto t0 = _mm256_unpacklo_pd(a0, a1), t1 = _mm256_unpackhi_pd(a0, a1), t2 = _mm256_unpacklo_pd(a2, a3),
t3 = _mm256_unpackhi_pd(a2, a3);
return std::array<vftype, 4>{
vftype(_mm256_permute2f128_pd(t0, t2, 0x20)), vftype(_mm256_permute2f128_pd(t1, t3, 0x20)),
vftype(_mm256_permute2f128_pd(t0, t2, 0x31)), vftype(_mm256_permute2f128_pd(t1, t3, 0x31))};
};
auto re = half([&](int k) { return real(a[k]); }), im = half([&](int k) { return imag(a[k]); });
return {vpoint{re[0], im[0]}, vpoint{re[1], im[1]}, vpoint{re[2], im[2]}, vpoint{re[3], im[3]}};
}
// Multiply groups of 16 points as polynomials modulo x^16 - rt: one radix-4 level on
// each operand, a lane-parallel 4-coefficient Karatsuba product, one inverse level.
// The weights rt, rt^2, rt^3 of a tile of groups are computed four groups per SIMD
// operation ahead of the tile, so the product loop only broadcasts them from memory.
static constexpr size_t dot_tile = 64;
template<size_t fixed, bool square = false>
void dot_fused16(cvector const& t, size_t offset, size_t length) {
constexpr size_t T = dot_tile;
const size_t n = fixed ? fixed : size();
point factor = root(n);
auto fr = _mm256_set1_pd(real(factor)), fi = _mm256_set1_pd(imag(factor));
alignas(32) double tw[6][T];
const bool ahead = offset + length < n;
auto cmadd = [] [[gnu::always_inline]] (vpoint x, vpoint y, vpoint c) {
auto re = _mm256_fmadd_pd(__m256d(real(x)), __m256d(real(y)),
_mm256_fnmadd_pd(__m256d(imag(x)), __m256d(imag(y)), __m256d(real(c))));
auto im = _mm256_fmadd_pd(__m256d(real(x)), __m256d(imag(y)),
_mm256_fmadd_pd(__m256d(imag(x)), __m256d(real(y)), __m256d(imag(c))));
return vpoint{vftype(re), vftype(im)};
};
auto cmcross = [] [[gnu::always_inline]] (vpoint x, vpoint y, vpoint p, vpoint q) {
auto re = _mm256_sub_pd(_mm256_fmsub_pd(__m256d(real(x)), __m256d(real(y)), __m256d(real(p))),
_mm256_fmadd_pd(__m256d(imag(x)), __m256d(imag(y)), __m256d(real(q))));
auto im = _mm256_add_pd(_mm256_fmsub_pd(__m256d(real(x)), __m256d(imag(y)), __m256d(imag(p))),
_mm256_fmsub_pd(__m256d(imag(x)), __m256d(real(y)), __m256d(imag(q))));
return vpoint{vftype(re), vftype(im)};
};
// z * conj(v)
auto mulconj = [] [[gnu::always_inline]] (vpoint z, vpoint v) {
auto re = _mm256_fmadd_pd(__m256d(real(z)), __m256d(real(v)),
_mm256_mul_pd(__m256d(imag(z)), __m256d(imag(v))));
auto im = _mm256_fmsub_pd(__m256d(imag(z)), __m256d(real(v)),
_mm256_mul_pd(__m256d(real(z)), __m256d(imag(v))));
return vpoint{vftype(re), vftype(im)};
};
for(size_t tile = offset; tile < offset + length; tile += 16 * T) {
size_t k0 = tile / 16;
auto const* e = reinterpret_cast<double const*>(k0 < pre_evals ? evalp.data() + k0
: extra.data() + (k0 - pre_evals));
for(size_t g = 0; g < T; g += 4) {
auto x = _mm256_loadu_pd(e + 2 * g), y = _mm256_loadu_pd(e + 2 * g + 4);
auto er = _mm256_permute4x64_pd(_mm256_unpacklo_pd(x, y), 0xD8),
ei = _mm256_permute4x64_pd(_mm256_unpackhi_pd(x, y), 0xD8);
auto r1 = _mm256_fmsub_pd(fr, er, _mm256_mul_pd(fi, ei)),
i1 = _mm256_fmadd_pd(fr, ei, _mm256_mul_pd(fi, er));
auto r2 = _mm256_fmsub_pd(r1, r1, _mm256_mul_pd(i1, i1)),
i2 = _mm256_fmadd_pd(r1, i1, _mm256_mul_pd(i1, r1));
auto r3 = _mm256_fmsub_pd(r1, r2, _mm256_mul_pd(i1, i2)),
i3 = _mm256_fmadd_pd(r1, i2, _mm256_mul_pd(i1, r2));
_mm256_store_pd(tw[0] + g, r1);
_mm256_store_pd(tw[1] + g, i1);
_mm256_store_pd(tw[2] + g, r2);
_mm256_store_pd(tw[3] + g, i2);
_mm256_store_pd(tw[4] + g, r3);
_mm256_store_pd(tw[5] + g, i3);
}
for(size_t g = 0; g < T; g++) {
size_t pos = tile + 16 * g;
if(ahead) {
// Pull the next block of both operands towards the cache while this one is multiplied.
auto const* pa = reinterpret_cast<char const*>(r.data() + (pos + length) / flen);
auto const* pb = reinterpret_cast<char const*>(t.r.data() + (pos + length) / flen);
for(size_t c = 0; c < 4; c++) {
_mm_prefetch(pa + 64 * c, _MM_HINT_T2);
_mm_prefetch(pb + 64 * c, _MM_HINT_T2);
}
}
auto bc = [&] [[gnu::always_inline]] (size_t c) {
return vftype(_mm256_broadcast_sd(tw[c] + g));
};
vpoint v1 = {bc(0), bc(1)}, v2 = {bc(2), bc(3)}, v3 = {bc(4), bc(5)};
auto forward = [&] [[gnu::always_inline]] (cvector const& a) {
auto A = a.at(pos), B = a.at(pos + 4) * v1, C = a.at(pos + 8) * v2, D = a.at(pos + 12) * v3;
return std::array<vpoint, 4>{(A + C) + (B + D), (A + C) - (B + D), (A - C) + vi(B - D),
(A - C) - vi(B - D)};
};
auto a = transpose(forward(*this));
auto b = [&] {
if constexpr(square) {
return a;
} else {
return transpose(forward(t));
}
}();
// The four residues are taken modulo x^4 - rt * {1, -1, i, -i}.
const auto flip_re = _mm256_set_pd(0., -0., -0., 0.), flip_im = _mm256_set_pd(-0., 0., -0., 0.);
vpoint w = {
vftype(_mm256_xor_pd(_mm256_blend_pd(__m256d(real(v1)), __m256d(imag(v1)), 0b1100), flip_re)),
vftype(_mm256_xor_pd(_mm256_blend_pd(__m256d(imag(v1)), __m256d(real(v1)), 0b1100), flip_im))};
auto mul = [&] [[gnu::always_inline]] (vpoint a0, vpoint a1, vpoint b0, vpoint b1) {
auto p = a0 * b0, q = a1 * b1;
return std::array<vpoint, 2>{cmadd(w, q, p), cmcross(a0 + a1, b0 + b1, p, q)};
};
auto p = mul(a[0], a[2], b[0], b[2]), q = mul(a[1], a[3], b[1], b[3]);
auto m = mul(a[0] + a[1], a[2] + a[3], b[0] + b[1], b[2] + b[3]);
auto c = transpose({cmadd(w, q[1], p[0]), (m[0] - p[0]) - q[0], p[1] + q[0], (m[1] - p[1]) - q[1]});
auto A = c[0], B = c[1], C = c[2], D = c[3];
at(pos) = (A + B) + (C + D);
at(pos + 8) = mulconj((A + B) - (C + D), v2);
at(pos + 4) = mulconj((A - B) - vi(C - D), v1);
at(pos + 12) = mulconj((A - B) + vi(C - D), v3);
}
}
}
// A product in two steps, this <- this * t modulo x^n - i up to the factor n / flen:
// forward() on both operands, then multiply(t), which may be *this for a square.
// The forward transform stops at groups of 16 points and dot_fused16 multiplies those,
// which saves a pass over each operand and one over the result. That needs powers of
// four, so a length 2 * 4^k takes its radix-two level at the top, modulo
// x^(n/2) -+ sqrt(i); short lengths take the complete transform and the 4-point product.
void forward() {
if(!fused_leaves()) {
return fft();
}
size_t n = size(), part = fused_part();
if(part < n) {
vpoint vrt = {vz + real(roots[4]), vz + imag(roots[4])};
for(size_t k = 0; k < part; k += flen) {
auto t = at(k + part) * vrt;
at(k + part) = at(k) - t;
at(k) += t;
}
}
for(size_t offset = 0; offset < n; offset += part) {
transform<false, 0, 0, true>(n, false, offset, part);
}
checkpoint("fft");
}
void multiply(cvector const& t) {
if(!fused_leaves()) {
dot(t);
return ifft<true, false>();
}
size_t n = size(), part = fused_part();
dot_fused16<0>(t, 0, n);
checkpoint("dot");
for(size_t offset = 0; offset < n; offset += part) {
transform<true, 0, 0, true>(n, false, offset, part);
}
if(part < n) {
vpoint cvrt = {vz + real(roots[4]), vz - imag(roots[4])};
for(size_t k = 0; k < part; k += flen) {
auto t = at(k) - at(k + part);
at(k) += at(k + part);
at(k + part) = t * cvrt;
}
}
checkpoint("ifft");
}
// Radix-64 out-of-cache pass for n = 2^24, run as two tiled radix-8 stages.
// With Fuse=1 (forward only) the first stage lifts u32 residues to Gaussian
// coordinates on load, so the spectrum buffer is written once and never re-read
// before the in-cache block phase. The index-keyed noise makes both branches see
// the same representatives.
struct fuse_args {
const uint32_t* src = nullptr;
size_t count = 0;
uint64_t seed = 0;
double a = 0, b = 0, a_over_p = 0, b_over_p = 0;
};
static constexpr size_t sweep_tile = 256;
template<bool inverse, int Mode, size_t Tile, int Fuse = 0, bool Neg = false>
void sweep8(fuse_args const& fa = {}) {
constexpr size_t n = 1 << 24;
static const std::array<std::array<point, 7>, 9> weights = []() {
std::array<std::array<point, 7>, 9> table;
for(size_t count: {size_t(1), size_t(8)}) {
for(size_t k = 0; k < count; k++) {
size_t rev = 0, x = k;
for(size_t c = count; c > 1; c >>= 1) {
rev = (rev << 1) | (x & 1);
x >>= 1;
}
long double angle = std::numbers::pi_v<long double> * (1 + 4 * rev) / (16 * count);
if(k & 1) {
angle -= std::numbers::pi_v<long double> / 4;
}
for(size_t j = 1; j < 8; j++) {
long double a = j * angle;
if constexpr(Mode == 0) {
table[(count - 1) / 7 + k][j - 1] = {double(cosl(a)), double(sinl(a))};
} else {
table[(count - 1) / 7 + k][j - 1] = {double(sinl(a)), double(tanl(a / 2))};
}
}
}
}
return table;
}();
auto lift = [&] [[gnu::always_inline]] (size_t idx) -> vpoint {
i32x4 bits{};
if(idx + 4 <= fa.count) {
std::memcpy(&bits, fa.src + idx, sizeof(bits));
} else if(idx < fa.count) {
for(size_t j = 0; j < fa.count - idx; j++) {
bits[j] = int32_t(fa.src[idx + j]);
}
} else {
return vpoint{vz, vz};
}
auto x = __builtin_convertvector(bits, vftype);
u32x4 h =
u32x4{uint32_t(idx), uint32_t(idx + 1), uint32_t(idx + 2), uint32_t(idx + 3)} ^ uint32_t(fa.seed);
h *= 0x9E3779B1u;
h ^= h >> 15;
h *= 0x85EBCA77u;
h ^= h >> 13;
h *= 0xC2B2AE3Du;
h ^= h >> 16;
auto noise = __builtin_convertvector(i32x4(h), vftype) * 0x1p-32;
auto q = round(x * fa.a_over_p + noise), t = round(x * fa.b_over_p + noise);
auto re = x - q * fa.a - t * fa.b, im = t * fa.a - q * fa.b;
return vpoint{re, Neg ? -im : im};
};
auto stage = [&]<bool top> [[gnu::always_inline]] (size_t offset, size_t length, size_t begin, size_t end) {
size_t step = length / 8, k = offset / length;
auto const& w = weights[(n / length - 1) / 7 + k];
auto rot = [&]<size_t J> [[gnu::always_inline]] (vpoint z) {
auto c = w[J - 1];
if constexpr(Mode == 0) {
return z * vpoint{vz + real(c), inverse ? vz - imag(c) : vz + imag(c)};
} else {
auto s = inverse ? vz - real(c) : vz + real(c), t = inverse ? vz - imag(c) : vz + imag(c);
auto x = vftype(_mm256_fnmadd_pd(__m256d(t), __m256d(imag(z)), __m256d(real(z))));
auto y = vftype(_mm256_fmadd_pd(__m256d(s), __m256d(x), __m256d(imag(z))));
return vpoint{vftype(_mm256_fnmadd_pd(__m256d(t), __m256d(y), __m256d(x))), y};
}
};
auto add = [] [[gnu::always_inline]] (vpoint a, vpoint b) {
if constexpr(Mode != 2) {
return a + b;
} else {
return vpoint{vftype(_mm256_fmadd_pd(__m256d(real(a)), _mm256_set1_pd(1), __m256d(real(b)))),
vftype(_mm256_fmadd_pd(__m256d(imag(a)), _mm256_set1_pd(1), __m256d(imag(b))))};
}
};
auto sub = [] [[gnu::always_inline]] (vpoint a, vpoint b) {
if constexpr(Mode != 2) {
return a - b;
} else {
return vpoint{vftype(_mm256_fmsub_pd(__m256d(real(a)), _mm256_set1_pd(1), __m256d(real(b)))),
vftype(_mm256_fmsub_pd(__m256d(imag(a)), _mm256_set1_pd(1), __m256d(imag(b))))};
}
};
auto d4 = [] [[gnu::always_inline]] (vpoint a, vpoint b, vpoint c, vpoint d) {
auto s = a + c, t = a - c, u = b + d, v = vi(b - d);
if constexpr(inverse) {
return std::array<vpoint, 4>{s + u, t - v, s - u, t + v};
} else {
return std::array<vpoint, 4>{s + u, t + v, s - u, t - v};
}
};
constexpr double q = 0.707106781186547524400844362104849039;
auto r1 = [] [[gnu::always_inline]] (vpoint z) {
if constexpr(inverse) {
return vpoint{(real(z) + imag(z)) * q, (imag(z) - real(z)) * q};
} else {
return vpoint{(real(z) - imag(z)) * q, (real(z) + imag(z)) * q};
}
};
auto r3 = [] [[gnu::always_inline]] (vpoint z) {
if constexpr(inverse) {
return vpoint{(imag(z) - real(z)) * q, (-imag(z) - real(z)) * q};
} else {
return vpoint{(-real(z) - imag(z)) * q, (real(z) - imag(z)) * q};
}
};
std::array<vpoint*, 8> input, output;
for(size_t j = 0; j < 8; j++) {
input[j] = output[j] = r.data() + (offset + begin + j * step) / flen;
}
if(k & 1) {
constexpr std::array<size_t, 8> perm = {7, 6, 4, 5, 0, 1, 2, 3};
for(size_t j = 0; j < 8; j++) {
if constexpr(inverse) {
input[j] = output[perm[j]];
} else {
output[j] = input[perm[j]];
}
}
}
constexpr bool fused_in = top && Fuse == 1 && !inverse;
for(size_t j = 0; j < (end - begin) / flen; j++) {
size_t base = offset + begin + j * flen;
auto in = [&]<size_t S> [[gnu::always_inline]] () {
if constexpr(fused_in) {
return lift(base + S * step);
} else {
return input[S][j];
}
};
if constexpr(!inverse) {
auto E =
d4(in.template operator()<0>(), rot.template operator()<2>(in.template operator()<2>()),
rot.template operator()<4>(in.template operator()<4>()),
rot.template operator()<6>(in.template operator()<6>()));
auto O = d4(rot.template operator()<1>(in.template operator()<1>()),
rot.template operator()<3>(in.template operator()<3>()),
rot.template operator()<5>(in.template operator()<5>()),
rot.template operator()<7>(in.template operator()<7>()));
auto a = O[0], b = r1(O[1]), c = vi(O[2]), d = r3(O[3]);
output[0][j] = add(E[0], a);
output[1][j] = sub(E[0], a);
output[2][j] = add(E[2], c);
output[3][j] = sub(E[2], c);
output[4][j] = add(E[1], b);
output[5][j] = sub(E[1], b);
output[6][j] = add(E[3], d);
output[7][j] = sub(E[3], d);
} else {
auto E = d4(add(input[0][j], input[1][j]), add(input[4][j], input[5][j]),
add(input[2][j], input[3][j]), add(input[6][j], input[7][j]));
auto O = d4(sub(input[0][j], input[1][j]), r1(sub(input[4][j], input[5][j])),
-vi(sub(input[2][j], input[3][j])), r3(sub(input[6][j], input[7][j])));
output[0][j] = E[0];
output[1][j] = rot.template operator()<1>(O[0]);
output[2][j] = rot.template operator()<2>(E[1]);
output[3][j] = rot.template operator()<3>(O[1]);
output[4][j] = rot.template operator()<4>(E[2]);
output[5][j] = rot.template operator()<5>(O[2]);
output[6][j] = rot.template operator()<6>(E[3]);
output[7][j] = rot.template operator()<7>(O[3]);
}
}
};
constexpr size_t h = n / 64;
for(size_t j = 0; j < h; j += Tile) {
size_t end = std::min(h, j + Tile);
auto first = [&]() {
for(size_t t = 0; t < 8; t++) {
stage.template operator()<true>(0, n, j + t * h, end + t * h);
}
};
auto second = [&]() {
for(size_t k = 0; k < 8; k++) {
stage.template operator()<false>(k * n / 8, n / 8, j, end);
}
};
if constexpr(inverse) {
second();
first();
} else {
first();
second();
}
}
}
// Product for n = 2^24: the top three stages split each input into 64 independent
// blocks. Both forward transforms read the u32 inputs directly; each block then
// completes its two forward transforms, the product, and the inverse before the
// final three inverse stages combine the results.
// Fusing the Gaussian lift into the first pass saves a write and a read of each
// spectrum, but makes that pass compute-bound on the judge (measured slower there),
// so it stays opt-in; by default both spectra are filled first and swept in place.
static constexpr bool fuse_forward = false;
template<bool Neg, bool Fused = fuse_forward, bool Square = false>
void cache_product(cvector& b, fuse_args const& fa = {}, fuse_args const& fb = {}) {
constexpr size_t n = 1 << 24, block = 1 << 18;
prepare_roots(n / 16);
prepare_shear_roots();
if constexpr(Fused) {
sweep8<false, 2, sweep_tile, 1, Neg>(fa);
if constexpr(!Square) {
b.sweep8<false, 2, sweep_tile, 1, Neg>(fb);
}
} else {
sweep8<false, 2, sweep_tile>();
if constexpr(!Square) {
b.sweep8<false, 2, sweep_tile>();
}
}
checkpoint("sweep forward");
for(size_t offset = 0; offset < n; offset += block) {
transform<false, n, block, true>(n, false, offset, block);
if constexpr(!Square) {
b.transform<false, n, block, true>(n, false, offset, block);
}
dot_fused16<n, Square>(b, offset, block);
transform<true, n, block, true>(n, false, offset, block);
}
checkpoint("blocks");
sweep8<true, 2, sweep_tile>();
checkpoint("sweep inverse");
}
static constexpr size_t pre_evals = 1 << 16;
static const std::array<size_t, pre_evals> eval_args;
static const std::array<point, pre_evals> evalp;
private:
// The power of four that the fused product works on: all of n = 4^k, half of n = 2 * 4^k.
size_t fused_part() const {
return size() >> (std::countr_zero(size()) % 2);
}
bool fused_leaves() const {
return fused_part() >= 16 * dot_tile;
}
// Tile two radix-four stages together before descending into each child.
template<bool inverse, size_t fixed = 0, size_t range_fixed = 0, bool omit16 = false>
void transform(size_t input_n, bool parity, size_t range_offset = 0, size_t range_length = 0) {
if constexpr(range_fixed) {
range_length = range_fixed;
}
const size_t n = fixed ? fixed : input_n;
if constexpr(!range_fixed) {
prepare_roots(n / 16);
if constexpr(fixed == (1 << 24)) {
prepare_shear_roots();
}
}
size_t log_n = std::countr_zero(n);
auto butterfly = [&] [[gnu::always_inline]] (size_t offset, size_t length, size_t begin, size_t end) {
if constexpr(omit16) {
if(length == 16) {
return;
}
}
size_t step = length / 4, log_length = std::countr_zero(length), k = offset >> log_length;
auto *p0 = r.data() + (offset + begin) / flen, *p1 = r.data() + (offset + begin + step) / flen,
*p2 = r.data() + (offset + begin + 2 * step) / flen,
*p3 = r.data() + (offset + begin + 3 * step) / flen;
auto run = [&]<bool shear> [[gnu::always_inline]] () {
vpoint v1, v2, v3;
vftype t1{}, t2{}, t3{};
if constexpr(shear && fixed == (1 << 24)) {
auto const& c = shear_roots[((n >> log_length) - 1) / 3 + k];
v1 = {vz, inverse ? vz - real(c[0]) : vz + real(c[0])};
v2 = {vz, inverse ? vz - real(c[1]) : vz + real(c[1])};
v3 = {vz, inverse ? vz - real(c[2]) : vz + real(c[2])};
t1 = inverse ? vz - imag(c[0]) : vz + imag(c[0]);
t2 = inverse ? vz - imag(c[1]) : vz + imag(c[1]);
t3 = inverse ? vz - imag(c[2]) : vz + imag(c[2]);
} else {
point e = k < pre_evals ? evalp[k] : extra[k - pre_evals];
point rt = roots[log_n + 5 - log_length] * e;
v1 = {vz + real(rt), inverse ? vz - imag(rt) : vz + imag(rt)};
if constexpr(shear) {
if(k & 1) {
if constexpr(inverse) {
v1 = vi(v1);
} else {
v1 = -vi(v1);
}
}
}
v2 = v1 * v1;
v3 = v1 * v2;
if constexpr(shear) {
t1 = imag(v1) / (vz + 1.0 + real(v1));
t2 = imag(v2) / (vz + 1.0 + real(v2));
t3 = imag(v3) / (vz + 1.0 + real(v3));
}
}
auto rotate = [] [[gnu::always_inline]] (vpoint z, vpoint v, vftype t) {
if constexpr(!shear) {
return z * v;
} else {
auto x = vftype(_mm256_fnmadd_pd(__m256d(t), __m256d(imag(z)), __m256d(real(z))));
auto y = vftype(_mm256_fmadd_pd(__m256d(imag(v)), __m256d(x), __m256d(imag(z))));
return vpoint{vftype(_mm256_fnmadd_pd(__m256d(t), __m256d(y), __m256d(x))), y};
}
};
auto *i0 = p0, *i1 = p1, *i2 = p2, *i3 = p3, *o0 = p0, *o1 = p1, *o2 = p2, *o3 = p3;
if constexpr(shear) {
if(k & 1) {
if constexpr(inverse) {
i0 = p3;
i1 = p2;
i2 = p0;
i3 = p1;
} else {
o0 = p3;
o1 = p2;
o2 = p0;
o3 = p1;
}
}
}
for(size_t j = 0; j < (end - begin) / flen; j++) {
auto A = i0[j], B = i1[j], C = i2[j], D = i3[j];
if constexpr(inverse) {
o0[j] = (A + B) + (C + D);
o2[j] = rotate((A + B) - (C + D), v2, t2);
o1[j] = rotate((A - B) - vi(C - D), v1, t1);
o3[j] = rotate((A - B) + vi(C - D), v3, t3);
} else {
B = rotate(B, v1, t1);
C = rotate(C, v2, t2);
D = rotate(D, v3, t3);
o0[j] = (A + C) + (B + D);
o1[j] = (A + C) - (B + D);
o2[j] = (A - C) + vi(B - D);
o3[j] = (A - C) - vi(B - D);
}
}
};
if(length >= shear_min<fixed>) {
run.template operator()<true>();
} else {
run.template operator()<false>();
}
};
auto recurse = [&](this auto const& self, size_t offset, size_t length) -> void {
if(length < 4 * flen) {
return;
}
if(length >= (1 << 15)) {
size_t step = length / 16;
if constexpr(inverse) {
for(size_t t = 0; t < 16; t++) {
self(offset + t * step, step);
}
}
for(size_t j = 0; j < step; j += 256) {
size_t end = std::min(step, j + 256);
if constexpr(inverse) {
for(size_t t = 0; t < 4; t++) {
butterfly(offset + t * length / 4, length / 4, j, end);
}
for(size_t t = 0; t < 4; t++) {
butterfly(offset, length, j + t * step, end + t * step);
}
} else {
for(size_t t = 0; t < 4; t++) {
butterfly(offset, length, j + t * step, end + t * step);
}
for(size_t t = 0; t < 4; t++) {
butterfly(offset + t * length / 4, length / 4, j, end);
}
}
}
if constexpr(!inverse) {
for(size_t t = 0; t < 16; t++) {
self(offset + t * step, step);
}
}
} else if(length >= (size_t(1) << (6 + parity))) {
auto finish = [&]<bool par>() {
constexpr size_t chunk = size_t(1) << (6 + par);
constexpr size_t bottom = size_t(1) << (4 + par);
auto small = [&]<size_t L> [[gnu::always_inline]] (this auto const& self, size_t pos) -> void {
if constexpr(inverse && L > bottom) {
for(size_t q = 0; q < 4; q++) {
self.template operator()<L / 4>(pos + q * (L / 4));
}
}
butterfly(pos, L, 0, L / 4);
if constexpr(!inverse && L > bottom) {
for(size_t q = 0; q < 4; q++) {
self.template operator()<L / 4>(pos + q * (L / 4));
}
}
};
for(size_t leaf = offset; leaf < offset + length; leaf += chunk) {
if constexpr(inverse) {
small.template operator()<chunk>(leaf);
size_t level =
std::min<size_t>(std::countr_one(leaf + chunk - 1), std::countr_zero(length));
for(size_t lvl = 6 + 2 + par; lvl <= level; lvl += 2) {
size_t len = size_t(1) << lvl;
butterfly(leaf & ~(len - 1), len, 0, len / 4);
}
} else {
size_t level = std::min<size_t>(std::countr_zero(n + leaf), std::countr_zero(length));
level -= level % 2 != par;
for(size_t lvl = level; lvl >= 6 + 2 + par; lvl -= 2) {
size_t len = size_t(1) << lvl;
butterfly(leaf & ~(len - 1), len, 0, len / 4);
}
small.template operator()<chunk>(leaf);
}
}
};
if(parity) {
finish.template operator()<true>();
} else {
finish.template operator()<false>();
}
} else {
if constexpr(inverse) {
for(size_t leaf = offset + 3 * flen; leaf < offset + length; leaf += 4 * flen) {
size_t level = std::min<size_t>(std::countr_one(leaf + 3), std::countr_zero(length));
for(size_t lvl = 4 + parity; lvl <= level; lvl += 2) {
size_t len = size_t(1) << lvl;
butterfly(leaf & ~(len - 1), len, 0, len / 4);
}
}
} else {
for(size_t leaf = offset; leaf < offset + length; leaf += 4 * flen) {
size_t level = std::min<size_t>(std::countr_zero(n + leaf), std::countr_zero(length));
level -= level % 2 != parity;
for(size_t lvl = level; lvl >= 4; lvl -= 2) {
size_t len = size_t(1) << lvl;
butterfly(leaf & ~(len - 1), len, 0, len / 4);
}
}
}
}
};
// Radix two is performed separately at the leaves.
recurse(range_offset, range_length ? range_length : n);
}
// Shortest butterfly that uses the shear rotation; the fixed 2^24 transform reads its
// shear roots from a table, so they are worth using down to the last in-block level.
template<size_t fixed>
static constexpr size_t shear_min = fixed == (1 << 24) ? 64 : 256;
static big_vector<std::array<point, 3>> shear_roots;
static void prepare_shear_roots() {
constexpr size_t n = 1 << 24;
if(!shear_roots.empty()) {
return;
}
shear_roots.resize((n / 16 - 1) / 3, std::array<point, 3>{});
for(size_t len = n; len >= shear_min<n>; len /= 4) {
size_t count = n / len, base = (count - 1) / 3;
point factor = roots[29 - std::countr_zero(len)];
for(size_t k = 0; k < count; k++) {
point rt = factor * (k < pre_evals ? evalp[k] : extra[k - pre_evals]);
vpoint v1 = {vz + real(rt), vz + imag(rt)};
if(k & 1) {
v1 = -vi(v1);
}
vpoint v2 = v1 * v1, v3 = v1 * v2;
vftype t1 = imag(v1) / (vz + 1.0 + real(v1)), t2 = imag(v2) / (vz + 1.0 + real(v2)),
t3 = imag(v3) / (vz + 1.0 + real(v3));
shear_roots[base + k] = {point(imag(v1)[0], t1[0]), point(imag(v2)[0], t2[0]),
point(imag(v3)[0], t3[0])};
}
}
}
static big_vector<point> extra;
// Keep the usual table small; cache additional roots for large transforms.
static void prepare_roots(size_t n) {
if(n <= pre_evals + extra.size()) {
return;
}
size_t old = extra.size();
extra.resize(std::bit_ceil(n) - pre_evals);
static const std::array<point, 256> coarse = []() {
std::array<point, 256> out;
for(size_t i = 0; i < 256; i++) {
out[i] = polar<ftype>(1., std::numbers::pi * double((eval_args[256 + i] - 1) / 2) / 512.0);
}
return out;
}();
for(size_t h = pre_evals + old; h < pre_evals + extra.size(); h *= 2) {
for(size_t i = h; i < 2 * h; i += 256) {
point fine = polar<ftype>(1., std::numbers::pi / double(4 * h) * double(eval_arg(4 * i)));
for(size_t j = 0; j < 256; j++) {
extra[i + j - pre_evals] = coarse[j] * fine;
}
}
}
}
};
big_vector<std::array<point, 3>> cvector::shear_roots;
big_vector<point> cvector::extra;
const std::array<size_t, cvector::pre_evals> cvector::eval_args = []() {
std::array<size_t, pre_evals> res = {};
for(size_t i = 1; i < pre_evals; i++) {
res[i] = res[i >> 1] | (i & 1) << (std::bit_width(i) - 1);
}
return res;
}();
const std::array<point, cvector::pre_evals> cvector::evalp = []() {
std::array<point, pre_evals> res = {};
res[0] = 1;
for(size_t n = 1; n < pre_evals; n++) {
res[n] = polar<ftype>(1., std::numbers::pi * ftype(eval_args[n]) / ftype(4 * std::bit_floor(n)));
}
return res;
}();
}
#pragma GCC pop_options
#line 12 "cp-algo/math/ring.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::fft {
size_t com_size(size_t as, size_t bs) {
if(!as || !bs) {
return 0;
}
return std::max(flen, std::bit_ceil(as + bs - 1) / 2);
}
// Whether a product of these sizes exceeds half of its padded length by a tail short enough
// to be corrected naively, which lets mul_truncate halve its transform.
constexpr size_t short_tail = 32;
bool has_short_tail(size_t as, size_t bs) {
size_t n = com_size(as, bs);
return as + bs - 1 - n <= short_tail && as <= n && bs <= n;
}
// Convolution through an imaginary quadratic ring, for a prime modulus p < 2^31.
// Let d be the smallest positive integer such that -d is a quadratic residue (d = 1 exactly
// when p = 1 mod 4) and root^2 = -d. A residue x is written as re + im*sqrt(-d) with
// re + im*root = x, (re, im) reduced by the lattice of representations of zero under the norm
// re^2 + d*im^2, so that |re| and sqrt(d)*|im| are of order sqrt(p), and is embedded as the
// complex number re + i*sqrt(d)*im. The product is then one complex convolution instead of
// three real ones. For d = 1 (Gaussian integers) it is computed modulo x^n - i and x^n + i
// (the latter through conjugation) and recombined modulo p, which needs i to lie in the ring;
// for any other d it is a single transform of the full product length.
constexpr uint64_t pow_mod(uint64_t x, uint64_t e, uint64_t p) {
uint64_t res = 1;
for(x %= p; e; e >>= 1, x = x * x % p) {
if(e & 1) {
res = res * x % p;
}
}
return res;
}
// Smallest d > 0 with -d a quadratic residue modulo the odd prime p < 2^31, or 0.
constexpr uint32_t least_negated_residue(uint64_t p) {
for(uint32_t d = 1; d < 256 && d < p; d++) {
if(pow_mod(p - d, (p - 1) / 2, p) == 1) {
return d;
}
}
return 0;
}
template<modint_type base>
struct quadratic {
static inline bool ready = false, available = false;
static inline uint32_t d = 0, root = 0, prime = 0;
// (a, b) and (a2, b2): reduced basis of the pairs (re, im) that represent zero.
static inline int32_t a = 0, b = 0, a2 = 0, b2 = 0;
// A fixed modulus settles d at compile time, so only the path in use is instantiated.
static constexpr bool fixed_mod = requires { typename std::bool_constant<(base::mod(), true)>; };
static constexpr uint32_t fixed_d = [] {
if constexpr(fixed_mod) {
return base::mod() > 2 && base::mod() % 2 ? least_negated_residue(base::mod()) : 0u;
} else {
return 0u;
}
}();
static void init() {
if(ready && prime == uint32_t(base::mod())) {
return;
}
ready = true;
available = false;
uint64_t p = base::mod();
prime = uint32_t(p);
if(p < 3 || p >= (uint64_t(1) << 31) || !is_prime(p)) {
return;
}
d = least_negated_residue(p);
auto s = d ? cp_algo::math::sqrt(base(-int64_t(d))) : std::nullopt;
if(!s) {
return;
}
root = s->getr();
// Lagrange reduction of (p, 0), (-root, 1) under the norm x^2 + d*y^2.
using wide = __int128;
wide x1 = p, y1 = 0, x2 = -wide(root), y2 = 1;
auto norm = [&](wide x, wide y) {
return x * x + d * y * y;
};
while(true) {
if(norm(x1, y1) > norm(x2, y2)) {
std::swap(x1, x2);
std::swap(y1, y2);
}
wide dot = x1 * x2 + d * y1 * y2, len = norm(x1, y1);
wide m = (2 * dot + (dot >= 0 ? len : -len)) / (2 * len);
if(m == 0 || norm(x2 - m * x1, y2 - m * y1) >= norm(x2, y2)) {
break;
}
x2 -= m * x1;
y2 -= m * y1;
}
a = int32_t(x1);
b = int32_t(y1);
// In the Gaussian integers i*(a, b) = (-b, a) lies in the lattice as well.
if(d == 1) {
a2 = b;
b2 = -a;
} else {
a2 = int32_t(x2);
b2 = int32_t(y2);
}
assert(base(a) + base(b) * base(root) == base(0) && base(a2) + base(b2) * base(root) == base(0));
assert(std::abs(int64_t(a) * b2 - int64_t(a2) * b) == int64_t(p));
available = true;
}
// Points per transform: two branches of half the padded product length for d = 1, one
// transform of the whole length otherwise. A short tail beyond that is computed naively
// and taken back out, as in mul_truncate, which halves the transform: over the residues
// for d = 1, where the branches together work modulo x^(2n) + 1, and in ring coordinates
// for the single transform, which works modulo x^n - i.
static size_t length(size_t as, size_t bs) {
return (d == 1 ? com_size(as, bs) : 2 * com_size(as, bs)) >> has_short_tail(as, bs);
}
// Whether this path is used for operands of these sizes. With n = com_size(as, bs) it
// takes six transforms of n points (three of 2n for d > 1), four for a square, where the
// split representation takes seven and five, so it is preferred whenever it is exact,
// unless a transform exceeds 2^24 points, the only length with a kernel tiled for data
// outside the caches (which in turn does not let an operand wrap around).
// Empirical rounding budget. Reusing a spectrum for a tiled square correlates its
// errors, so that optimization uses a stricter budget below and otherwise keeps
// independent lifts of the two operands.
static bool usable(size_t as, size_t bs) {
init();
if(!available || std::min(as, bs) < size_t(magic)) {
return false;
}
size_t need = as + bs - 1, n = length(as, bs);
if(n > (1 << 24) || (n == (1 << 24) && d == 1 && std::max(as, bs) > n)) {
return false;
}
using wide = unsigned __int128;
return wide(need) * d * base::mod() * base::mod() <= wide(1) << 90;
}
// Lattice constants as doubles, hoisted out of the hot loops (the statics are 32-bit
// integers and could alias the 32-bit output stream).
struct lattice {
// (c, e): the basis vector with the larger second coordinate, used to shrink im.
double a, b, a2, b2, to_q, to_t, c, e, inv_e, root, scale, inv_scale, p;
lattice(): a(quadratic::a), b(quadratic::b), a2(quadratic::a2), b2(quadratic::b2), p(base::mod()) {
double det = a * b2 - a2 * b;
to_q = b2 / det;
to_t = -b / det;
bool first = std::abs(b) >= std::abs(b2);
c = first ? a : a2;
e = first ? b : b2;
inv_e = 1.0 / e;
root = quadratic::root;
scale = std::sqrt(double(d));
inv_scale = 1.0 / scale;
}
};
// Map a rounded coordinate pair back to the residue re + root*im.
template<bool unit>
static u32x4 project(vpoint value, bool negative, lattice const& L) {
const double p = L.p;
auto R = round(real(value)), I = negative ? -imag(value) : imag(value);
if constexpr(unit) {
I = round(I);
} else {
I = round(I * L.inv_scale);
}
auto q = round(I * L.inv_e);
auto U = vftype(_mm256_fnmadd_pd(__m256d(q), _mm256_set1_pd(L.c), __m256d(R)));
auto V = vftype(_mm256_fnmadd_pd(__m256d(q), _mm256_set1_pd(L.e), __m256d(I)));
auto h = vftype(_mm256_fmadd_pd(__m256d(V), _mm256_set1_pd(L.root), __m256d(U)));
q = round(h * (1.0 / p));
auto out = vftype(_mm256_fnmadd_pd(__m256d(q), _mm256_set1_pd(p), __m256d(h)));
out = out < 0 ? out + p : out;
return u32x4(_mm256_cvttpd_epi32(__m256d(out)));
}
// Lift residues to ring coordinates with stochastic rounding.
// The xorshift stream is advanced once per 8 residues and restarted from the same
// seed for both branches, so both see the same representatives.
// With stream set the spectrum is written with non-temporal stores and left for
// cache_product to transform: for n = 2^24 it is far larger than the caches and is next
// read by a separate pass, so this saves the read-for-ownership of every destination line.
// Coefficients from n on (d = 1 only) wrap around: x^n = i in the branch modulo x^n - i,
// and the conjugated branch modulo x^n + i sees conj(-i * z) = i * conj(z).
template<bool stream, bool unit>
static void fill(cvector& c, auto const& x, auto const& upper, size_t n, bool negative, u64x4 state,
bool transform = true) {
const lattice L;
auto const* src = reinterpret_cast<const uint32_t*>(std::data(x));
size_t count = std::size(x);
assert(count <= n && (std::empty(upper) || (unit && !stream && count == n)));
c.r.resize(n / flen);
auto *dst = reinterpret_cast<double*>(c.r.data());
u32x8 words{};
auto emit = [&]<bool wrap = false> [[gnu::always_inline]] (size_t i, i32x4 bits) {
auto v = __builtin_convertvector(bits, vftype);
if(i % 8 == 0) {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
words = u32x8(state);
}
i32x4 small = i % 8 ? i32x4(__builtin_shufflevector(words, words, 1, 3, 5, 7))
: i32x4(__builtin_shufflevector(words, words, 0, 2, 4, 6));
auto noise = __builtin_convertvector(small, vftype) * 0x1p-32;
auto q = round(v * L.to_q + noise), t = round(v * L.to_t + noise);
auto re = v - q * L.a - t * L.a2, im = -(q * L.b) - t * L.b2;
if constexpr(!unit) {
im = im * L.scale;
}
if constexpr(stream) {
_mm256_stream_pd(dst + 2 * i, __m256d(re));
_mm256_stream_pd(dst + 2 * i + flen, __m256d(negative ? -im : im));
} else if constexpr(wrap) {
c.r[(i - n) / flen] += vpoint{negative ? im : -im, re};
} else {
c.r[i / flen] = vpoint{re, negative ? -im : im};
}
};
size_t full = count / flen * flen;
for(size_t i = 0; i < full; i += flen) {
i32x4 bits;
std::memcpy(&bits, src + i, sizeof(bits));
emit(i, bits);
}
if(full < count) {
i32x4 bits{};
for(size_t j = full; j < count; j++) {
bits[j - full] = int32_t(src[j]);
}
emit(full, bits);
}
if constexpr(stream) {
_mm_sfence();
}
std::fill(c.r.begin() + (count + flen - 1) / flen, c.r.end(), vpoint{});
auto const* wrapped = reinterpret_cast<const uint32_t*>(std::data(upper));
for(size_t i = 0; i < std::size(upper); i += flen) {
i32x4 bits{};
for(size_t j = i; j < std::min(i + flen, std::size(upper)); j++) {
bits[j - i] = int32_t(wrapped[j]);
}
emit.template operator()<true>(n + i, bits);
}
checkpoint("quadratic init");
if constexpr(!stream) {
if(transform) {
c.forward();
}
}
}
// Lift a range of residues to ring coordinates, one point per coefficient, for the
// reusable transforms below. Unlike fill it reads through the modint interface, so it
// serves views and Montgomery storage alike, and it neither wraps nor streams.
template<bool unit>
static void lift(cvector& c, auto const& x, size_t n, bool negative, u64x4 state) {
const lattice L;
size_t total = std::size(x), count = std::min(n, total);
assert(total <= 2 * n && (d == 1 || total <= n));
c.r.resize(n / flen);
u32x8 words{};
size_t step = 0;
auto emit = [&]<bool wrap> [[gnu::always_inline]] (size_t i, i32x4 bits) {
auto v = __builtin_convertvector(bits, vftype);
if(step % 2 == 0) {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
words = u32x8(state);
}
i32x4 small = step++ % 2 ? i32x4(__builtin_shufflevector(words, words, 1, 3, 5, 7))
: i32x4(__builtin_shufflevector(words, words, 0, 2, 4, 6));
auto noise = __builtin_convertvector(small, vftype) * 0x1p-32;
auto q = round(v * L.to_q + noise), t = round(v * L.to_t + noise);
auto re = v - q * L.a - t * L.a2, im = -(q * L.b) - t * L.b2;
if constexpr(!unit) {
im = im * L.scale;
}
// Coefficients from n on wrap around: x^n is i in this branch, -i in the other.
if constexpr(wrap) {
c.r[(i - n) / flen] += vpoint{negative ? im : -im, re};
} else {
c.r[i / flen] = vpoint{re, negative ? -im : im};
}
};
// Residues are raw words exactly when the modulus is a compile-time constant; wider
// storage than the residue needs is narrowed on the way in.
constexpr bool plain = fixed_mod && std::ranges::contiguous_range<std::decay_t<decltype(x)>>;
constexpr bool raw = plain && sizeof(base) == 4;
constexpr bool wide = plain && sizeof(base) == 8;
auto load = [&](size_t i, size_t upto) {
i32x4 bits{};
if constexpr(raw || wide) {
if(i + flen <= upto) {
if constexpr(raw) {
std::memcpy(&bits, reinterpret_cast<const uint32_t*>(std::data(x)) + i, sizeof(bits));
} else {
u64x4 words;
std::memcpy(&words, reinterpret_cast<const uint64_t*>(std::data(x)) + i, sizeof(words));
bits = __builtin_convertvector(words, i32x4);
}
return bits;
}
}
for(size_t j = i; j < std::min(i + flen, upto); j++) {
bits[j - i] = int32_t(x[j].getr());
}
return bits;
};
for(size_t i = 0; i < count; i += flen) {
emit.template operator()<false>(i, load(i, count));
}
std::fill(c.r.begin() + (count + flen - 1) / flen, c.r.end(), vpoint{});
for(size_t i = n; i < total; i += flen) {
emit.template operator()<true>(i, load(i, total));
}
checkpoint("quadratic init");
}
// Read a product out of its transformed branches, applying the inverse-transform scale.
// For d = 1 the branches hold the product modulo x^n - i and modulo x^n + i, so the low
// half of the result is their half-sum and the high half their half-difference over i.
// The half of the result that the branches are recombined into is parked in the output
// itself, which is always long enough: the high half is only wanted where the low half
// has already been read back.
static void recover(std::array<cvector, 2> const& parts, size_t n, double factor, auto &out, size_t k) {
// Residues are raw 32-bit words exactly when the modulus is a compile-time constant,
// which is what lets the recombination stay vectorized.
constexpr bool plain = fixed_mod && std::ranges::contiguous_range<std::decay_t<decltype(out)>>;
constexpr bool raw = plain && sizeof(base) == 4;
constexpr bool wide = plain && sizeof(base) == 8;
const lattice L;
auto scale = vz + factor;
size_t low = std::min(k, n);
auto project_at = [&](cvector const& part, size_t i, bool negative) {
if(d == 1) {
return project<true>(part.at(i) * scale, negative, L);
} else {
return project<false>(part.at(i) * scale, negative, L);
}
};
// Eight coefficients at a time, the width the recombination works in.
auto project8 = [&](cvector const& part, size_t i, bool negative, size_t count) {
auto lo8 = project_at(part, i, negative);
auto hi8 = count > flen ? project_at(part, i + flen, negative) : u32x4{};
return __builtin_shufflevector(lo8, hi8, 0, 1, 2, 3, 4, 5, 6, 7);
};
auto store8 = [&](size_t idx, u32x8 v, size_t count) {
if constexpr(raw) {
if(count == 8) {
std::memcpy(reinterpret_cast<uint32_t*>(std::data(out)) + idx, &v, sizeof(v));
return;
}
} else if constexpr(wide) {
if(count == 8) {
auto words = reinterpret_cast<uint64_t*>(std::data(out)) + idx;
auto lo4 = __builtin_convertvector(u32x4{v[0], v[1], v[2], v[3]}, u64x4);
auto hi4 = __builtin_convertvector(u32x4{v[4], v[5], v[6], v[7]}, u64x4);
std::memcpy(words, &lo4, sizeof(lo4));
std::memcpy(words + flen, &hi4, sizeof(hi4));
return;
}
}
for(size_t l = 0; l < count; l++) {
out[idx + l].setr(typename base::UInt(v[l]));
}
};
auto load8 = [&](size_t idx, size_t count) {
u32x8 v{};
if constexpr(raw) {
if(count == 8) {
std::memcpy(&v, reinterpret_cast<uint32_t const*>(std::data(out)) + idx, sizeof(v));
return v;
}
} else if constexpr(wide) {
if(count == 8) {
auto words = reinterpret_cast<uint64_t const*>(std::data(out)) + idx;
u64x4 lo4, hi4;
std::memcpy(&lo4, words, sizeof(lo4));
std::memcpy(&hi4, words + flen, sizeof(hi4));
auto lo = __builtin_convertvector(lo4, u32x4), hi = __builtin_convertvector(hi4, u32x4);
return u32x8(__builtin_shufflevector(lo, hi, 0, 1, 2, 3, 4, 5, 6, 7));
}
}
for(size_t l = 0; l < count; l++) {
v[l] = uint32_t(out[idx + l].getr());
}
return v;
};
for(size_t i = 0; i < low; i += 8) {
store8(i, project8(parts[0], i, false, std::min<size_t>(8, low - i)), std::min<size_t>(8, low - i));
}
if(d > 1) {
checkpoint("quadratic recover");
return;
}
const uint32_t mod32 = uint32_t(base::mod()), imod32 = -inv2<uint32_t>(base::mod());
auto highmul = u32x8{} + uint32_t(((base(2) * base(root)).inv() * bpow(base(2), 32)).getr());
for(size_t i = 0; i < low; i += 8) {
size_t count = std::min<size_t>(8, low - i);
auto minus = project8(parts[1], i, true, count);
auto plus = load8(i, count);
auto lo8 = reduce_once(plus + minus, mod32);
lo8 = (lo8 + (lo8 & 1) * mod32) >> 1;
auto hi8 = reduce_once(montgomery_mul(plus + mod32 - minus, highmul, mod32, imod32), mod32);
store8(i, lo8, count);
if(n + i < k) {
store8(n + i, hi8, std::min<size_t>(count, k - n - i));
}
}
checkpoint("quadratic recover");
}
// Cyclic product modulo x^k - 1, in place over a, for operands of exactly k coefficients.
// The transform evaluates at the k-th roots of i, so twisting coefficient j by w^j with
// w^k = -i turns the product it computes, modulo x^k - i, into the cyclic one; the
// inverse twist is folded into the readback. A fold of the linear product would need
// twice the transform.
static void cyclic(auto &a, auto const& b, size_t k) {
init();
assert(available && std::popcount(k) == 1 && std::size(a) == k && std::size(b) == k);
bool square = (void const*)std::data(a) == (void const*)std::data(b);
// w^j from a table of every fourth power, built the way the root tables of the
// transform are, and four consecutive powers per vector by one broadcast multiply.
size_t groups = k / flen;
size_t fine_bits = std::min<size_t>(8, std::countr_zero(std::max<size_t>(groups, 1)));
size_t fine = size_t(1) << fine_bits;
big_vector<point> low(fine), high((groups + fine - 1) >> fine_bits), step(groups);
auto w = [&](size_t j) {
return polar<ftype>(1., -std::numbers::pi * ftype(j) / ftype(2 * k));
};
for(size_t t = 0; t < low.size(); t++) {
low[t] = w(flen * t);
}
for(size_t c = 0; c < high.size(); c++) {
high[c] = w(flen * (c << fine_bits));
}
for(size_t c = 0; c < groups; c++) {
step[c] = high[c >> fine_bits] * low[c & (fine - 1)];
}
vpoint quarter, quarter_conj;
for(size_t l = 0; l < flen; l++) {
point v = w(l);
real(quarter)[l] = real(v);
imag(quarter)[l] = imag(v);
real(quarter_conj)[l] = real(v);
imag(quarter_conj)[l] = -imag(v);
}
auto twiddle = [&](size_t j, bool inverse, ftype scale) {
point t = step[j / flen];
vpoint head = {vz + real(t) * scale, vz + (inverse ? -imag(t) : imag(t)) * scale};
return head * (inverse ? quarter_conj : quarter);
};
auto run = [&]<bool unit>() {
const lattice L;
cvector A(k), B(0);
lift<unit>(A, a, k, false, seed());
if(!square) {
lift<unit>(B, b, k, false, seed());
}
for(size_t j = 0; j < k; j += flen) {
auto w = twiddle(j, false, 1);
A.at(j) *= w;
if(!square) {
B.at(j) *= w;
}
}
A.forward();
if(!square) {
B.forward();
}
A.multiply(square ? A : B);
for(size_t j = 0; j < k; j += flen) {
auto v = project<unit>(A.at(j) * twiddle(j, true, ftype(flen) / ftype(k)), false, L);
for(size_t l = 0; l < flen; l++) {
a[j + l].setr(typename base::UInt(v[l]));
}
}
};
if(d == 1) {
run.template operator()<true>();
} else {
run.template operator()<false>();
}
checkpoint("quadratic recover");
}
static u64x4 seed() {
return u64x4{random::rng() | 1, random::rng() | 1, random::rng() | 1, random::rng() | 1};
}
// a <- a * b for d = 1, or a <- a * a with square set (b is not read then).
// The first branch is stored in the upper half of a, so the part of a that wraps around
// is set aside first.
static void mul_branches(auto &a, auto const& b, bool square) {
size_t as = std::size(a), bs = square ? as : std::size(b), need = as + bs - 1;
size_t n = length(as, bs);
assert(available && d == 1);
// Coefficients from 2n on come back negated at the bottom; there are few of them.
std::array<uint32_t, short_tail> high{};
size_t tail = need > 2 * n ? need - 2 * n : 0;
for(size_t i = 0; i < tail; i++) {
auto const* x = reinterpret_cast<const uint32_t*>(std::data(a));
auto const* y = square ? x : reinterpret_cast<const uint32_t*>(std::data(b));
uint64_t sum = 0;
for(size_t j = 2 * n + i - bs + 1; j < as; j++) {
sum = (sum + uint64_t(x[j]) * y[2 * n + i - j]) % prime;
}
high[i] = uint32_t(sum);
}
// Montgomery constants for the recombination of the two branches.
const uint32_t mod32 = uint32_t(base::mod()), imod32 = -inv2<uint32_t>(base::mod());
base r32 = bpow(base(2), 32);
auto highmul = u32x8{} + uint32_t(((base(2) * base(root)).inv() * r32).getr());
u64x4 seed_a = seed(), seed_b = seed();
const lattice L;
big_vector<base> a_upper(std::begin(a) + std::min(as, n), std::end(a));
a.resize(2 * n);
std::span<base const> a_lower = std::span(a).first(std::min(as, n));
// A square may come with b aliasing the storage that a has just left.
auto b_lower = square ? a_lower : std::span<base const>(b).first(std::min(bs, n));
auto b_upper = square ? std::span<base const>(a_upper) : std::span<base const>(b).subspan(b_lower.size());
cvector A(0), B(0);
auto *out = reinterpret_cast<uint32_t*>(std::data(a));
for(bool negative: {false, true}) {
if(n == (1 << 24)) {
if constexpr(cvector::fuse_forward) {
cvector::fuse_args fa{
out, as, seed_a[0], L.a, L.b, L.a / double(base::mod()), L.b / double(base::mod())};
cvector::fuse_args fb{reinterpret_cast<const uint32_t*>(std::data(b)),
bs,
seed_b[0],
fa.a,
fa.b,
fa.a_over_p,
fa.b_over_p};
if(negative) {
A.template cache_product<true>(B, fa, fb);
} else {
A.template cache_product<false>(B, fa, fb);
}
} else {
fill<true, true>(A, a_lower, a_upper, n, negative, seed_a);
if(square && __uint128_t(need) * d * prime * prime <= (__uint128_t(1) << 88)) {
if(negative) {
A.template cache_product<true, false, true>(A);
} else {
A.template cache_product<false, false, true>(A);
}
} else {
fill<true, true>(B, b_lower, b_upper, n, negative, seed_b);
if(negative) {
A.template cache_product<true>(B);
} else {
A.template cache_product<false>(B);
}
}
}
} else {
fill<false, true>(A, a_lower, a_upper, n, negative, seed_a);
if(!square) {
fill<false, true>(B, b_lower, b_upper, n, negative, seed_b);
}
A.multiply(square ? A : B);
}
auto scale = vz + double(flen) / double(n);
for(size_t i = 0; i < n; i += 8) {
auto sum0 = project<true>(A.at(i) * scale, negative, L);
auto sum1 = project<true>(A.at(i + 4) * scale, negative, L);
auto sum = __builtin_shufflevector(sum0, sum1, 0, 1, 2, 3, 4, 5, 6, 7);
if(negative) {
u32x8 plus;
std::memcpy(&plus, out + n + i, sizeof(plus));
auto lo = plus + sum;
// Unsigned reduction: these sums pass 2^31 for moduli above 2^30.
lo = reduce_once(lo, mod32);
lo = (lo + (lo & 1) * base::mod()) >> 1;
auto hi = reduce_once(montgomery_mul(plus + base::mod() - sum, highmul, mod32, imod32), mod32);
std::memcpy(out + i, &lo, sizeof(lo));
std::memcpy(out + n + i, &hi, sizeof(hi));
} else {
std::memcpy(out + n + i, &sum, sizeof(sum));
}
}
checkpoint("quadratic recover");
}
a.resize(need);
out = reinterpret_cast<uint32_t*>(std::data(a));
for(size_t i = 0; i < tail; i++) {
uint32_t sum = out[i] + high[i];
out[i] = std::min(sum, sum - prime);
out[2 * n + i] = high[i];
}
}
// a <- a * b for d > 1, or a <- a * a with square set: i is not in the ring, so two
// branches could only be recombined as complex numbers, which is slower and less exact
// than one full transform.
static void mul_single(auto &a, auto const& b, bool square) {
size_t as = std::size(a), bs = square ? as : std::size(b), need = as + bs - 1;
size_t n = length(as, bs), tail = need > n ? need - n : 0;
assert(available && d > 1);
const lattice L;
cvector A(0), B(0);
std::span<base const> none;
// Coefficients from n on, in ring coordinates: they come back multiplied by i.
std::array<point, short_tail> high{};
auto wrapped = [&](cvector const& rhs) {
for(size_t i = 0; i < tail; i++) {
for(size_t j = n + i - bs + 1; j < as; j++) {
high[i] += A.template get<point>(j) * rhs.template get<point>(n + i - j);
}
}
};
if(n == (1 << 24)) {
fill<true, false>(A, a, none, n, false, seed());
if(square && __uint128_t(need) * d * prime * prime <= (__uint128_t(1) << 88)) {
wrapped(A);
A.template cache_product<false, false, true>(A);
} else {
if(square) {
fill<true, false>(B, a, none, n, false, seed());
} else {
fill<true, false>(B, b, none, n, false, seed());
}
wrapped(B);
A.template cache_product<false>(B);
}
} else {
fill<false, false>(A, a, none, n, false, seed(), !tail);
if(!square) {
fill<false, false>(B, b, none, n, false, seed(), !tail);
}
if(tail) {
wrapped(square ? A : B);
A.forward();
if(!square) {
B.forward();
}
}
A.multiply(square ? A : B);
}
for(size_t i = 0; i < tail; i++) {
A.set(i, A.template get<point>(i) - point(0, 1) * high[i] * (double(n) / double(flen)));
}
a.resize(n);
auto *out = reinterpret_cast<uint32_t*>(std::data(a));
auto scale = vz + double(flen) / double(n);
for(size_t i = 0; i < n; i += flen) {
auto sum = project<false>(A.at(i) * scale, false, L);
std::memcpy(out + i, &sum, sizeof(sum));
}
checkpoint("quadratic recover");
a.resize(need);
out = reinterpret_cast<uint32_t*>(std::data(a));
for(size_t i = 0; i < tail; i += flen) {
vpoint lanes = {vz, vz};
for(size_t j = i; j < std::min(tail, i + flen); j++) {
real(lanes)[j - i] = real(high[j]);
imag(lanes)[j - i] = imag(high[j]);
}
auto sum = project<false>(lanes, false, L);
for(size_t j = i; j < std::min(tail, i + flen); j++) {
out[n + j] = sum[j - i];
}
}
}
// Both routines read and write the storage as plain residues, which it is for modint<m>.
// A runtime-modulus type keeps another form, so its operands are converted on the way.
static void mul(auto &a, auto const& b, bool square) {
static_assert(sizeof(std::decay_t<decltype(a[0])>) == 4);
auto run = [&](auto const& rhs) {
if constexpr(fixed_d == 1) {
mul_branches(a, rhs, square);
} else if constexpr(fixed_d > 1) {
mul_single(a, rhs, square);
} else if(d == 1) {
mul_branches(a, rhs, square);
} else {
mul_single(a, rhs, square);
}
};
if constexpr(fixed_mod) {
run(b);
} else {
big_vector<base> plain;
if(!square) {
plain.assign(std::begin(b), std::end(b));
}
for(auto &x: plain) {
x.setr_direct(x.getr());
}
for(auto &x: a) {
x.setr_direct(x.getr());
}
run(plain);
for(auto &x: a) {
x.setr(x.getr_direct());
}
}
}
};
// A polynomial transformed for repeated multiplication in the ring of quadratic<base>.
//
// capacity() is the number of product coefficients the transform represents, and both
// operands of a product must fit in it. For d = 1 the polynomial is held as two conjugate
// branches of capacity/2 points, modulo x^n - i and modulo x^n + i, whose moduli multiply
// to x^(2n) + 1; for larger d as one transform of capacity points modulo x^n - i. Either
// way a product of at most capacity coefficients does not wrap around.
//
// complete selects the layout. The default stops one radix level short, which the fused
// product of multiply() finishes; the complete transform makes a product pointwise, which
// is what an accumulated sum of products needs.
//
// A transform costs two doubles per coefficient, so the type is move-only and every copy is
// spelled clone(). multiply() and square() consume the object and only read the other
// operand, which is what lets one transform serve several products.
//
// A transform belongs to the modulus it was built under; a runtime modulus must not change
// while one is alive.
template<modint_type base, bool complete = false>
struct spectrum {
using ring = quadratic<base>;
spectrum(auto const& a, size_t capacity): cap(std::max(2 * flen, std::bit_ceil(capacity))) {
ring::init();
assert(ring::available && std::size(a) <= cap);
auto state = ring::seed();
for(size_t j = 0; j < branches(); j++) {
if(ring::d == 1) {
ring::template lift<true>(parts[j], a, points(), j == 1, state);
} else {
ring::template lift<false>(parts[j], a, points(), false, state);
}
if constexpr(complete) {
parts[j].template fft<false>();
} else {
parts[j].forward();
}
}
}
spectrum(spectrum&&) = default;
spectrum& operator=(spectrum&&) = default;
spectrum clone() const { return *this; }
size_t capacity() const { return cap; }
// out[0..k) = *this * other. Consumes *this and only reads other.
void multiply(spectrum const& other, auto &out, size_t k) && requires(!complete) {
assert(other.cap == cap && k <= cap);
for(size_t j = 0; j < branches(); j++) {
parts[j].multiply(other.parts[j]);
}
ring::recover(parts, points(), double(flen) / double(points()), out, k);
}
// out[0..k) = *this * *this, consuming *this.
void square(auto &out, size_t k) && requires(!complete) {
std::move(*this).multiply(*this, out, k);
}
private:
spectrum(spectrum const&) = default;
size_t branches() const { return ring::d == 1 ? 2 : 1; }
size_t points() const { return ring::d == 1 ? cap / 2 : cap; }
template<modint_type>
friend struct product;
std::array<cvector, 2> parts = {cvector(0), cvector(0)};
size_t cap;
};
// A sum of products of transforms, read back once. Every term costs one pointwise pass and
// the sum one inverse transform, which is what makes it worth keeping the operands around.
template<modint_type base>
struct product {
using ring = quadratic<base>;
using operand = spectrum<base, true>;
explicit product(size_t capacity): cap(std::max(2 * flen, std::bit_ceil(capacity))) {
ring::init();
assert(ring::available);
for(size_t j = 0; j < branches(); j++) {
acc[j] = cvector(points());
}
}
// *this += x * y.
void add(operand const& x, operand const& y) {
assert(x.cap == cap && y.cap == cap);
for(size_t j = 0; j < branches(); j++) {
for(size_t i = 0; i < points(); i += flen) {
acc[j].at(i) += x.parts[j].at(i) * y.parts[j].at(i);
}
}
checkpoint("dot");
}
// out[0..k) = the accumulated sum, consuming *this.
void recover(auto &out, size_t k) && {
assert(k <= cap);
for(size_t j = 0; j < branches(); j++) {
acc[j].template ifft<false>();
}
ring::recover(acc, points(), 1.0, out, k);
}
private:
size_t branches() const { return ring::d == 1 ? 2 : 1; }
size_t points() const { return ring::d == 1 ? cap / 2 : cap; }
std::array<cvector, 2> acc = {cvector(0), cvector(0)};
size_t cap;
};
}
#pragma GCC pop_options
#line 6 "cp-algo/math/fft.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::fft {
void mul_slow(auto &a, auto const& b, size_t k) {
using base = std::decay_t<decltype(a[0])>;
if(!std::empty(a) && std::data(a) == std::data(b)) {
size_t n = std::min(k, std::size(a)), m = std::min(k, std::size(b));
if(!m) {
a.clear();
return;
}
a.resize(k);
// Descending output only reads original coefficients at indices <=j.
for(size_t j = k; j-- > 0;) {
base sum = 0;
size_t lo = j >= n ? j + 1 - n : 0, hi = std::min(j + 1, m);
for(size_t i = lo; i < hi; i++) {
if(n == m && i > j - i) {
break;
}
auto term = a[i] * a[j - i];
sum += n == m && i != j - i ? term + term : term;
}
a[j] = sum;
}
return;
}
if(std::empty(a) || std::empty(b)) {
a.clear();
} else {
size_t n = std::min(k, std::size(a));
size_t m = std::min(k, std::size(b));
size_t had = std::size(a);
a.resize(k);
// The loop below reads every coefficient it writes, so the growth is zeroed here
// rather than relying on the caller's allocator to do it.
if(k > had) {
std::fill(std::begin(a) + had, std::end(a), base(0));
}
for(int j = int(k - 1); j >= 0; j--) {
a[j] *= b[0];
for(int i = std::max(j - (int)n, 0) + 1; i < std::min(j + 1, (int)m); i++) {
a[j] += a[j - i] * b[i];
}
}
}
}
// Whether two ranges are backed by the same storage, which makes a product a square.
bool same_storage(auto const& a, auto const& b) {
if constexpr(std::ranges::contiguous_range<decltype(b)>) {
return !std::empty(a) && (void const*)std::data(a) == (void const*)std::data(b);
} else {
return false;
}
}
// The product of the truncated operands, keeping k coefficients. Short operands are cheaper
// to multiply naively; every other product goes through the ring.
void mul_truncate(auto &a, auto const& b, size_t k) {
using base = std::decay_t<decltype(a[0])>;
if(std::min({k, std::size(a), std::size(b)}) < magic) {
mul_slow(a, b, k);
return;
}
size_t as = std::min(k, std::size(a)), bs = std::min(k, std::size(b));
assert(quadratic<base>::usable(as, bs) &&
"the ring needs an odd prime modulus below 2^31 and a product within its rounding bound");
bool aliased = same_storage(a, b), square = aliased && as == bs;
// Everything past the true length of the product is zero, written here rather than
// left to the caller's allocator.
size_t need = as + bs - 1, keep = std::min(k, need);
auto pad = [&] {
if(k > need) {
std::fill(std::begin(a) + need, std::end(a), base(0));
}
};
if constexpr(sizeof(base) == 4 && std::ranges::contiguous_range<decltype(b)>) {
if(aliased && !square) {
big_vector<base> copy(std::data(b), std::data(b) + bs);
a.resize(as);
quadratic<base>::mul(a, copy, false);
} else {
auto prefix = std::span<base const>(std::data(b), bs);
a.resize(as);
quadratic<base>::mul(a, prefix, square);
}
a.resize(k);
pad();
} else {
// Wider storage or a non-contiguous operand: the reusable transform reads and writes
// through the modint interface instead of the raw residues.
size_t cap = std::bit_ceil(need);
auto A = spectrum<base>(a | std::views::take(as), cap);
a.resize(k);
if(square) {
std::move(A).square(a, keep);
} else {
std::move(A).multiply(spectrum<base>(b | std::views::take(bs), cap), a, keep);
}
pad();
}
}
// Cyclic product modulo x^k - 1, in place over a.
void cyclic_mul(auto &a, auto const& b, size_t k) {
using base = std::decay_t<decltype(a[0])>;
quadratic<base>::cyclic(a, b, k);
}
namespace impl {
// Overlap-add for a short fixed operand; every block reuses its transform.
void mul_unbalanced(auto &a, auto const& b) {
using base = std::decay_t<decltype(a[0])>;
auto x = std::span<base const>(a), y = std::span<base const>(b);
if(x.size() < y.size()) {
std::swap(x, y);
}
constexpr size_t length = 1 << 15;
size_t step = length - y.size() + 1;
auto fixed = spectrum<base>(y, length);
std::decay_t<decltype(a)> result;
// Accumulated into, so the zeros are written rather than assumed.
result.assign(x.size() + y.size() - 1, base(0));
big_vector<base> work(length);
for(size_t start = 0; start < x.size(); start += step) {
size_t count = std::min(step, x.size() - start);
size_t need = count + y.size() - 1;
spectrum<base>(x.subspan(start, count), length).multiply(fixed, work, need);
for(size_t i = 0; i < need; i++) {
result[start + i] += work[i];
}
}
a = std::move(result);
}
}
void mul(auto &a, auto const& b) {
if(std::empty(a) || std::empty(b)) {
a.clear();
return;
}
size_t small = std::min(std::size(a), std::size(b));
size_t large = std::max(std::size(a), std::size(b));
if(small >= magic && small <= 4096 && large >= (1 << 20) && large / small >= 64) {
return impl::mul_unbalanced(a, b);
}
mul_truncate(a, b, std::size(a) + std::size(b) - 1);
}
}
#pragma GCC pop_options
#line 4 "cp-algo/math/poly/base.hpp"
#include <array>
#line 7 "cp-algo/math/poly/base.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
template<typename T> struct poly_t;
template<typename T>
std::array<poly_t<T>, 2> divmod(poly_t<T> p, poly_t<T> const& q);
template<typename T>
struct poly_t {
using Vector = big_vector<T>;
using base = T;
Vector a;
poly_t& normalize() {
while(deg() >= 0 && lead() == base(0)) {
a.pop_back();
}
return *this;
}
poly_t() = default;
poly_t(T a0): a{a0} {normalize();}
poly_t(Vector const& t): a(t) {normalize();}
poly_t(Vector &&t): a(std::move(t)) {normalize();}
poly_t& negate_inplace() {
std::ranges::transform(a, begin(a), std::negate{});
return *this;
}
friend poly_t operator -(poly_t p) {p.negate_inplace(); return p;}
poly_t& operator += (poly_t const& t) {
a.resize(std::max(size(a), size(t.a)));
std::ranges::transform(a, t.a, begin(a), std::plus{});
return normalize();
}
poly_t& operator -= (poly_t const& t) {
a.resize(std::max(size(a), size(t.a)));
std::ranges::transform(a, t.a, begin(a), std::minus{});
return normalize();
}
friend poly_t operator + (poly_t p, poly_t const& t) {p += t; return p;}
friend poly_t operator - (poly_t p, poly_t const& t) {p -= t; return p;}
poly_t& mod_xk_inplace(size_t k) {
a.resize(std::min(size(a), k));
return normalize();
}
poly_t& mul_xk_inplace(size_t k) {
if(is_zero()) {return *this;}
a.insert(begin(a), k, T(0));
return normalize();
}
poly_t& div_xk_inplace(int64_t k) {
if(k < 0) {
return mul_xk_inplace(-k);
}
a.erase(begin(a), begin(a) + std::min<size_t>(k, size(a)));
return normalize();
}
poly_t &substr_inplace(size_t l, size_t k) {
return mod_xk_inplace(l + k).div_xk_inplace(l);
}
poly_t mod_xk(size_t k) const & {return substr(0, k);}
poly_t mod_xk(size_t k) && {mod_xk_inplace(k); return std::move(*this);}
poly_t mul_xk(size_t k) const & {auto p = *this; p.mul_xk_inplace(k); return p;}
poly_t mul_xk(size_t k) && {mul_xk_inplace(k); return std::move(*this);}
poly_t div_xk(int64_t k) const & {return k < 0 ? mul_xk(-k) : substr(k, a.size());}
poly_t div_xk(int64_t k) && {div_xk_inplace(k); return std::move(*this);}
poly_t substr(size_t l, size_t k) const & {
l = std::min(l, a.size());
k = std::min(k, a.size() - l);
return Vector(begin(a) + l, begin(a) + l + k);
}
poly_t substr(size_t l, size_t k) && {substr_inplace(l, k); return std::move(*this);}
poly_t& operator *= (const poly_t &t) {fft::mul(a, t.a); normalize(); return *this;}
friend poly_t operator * (poly_t p, const poly_t &t) {p *= t; return p;}
poly_t& operator /= (const poly_t &t) {
assert(!t.is_zero());
if(this == &t) {return *this = T(1);}
auto [q, r] = divmod(std::move(*this), t);
return *this = std::move(q);
}
poly_t& operator %= (const poly_t &t) {
assert(!t.is_zero());
if(this == &t) {a.clear(); return *this;}
auto [q, r] = divmod(std::move(*this), t);
return *this = std::move(r);
}
friend poly_t operator / (poly_t p, poly_t const& t) {p /= t; return p;}
friend poly_t operator % (poly_t p, poly_t const& t) {p %= t; return p;}
poly_t& operator *= (T const& x) {
for(auto &it: a) {
it *= x;
}
return normalize();
}
poly_t& operator /= (T const& x) {return *this *= x.inv();}
friend poly_t operator * (poly_t p, T const& x) {p *= x; return p;}
friend poly_t operator / (poly_t p, T const& x) {p /= x; return p;}
poly_t& reverse(size_t n) {
a.resize(n);
std::ranges::reverse(a);
return normalize();
}
poly_t& reverse() {return reverse(size(a));}
poly_t reversed(size_t n) const & {auto p = *this; p.reverse(n); return p;}
poly_t reversed(size_t n) && {reverse(n); return std::move(*this);}
poly_t reversed() const & {return reversed(a.size());}
poly_t reversed() && {reverse(); return std::move(*this);}
friend poly_t operator * (T const& x, poly_t p) {p *= x; return p;}
poly_t negx() const { // A(x) -> A(-x)
auto res = *this;
for(int i = 1; i <= deg(); i += 2) {
res.a[i] = -res[i];
}
return res;
}
void print(int n) const {
for(int i = 0; i < n; i++) {
std::cout << (*this)[i] << ' ';
}
std::cout << "\n";
}
void print() const {
print(deg() + 1);
}
T eval(T x) const { // evaluates in single point x
T res(0);
for(int i = deg(); i >= 0; i--) {
res *= x;
res += a[i];
}
return res;
}
T lead() const { // leading coefficient
assert(!is_zero());
return a.back();
}
int deg() const { // degree, -1 for P(x) = 0
return (int)a.size() - 1;
}
bool is_zero() const {
return a.empty();
}
T operator [](int idx) const {
return idx < 0 || idx > deg() ? T(0) : a[idx];
}
T& coef(size_t idx) { // mutable reference at coefficient
return a[idx];
}
bool operator == (const poly_t &t) const = default;
size_t trailing_xk() const { // Let p(x) = x^k * t(x), return k
if(is_zero()) {
return -1;
}
int res = 0;
while(a[res] == T(0)) {
res++;
}
return res;
}
poly_t& mul_truncate(poly_t const& t, size_t k) {
fft::mul_truncate(a, t.a, k);
return normalize();
}
static poly_t xk(size_t n) { // P(x) = x^n
return poly_t(T(1)).mul_xk(n);
}
static poly_t ones(size_t n) { // P(x) = 1 + x + ... + x^{n-1}
return Vector(n, 1);
}
poly_t x2() const { // P(x) -> P(x^2)
Vector res(2 * a.size());
for(size_t i = 0; i < a.size(); i++) {
res[2 * i] = a[i];
}
return res;
}
// Return {P0, P1}, where P(x) = P0(x^2) + xP1(x^2)
std::array<poly_t, 2> bisect(size_t n) const {
n = std::min(n, size(a));
Vector res[2];
for(size_t i = 0; i < n; i++) {
res[i % 2].push_back(a[i]);
}
return {std::move(res[0]), std::move(res[1])};
}
std::array<poly_t, 2> bisect() const {
return bisect(size(a));
}
};
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/div.hpp"
#line 1 "cp-algo/math/poly/impl/div.hpp"
#line 1 "cp-algo/math/poly/series/inv.hpp"
#line 4 "cp-algo/math/poly/series/inv.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::poly::impl {
template<typename poly>
poly& inv_inplace(poly& p, size_t n) {
using base = poly::base;
if(n == 0) {
p.a.clear();
return p;
}
assert(p[0] != base(0));
if(n < magic) {
typename poly::Vector q(n);
q[0] = base(1) / p[0];
for(size_t i = 1; i < n; i++) {
for(size_t j = 1; j <= std::min(i, p.a.size() - 1); j++) {
q[i] -= p.a[j] * q[i - j];
}
q[i] *= q[0];
}
return p = std::move(q);
}
size_t m = std::bit_floor(size_t(magic - 1));
auto q = p.mod_xk(m);
inv_inplace(q, m);
for(; m < n; m *= 2) {
size_t k = std::min(2 * m, n);
typename poly::Vector error((k + fft::flen - 1) / fft::flen * fft::flen);
auto Q = fft::spectrum<base>(q.a, 2 * m);
// Wrapping modulo x^(2m) + 1 only changes the discarded low half.
fft::spectrum<base>(p.a | std::views::take(k), 2 * m).multiply(Q, error, k);
auto E = fft::spectrum<base>(error | std::views::drop(m) | std::views::take(k - m), 2 * m);
std::move(Q).multiply(E, error, k - m);
q.a.resize(k);
for(size_t i = m; i < k; i++) {
q.a[i] = -error[i - m];
}
}
p = std::move(q);
p.normalize();
return p;
}
}
namespace cp_algo::math {
// Inverse modulo x^n; the constant coefficient must be invertible.
template<typename T>
poly_t<T> inv(poly_t<T> p, size_t n) {
poly::impl::inv_inplace(p, n);
return p;
}
}
#pragma GCC pop_options
#line 4 "cp-algo/math/poly/impl/div.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::poly::impl {
template<typename T>
std::array<poly_t<T>, 2> divmod_slow(poly_t<T> p, poly_t<T> const& q) {
poly_t<T> d;
auto qi = q.lead() == T(1) ? T(1) : q.lead().inv();
while(p.deg() >= q.deg()) {
d.a.push_back(p.lead() * qi);
if(d.lead() != T(0)) {
for(size_t i = 1; i <= q.a.size(); i++) {
p.a[p.a.size() - i] -= d.lead() * q.a[q.a.size() - i];
}
}
p.a.pop_back();
}
std::ranges::reverse(d.a);
p.normalize();
return {std::move(d), std::move(p)};
}
template<typename T>
std::array<poly_t<T>, 2> divmod_hint(poly_t<T> p, poly_t<T> const& q, poly_t<T> const& qri) {
assert(!q.is_zero());
int n = p.deg() - q.deg();
if(std::min(n, q.deg()) < magic) {
return divmod_slow(std::move(p), q);
}
poly_t<T> d(typename poly_t<T>::Vector(p.a.rbegin(), p.a.rbegin() + n + 1));
d.mul_truncate(qri, n + 1).reverse(n + 1);
// Only coefficients below deg(q) survive in the remainder.
auto low = d.mod_xk(q.deg());
low.mul_truncate(q, q.deg());
p.mod_xk_inplace(q.deg());
p -= low;
return {std::move(d), std::move(p)};
}
}
#pragma GCC pop_options
#line 4 "cp-algo/math/poly/div.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// Quotient and remainder; q must be nonzero.
template<typename T>
std::array<poly_t<T>, 2> divmod(poly_t<T> p, poly_t<T> const& q) {
assert(!q.is_zero());
int n = p.deg() - q.deg();
if(std::min(n, q.deg()) < magic) {
return poly::impl::divmod_slow(std::move(p), q);
}
auto qi = inv(q.reversed(), n + 1);
return poly::impl::divmod_hint(std::move(p), q, qi);
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/series.hpp"
#line 1 "cp-algo/math/poly/series/log.hpp"
#line 1 "cp-algo/math/poly/calculus.hpp"
#line 1 "cp-algo/math/factorials.hpp"
#line 1 "cp-algo/util/bump_alloc.hpp"
#line 5 "cp-algo/util/bump_alloc.hpp"
namespace cp_algo {
template<class T, size_t max_len>
struct bump_alloc {
static inline char* buf = big_alloc<char>().allocate(max_len * sizeof(T));
static inline size_t buf_ind = max_len * sizeof(T);
using value_type = T;
template <class U> struct rebind { using other = bump_alloc<U, max_len>; };
constexpr bool operator==(const bump_alloc&) const = default;
bump_alloc() = default;
template<class U> bump_alloc(const U&) {}
T* allocate(size_t n) {
buf_ind -= n * sizeof(T);
buf_ind &= 0 - alignof(T);
return (T*)(buf + buf_ind);
}
void deallocate(T*, size_t) {}
};
}
#line 1 "cp-algo/math/combinatorics.hpp"
#line 7 "cp-algo/math/combinatorics.hpp"
namespace cp_algo::math {
// fact/rfact/small_inv are caching
// Beware of usage with dynamic mod
template<typename T>
T fact(auto n) {
static const auto F = [] {
big_vector<T> F(maxn);
F[0] = T(1);
for(int i = 1; i < maxn; i++) {
F[i] = F[i - 1] * T(i);
}
return F;
}();
return F[n];
}
// Only works for modint types
template<typename T>
T rfact(auto n) {
static const auto F = [] {
big_vector<T> F(maxn);
int t = (int)std::min<int64_t>(T::mod(), maxn) - 1;
F[t] = T(1) / fact<T>(t);
for(int i = t - 1; i >= 0; i--) {
F[i] = F[i + 1] * T(i + 1);
}
return F;
}();
return F[n];
}
template<typename T, int base>
T pow_fixed(int n) {
static const auto prec = [] {
big_vector<T> prec_low(1 << 16), prec_high(1 << 16);
prec_low[0] = prec_high[0] = T(1);
T step_low = T(base);
T step_high = bpow(T(base), 1 << 16);
for(int i = 1; i < (1 << 16); i++) {
prec_low[i] = prec_low[i - 1] * step_low;
prec_high[i] = prec_high[i - 1] * step_high;
}
return std::pair{std::move(prec_low), std::move(prec_high)};
}();
return prec.first[n & 0xFFFF] * prec.second[n >> 16];
}
template<typename T>
big_vector<T> bulk_invs(auto const& args) {
big_vector<T> res(std::size(args), args[0]);
for(size_t i = 1; i < std::size(args); i++) {
res[i] = res[i - 1] * args[i];
}
auto all_invs = T(1) / res.back();
for(size_t i = std::size(args) - 1; i > 0; i--) {
res[i] = all_invs * res[i - 1];
all_invs *= args[i];
}
res[0] = all_invs;
return res;
}
template<typename T>
T small_inv(auto n) {
static auto F = bulk_invs<T>(std::views::iota(1, maxn));
return F[n - 1];
}
template<typename T>
T binom_large(T n, auto r) {
assert(r < maxn);
T ans = 1;
for(decltype(r) i = 0; i < r; i++) {
ans = ans * T(n - i) * small_inv<T>(i + 1);
}
return ans;
}
template<typename T>
T binom(auto n, auto r) {
if(r < 0 || r > n) {
return T(0);
} else if(n >= maxn) {
return binom_large(T(n), r);
} else {
return fact<T>(n) * rfact<T>(r) * rfact<T>(n - r);
}
}
}
#line 9 "cp-algo/math/factorials.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
template<bool use_bump_alloc = false, int maxn = -1>
auto facts(auto const& args) {
static_assert(!use_bump_alloc || maxn > 0, "maxn must be set if use_bump_alloc is true");
constexpr int max_mod = 1'000'000'000;
constexpr int accum = 4;
constexpr int simd_size = 8;
constexpr int block = 1 << 18;
constexpr int subblock = block / simd_size;
using base = std::decay_t<decltype(args[0])>;
static_assert(modint_type<base>, "Base type must be a modint type");
using T = std::array<int, 2>;
using alloc = std::conditional_t<use_bump_alloc,
bump_alloc<T, 30 * maxn>,
big_alloc<T>>;
std::basic_string<T, std::char_traits<T>, alloc> odd_args_per_block[max_mod / subblock];
std::basic_string<T, std::char_traits<T>, alloc> reg_args_per_block[max_mod / subblock];
constexpr int limit_reg = max_mod / 64;
int limit_odd = 0;
big_vector<base> res(size(args), 1);
const int mod = base::mod();
const int imod = -math::inv2(mod);
for(auto [i, xy]: std::views::zip(args, res) | std::views::enumerate) {
auto [x, y] = xy;
int t = x.getr();
if(t >= mod / 2) {
t = mod - t - 1;
y = t % 2 ? 1 : mod-1;
}
auto pw = 32ull * (t + 1);
while(t > limit_reg) {
limit_odd = std::max(limit_odd, (t - 1) / 2);
odd_args_per_block[(t - 1) / 2 / subblock].push_back({int(i), (t - 1) / 2});
t /= 2;
pw += t;
}
reg_args_per_block[t / subblock].push_back({int(i), t});
y *= pow_fixed<base, 2>(int(pw % (mod - 1)));
}
checkpoint("init");
base bi2x32 = pow_fixed<base, 2>(32).inv();
auto process = [&](int limit, auto &args_per_block, auto step, auto &&proj) {
base fact = 1;
for(int b = 0; b <= limit; b += accum * block) {
u32x8 cur[accum];
static std::array<u32x8, subblock> prods[accum];
for(int z = 0; z < accum; z++) {
for(int j = 0; j < simd_size; j++) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
cur[z][j] = uint32_t(b + z * block + j * subblock);
cur[z][j] = proj(cur[z][j]);
prods[z][0][j] = cur[z][j] + !cur[z][j];
prods[z][0][j] = uint32_t(uint64_t(prods[z][0][j]) * bi2x32.getr() % mod);
#pragma GCC diagnostic pop
}
}
for(int i = 1; i < block / simd_size; i++) {
for(int z = 0; z < accum; z++) {
cur[z] += step;
prods[z][i] = montgomery_mul(prods[z][i - 1], cur[z], mod, imod);
}
}
checkpoint("inner loop");
for(int z = 0; z < accum; z++) {
for(int j = 0; j < simd_size; j++) {
int bl = b + z * block + j * subblock;
for(auto [i, x]: args_per_block[bl / subblock]) {
res[i] *= fact * prods[z][x - bl][j];
}
fact *= base(prods[z].back()[j]);
}
}
checkpoint("mul ans");
}
};
process(limit_reg, reg_args_per_block, 1, std::identity{});
process(limit_odd, odd_args_per_block, 2, [](uint32_t x) {return 2 * x + 1;});
auto invs = bulk_invs<base>(res);
for(auto [i, x]: res | std::views::enumerate) {
if (args[i] >= mod / 2) {
x = invs[i];
}
}
checkpoint("inv ans");
return res;
}
}
#pragma GCC pop_options
#line 5 "cp-algo/math/poly/calculus.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// k-th derivative; consumes temporaries and moved polynomials.
template<typename T>
poly_t<T> deriv(poly_t<T> p, int k = 1) {
assert(k >= 0);
if(k > p.deg()) {return k == 0 ? p : poly_t<T>{};}
if(k == 0) {return p;}
if(k == 1) {
for(int i = 1; i <= p.deg(); i++) {p.a[i - 1] = T(i) * p.a[i];}
p.a.pop_back();
p.normalize();
return p;
}
for(int i = k; i <= p.deg(); i++) {
p.a[i - k] = fact<T>(i) * rfact<T>(i - k) * p.a[i];
}
p.a.resize(p.a.size() - k);
p.normalize();
return p;
}
// Antiderivative with zero constant coefficient.
template<typename T>
poly_t<T> integr(poly_t<T> p) {
if(p.is_zero()) {return p;}
p.a.push_back(0);
for(int i = p.deg() - 1; i >= 0; i--) {
p.a[i + 1] = p.a[i] * small_inv<T>(i + 1);
}
p.a[0] = 0;
return p;
}
}
#pragma GCC pop_options
#line 5 "cp-algo/math/poly/series/log.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// log(p) modulo x^n, for p[0] = 1.
template<typename T>
poly_t<T> log(poly_t<T> p, size_t n) {
if(n == 0) {
return {};
}
assert(p[0] == T(1));
p.mod_xk_inplace(n);
auto dp = deriv(p);
size_t k = n - 1;
if(k < magic) {
poly::impl::inv_inplace(p, k);
p.mul_truncate(dp, k);
return integr(std::move(p));
}
// Solve p*q = p' in two halves, avoiding a full-precision reciprocal.
size_t m = std::bit_floor(k - 1), t = k - m;
auto r = inv(p, m);
auto R = fft::spectrum<T>(r.a, 2 * m);
typename poly_t<T>::Vector work(2 * m);
fft::spectrum<T>(dp.a | std::views::take(m), 2 * m).multiply(R, work, m);
poly_t<T> q(typename poly_t<T>::Vector(begin(work), begin(work) + m));
{
auto Q = fft::spectrum<T>(q.a, 2 * m);
fft::spectrum<T>(p.a | std::views::take(k), 2 * m).multiply(Q, work, k);
}
// Cyclic wraparound only affects the discarded low half.
for(size_t i = 0; i < t; i++) {
work[m + i] = dp[int(m + i)] - work[m + i];
}
fft::spectrum<T>(work | std::views::drop(m) | std::views::take(t), 2 * m).multiply(R, work, t);
q.a.resize(k);
std::copy_n(begin(work), t, begin(q.a) + m);
q.normalize();
return integr(std::move(q));
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/series/exp.hpp"
#line 5 "cp-algo/math/poly/series/exp.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// exp(p) modulo x^n, for p[0] = 0.
template<typename T>
poly_t<T> exp(poly_t<T> p, size_t n) {
if(n == 0) {
return {};
}
assert(p[0] == T(0));
p.mod_xk_inplace(n);
if(p.is_zero()) {
return T(1);
}
size_t m = std::min(n, std::bit_floor(size_t(magic - 1)));
typename poly_t<T>::Vector seed(m);
seed[0] = 1;
for(size_t i = 1; i < m; i++) {
for(size_t j = 1; j <= i && j < p.a.size(); j++) {
seed[i] += T(j) * p.a[j] * seed[i - j];
}
seed[i] *= small_inv<T>(i);
}
poly_t<T> q(std::move(seed));
if(m == n) {
return q;
}
auto r = inv(q, m), dp = deriv(p);
for(; m < n; m *= 2) {
size_t k = std::min(2 * m, n), t = k - m;
auto Q = fft::spectrum<T>(q.a, 2 * m), R = fft::spectrum<T>(r.a, 2 * m);
typename poly_t<T>::Vector work(2 * m);
fft::spectrum<T>(dp.a | std::views::take(k - 1), 2 * m).multiply(Q, work, k - 1);
// p' q - q' vanishes below m-1. Cyclic wrap only touches <m-1.
fft::spectrum<T>(work | std::views::drop(m - 1) | std::views::take(t), 2 * m).multiply(R, work, t);
for(size_t i = 0; i < t; i++) {
work[i] *= small_inv<T>(m + i);
}
// d = (p - log(q)) / x^m. Keep it for the reciprocal correction.
auto d = typename poly_t<T>::Vector(begin(work), begin(work) + t);
fft::spectrum<T>(d, 2 * m).multiply(Q, work, t);
q.a.resize(k);
std::copy_n(begin(work), t, begin(q.a) + m);
if(k == n) {
break;
}
// (q*(1+x^m*d))^-1 = q^-1*(1-x^m*d) modulo x^(2m).
std::move(Q).multiply(R, work, k);
for(size_t i = 0; i < t; i++) {
work[m + i] += d[i];
}
fft::spectrum<T>(work | std::views::drop(m) | std::views::take(t), 2 * m).multiply(R, work, t);
r.a.resize(k);
for(size_t i = 0; i < t; i++) {
r.a[m + i] = -work[i];
}
}
q.normalize();
return q;
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/series/pow.hpp"
#line 5 "cp-algo/math/poly/series/pow.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
namespace poly::impl {
// O(deg(p) * n), using p q' = k p' q.
template<typename T>
poly_t<T> pow_slow(poly_t<T> const& p, int64_t k, size_t n) {
typename poly_t<T>::Vector q(n);
q[0] = bpow(p[0], k);
auto a0inv = p[0].inv();
for(int i = 1; i < (int)n; i++) {
for(int j = 1; j <= std::min(p.deg(), i); j++) {
q[i] += p[j] * q[i - j] * (T(k) * T(j) - T(i - j));
}
q[i] *= small_inv<T>(i) * a0inv;
}
return q;
}
}
// Nonnegative integer power modulo x^n.
template<typename T>
poly_t<T> pow(poly_t<T> p, int64_t k, size_t n) {
assert(k >= 0);
if(n == 0) {return {};}
if(k == 0) {return T(1);}
p.mod_xk_inplace(n);
if(p.is_zero()) {return p;}
size_t shift = p.trailing_xk();
if(shift) {
if(uint64_t(k) > (n - 1) / shift) {return {};}
p.div_xk_inplace(shift);
return pow(std::move(p), k, n - shift * k).mul_xk(shift * k);
}
if(std::min(p.deg(), (int)n) <= magic) {
return poly::impl::pow_slow(p, k, n);
}
if(k <= magic) {
auto t = pow(p, k / 2, n);
t.mul_truncate(t, n);
if(k % 2) {t.mul_truncate(p, n);}
return t;
}
T c = p[0];
p /= c;
return bpow(c, k) * exp(log(std::move(p), n) * T(k), n);
}
}
#pragma GCC pop_options
#line 6 "cp-algo/math/poly/series.hpp"
#line 1 "cp-algo/math/poly/series/sqrt.hpp"
#line 5 "cp-algo/math/poly/series/sqrt.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// A square root modulo x^n, or nullopt if none exists.
template<typename T>
std::optional<poly_t<T>> sqrt(poly_t<T> p, size_t n) {
if(n == 0) {
return poly_t<T>{};
}
p.mod_xk_inplace(n);
if(p.is_zero()) {
return p;
}
size_t shift = p.trailing_xk();
if(shift % 2) {
return std::nullopt;
}
if(shift) {
p.div_xk_inplace(shift);
// The unit square root is only constrained modulo x^(n-shift).
auto ans = sqrt(std::move(p), n - shift);
if(ans) {
ans->mul_xk_inplace(shift / 2);
}
return ans;
}
auto c = math::sqrt(p[0]);
if(!c) {
return std::nullopt;
}
size_t m = std::min(n, std::bit_floor(size_t(magic - 1)));
typename poly_t<T>::Vector seed(m);
T half_inv = T(1) / (T(2) * *c);
seed[0] = *c;
for(size_t i = 1; i < m; i++) {
seed[i] = p[int(i)];
for(size_t j = 1; j < i; j++) {
seed[i] -= seed[j] * seed[i - j];
}
seed[i] *= half_inv;
}
poly_t<T> ans(std::move(seed));
if(m == n) {
return ans;
}
auto r = inv(ans, m);
T half = T(1) / T(2);
for(; m < n; m *= 2) {
size_t k = std::min(2 * m, n), t = k - m;
auto R = fft::spectrum<T>(r.a, 2 * m);
typename poly_t<T>::Vector work(2 * m);
fft::spectrum<T>(ans.a, 2 * m).square(work, k);
for(size_t i = 0; i < t; i++) {
work[m + i] = (p[int(m + i)] - work[m + i]) * half;
}
fft::spectrum<T>(work | std::views::drop(m) | std::views::take(t), 2 * m).multiply(R, work, t);
ans.a.resize(k);
std::copy_n(begin(work), t, begin(ans.a) + m);
if(k == n) {
break;
}
// Extend the reciprocal of the updated root, reusing its old transform.
fft::spectrum<T>(ans.a, 2 * m).multiply(R, work, k);
fft::spectrum<T>(work | std::views::drop(m) | std::views::take(t), 2 * m).multiply(R, work, t);
r.a.resize(k);
for(size_t i = 0; i < t; i++) {
r.a[m + i] = -work[i];
}
}
ans.normalize();
return ans;
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/sparse.hpp"
#line 1 "cp-algo/math/poly/sparse/inv.hpp"
#line 1 "cp-algo/math/poly/sparse/impl.hpp"
#line 5 "cp-algo/math/poly/sparse/impl.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::poly::impl {
template<typename T>
auto sparse_terms(poly_t<T> const& p, size_t n, size_t shift = 0) {
std::vector<std::pair<size_t, T>> terms;
for(size_t i = shift + 1; i < p.a.size() && i - shift < n; i++) {
if(p.a[i] != T(0)) {terms.emplace_back(i - shift, p.a[i]);}
}
return terms;
}
template<typename T>
T sparse_dot(auto const& terms, typename poly_t<T>::Vector const& q, size_t i) {
if constexpr(T::bits <= 32) {
// Sixteen products below 2^60 have a sum strictly below 2^64.
if(T::mod() < (1 << 30) && terms.size() <= 16) {
uint64_t sum = 0;
for(auto [j, a]: terms) {
if(j > i) {break;}
sum += uint64_t(a.getr()) * q[i-j].getr();
}
return T(sum % T::mod());
}
}
T sum = 0;
for(auto [j, a]: terms) {
if(j > i) {break;}
sum += a * q[i-j];
}
return sum;
}
// For q = (p / p[shift])^k, use p q' = k p' q after removing x^shift.
template<typename T>
poly_t<T> pow_sparse_unit(poly_t<T> const& p, T k, size_t n, size_t shift) {
auto terms = sparse_terms(p, n, shift);
T a0inv = T(1) / p.a[shift];
for(auto &[j, a]: terms) {a *= a0inv;}
std::vector<T> weights;
for(auto [j, a]: terms) {weights.push_back((k + T(1)) * T(j));}
typename poly_t<T>::Vector q(n);
q[0] = 1;
if constexpr(T::bits <= 32) {
if(T::mod() < (1 << 30) && terms.size() >= 6 && terms.size() <= 16) {
for(size_t t = 0; t < terms.size(); t++) {weights[t] *= terms[t].second;}
for(size_t i = 1; i < n; i++) {
uint64_t plain = 0, weighted = 0;
for(size_t t = 0; t < terms.size(); t++) {
auto [j, a] = terms[t];
if(j > i) {break;}
auto v = q[i-j].getr();
plain += uint64_t(a.getr()) * v;
weighted += uint64_t(weights[t].getr()) * v;
}
// Each sum has at most sixteen products below 2^60.
q[i] = (T(weighted % T::mod()) - T(i) * T(plain % T::mod())) * small_inv<T>(i);
}
return q;
}
}
for(size_t i = 1; i < n; i++) {
T index = T(i);
for(size_t t = 0; t < terms.size(); t++) {
auto [j, a] = terms[t];
if(j > i) {break;}
q[i] += a * q[i - j] * (weights[t] - index);
}
q[i] *= small_inv<T>(i);
}
return q;
}
}
#pragma GCC pop_options
#line 4 "cp-algo/math/poly/sparse/inv.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// Inverse modulo x^n in O(n * number of nonzero terms), with p[0] != 0.
template<typename T>
poly_t<T> inv_sparse(poly_t<T> const& p, size_t n) {
if(n == 0) {return {};}
assert(p[0] != T(0));
auto terms = poly::impl::sparse_terms(p, n);
T a0inv = T(1) / p[0];
for(auto &[j, a]: terms) {a *= -a0inv;}
typename poly_t<T>::Vector q(n);
q[0] = a0inv;
for(size_t i = 1; i < n; i++) {
q[i] = poly::impl::sparse_dot<T>(terms, q, i);
}
return q;
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/sparse/exp.hpp"
#line 4 "cp-algo/math/poly/sparse/exp.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// Exponential modulo x^n in O(n * number of nonzero terms), with p[0] = 0.
template<typename T>
poly_t<T> exp_sparse(poly_t<T> const& p, size_t n) {
if(n == 0) {return {};}
assert(p[0] == T(0));
auto terms = poly::impl::sparse_terms(p, n);
for(auto &[j, a]: terms) {a *= T(j);}
typename poly_t<T>::Vector q(n);
q[0] = 1;
for(size_t i = 1; i < n; i++) {
q[i] = poly::impl::sparse_dot<T>(terms, q, i);
q[i] *= small_inv<T>(i);
}
return q;
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/sparse/log.hpp"
#line 4 "cp-algo/math/poly/sparse/log.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// Logarithm modulo x^n in O(n * number of nonzero terms), with p[0] = 1.
template<typename T>
poly_t<T> log_sparse(poly_t<T> const& p, size_t n) {
if(n == 0) {return {};}
assert(p[0] == T(1));
auto terms = poly::impl::sparse_terms(p, n);
typename poly_t<T>::Vector q(n);
// Store x * log(p)' first, so the convolution has an unweighted kernel.
for(size_t i = 1; i < n; i++) {
q[i] = T(i) * p[int(i)];
q[i] -= poly::impl::sparse_dot<T>(terms, q, i);
}
for(size_t i = 1; i < n; i++) {q[i] *= small_inv<T>(i);}
return q;
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/sparse/pow.hpp"
#line 4 "cp-algo/math/poly/sparse/pow.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// Nonnegative integer power modulo x^n in O(n * number of nonzero terms).
template<typename T>
poly_t<T> pow_sparse(poly_t<T> const& p, int64_t k, size_t n) {
assert(k >= 0);
if(n == 0) {return {};}
if(k == 0) {return T(1);}
if(p.is_zero()) {return {};}
size_t shift = p.trailing_xk();
if(shift && uint64_t(k) > (n - 1) / shift) {return {};}
size_t offset = shift * uint64_t(k);
auto q = poly::impl::pow_sparse_unit(p, T(k), n - offset, shift);
q *= bpow(p.a[shift], k);
q.mul_xk_inplace(offset);
return q;
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/sparse/sqrt.hpp"
#line 5 "cp-algo/math/poly/sparse/sqrt.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// Square root modulo x^n in O(n * number of nonzero terms), or nullopt.
template<typename T>
std::optional<poly_t<T>> sqrt_sparse(poly_t<T> const& p, size_t n) {
if(n == 0 || p.is_zero()) {return poly_t<T>{};}
size_t shift = p.trailing_xk();
if(shift >= n) {return poly_t<T>{};}
if(shift % 2) {return std::nullopt;}
auto c = math::sqrt(p.a[shift]);
if(!c) {return std::nullopt;}
auto q = poly::impl::pow_sparse_unit(p, T(1) / T(2), n - shift, shift);
q *= *c;
q.mul_xk_inplace(shift / 2);
return q;
}
}
#pragma GCC pop_options
#line 8 "cp-algo/math/poly/sparse.hpp"
#line 1 "cp-algo/math/poly/euclid.hpp"
#line 1 "cp-algo/math/poly/impl/euclid.hpp"
#line 8 "cp-algo/math/poly/impl/euclid.hpp"
#include <numeric>
#line 12 "cp-algo/math/poly/impl/euclid.hpp"
#include <list>
CP_ALGO_SIMD_PRAGMA_PUSH
// operations related to gcd and Euclidean algo
namespace cp_algo::math::poly::impl {
template<typename poly>
using gcd_result = std::pair<
std::list<std::decay_t<poly>>,
linfrac<std::decay_t<poly>>>;
template<bool quotients = true, typename poly>
gcd_result<poly> half_gcd(poly &&A, poly &&B) {
assert(A.deg() >= B.deg());
size_t m = size(A.a) / 2;
if(B.deg() < (int)m) {
return {};
}
auto [ai, R] = divmod(A, B);
A = std::move(B);
B = std::move(R);
std::list<std::decay_t<poly>> a;
if constexpr(quotients) {a.push_back(ai);}
auto T = -linfrac(ai).adj();
auto advance = [&](size_t k) {
auto [ak, Tk] = half_gcd<quotients>(A.div_xk(k), B.div_xk(k));
a.splice(end(a), ak);
T.prepend(Tk);
return Tk;
};
advance(m).apply(A, B);
if constexpr (std::is_reference_v<poly>) {
advance(2 * m - A.deg()).apply(A, B);
} else {
advance(2 * m - A.deg());
}
return {std::move(a), std::move(T)};
}
template<bool extended = true, bool quotients = true, typename poly>
gcd_result<poly> full_gcd(poly &&A, poly &&B) {
using poly_t = std::decay_t<poly>;
std::list<poly_t> ak;
big_vector<linfrac<poly_t>> trs;
while(!B.is_zero()) {
auto [a0, R] = divmod(A, B);
if constexpr(extended) {trs.push_back(-linfrac(a0).adj());}
if constexpr(quotients) {ak.push_back(std::move(a0));}
A = std::move(B);
B = std::move(R);
auto [a, Tr] = half_gcd<quotients>(A, B);
ak.splice(end(ak), a);
if constexpr(extended) {trs.push_back(std::move(Tr));}
}
if constexpr(extended) {
return {std::move(ak), std::accumulate(rbegin(trs), rend(trs), linfrac<poly_t>{}, std::multiplies{})};
} else {
return {std::move(ak), {}};
}
}
// computes product of linfrac on [L, R)
auto convergent(auto L, auto R) {
using poly = decltype(L)::value_type;
if(L == R) {
return linfrac<poly>{};
} else if(R == next(L)) {
return linfrac(*L);
} else {
// split so that both halves have approximately equal total degree
int s = std::transform_reduce(L, R, 0, std::plus{}, std::mem_fn(&poly::deg));
auto M = next(L);
for(int c = L->deg(); next(M) != R && 2 * c < s; c += M->deg(), ++M) {}
return convergent(L, M) * convergent(M, R);
}
}
template<typename poly>
poly min_rec(poly const& p, size_t d) {
auto R2 = p.mod_xk(d).reversed(d), R1 = poly::xk(d);
if(R2.is_zero()) {
return poly(1);
}
auto [a, Tr] = half_gcd(R1, R2);
// The stopping degree bound needs at most one quotient beyond the halfway point.
if(!R2.is_zero()) {a.push_back(divmod(R1, R2)[0]);}
a.emplace_back();
auto pref = begin(a);
// An exact finite expansion can end before the degree bound is crossed.
for(int delta = (int)d - a.front().deg(); next(pref) != end(a) && delta >= 0; pref++) {
delta -= pref->deg() + next(pref)->deg();
}
return convergent(begin(a), pref).a;
}
template<typename poly>
std::optional<poly> inv_mod(poly p, poly q) {
assert(!q.is_zero());
auto [a, Tr] = full_gcd<true, false>(q, p);
if(q.deg() != 0) {
return std::nullopt;
}
return std::move(Tr.b) / q[0];
}
}
#pragma GCC pop_options
#line 4 "cp-algo/math/poly/euclid.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// GCD, without normalizing the leading coefficient.
template<typename T>
poly_t<T> gcd(poly_t<T> a, poly_t<T> b) {
poly::impl::full_gcd<false, false>(a, b);
return a;
}
// Inverse modulo q, or nullopt when p and q are not coprime.
template<typename T>
std::optional<poly_t<T>> inv_mod(poly_t<T> p, poly_t<T> q) {
return poly::impl::inv_mod(std::move(p), std::move(q));
}
template<typename T>
T resultant(poly_t<T> a, poly_t<T> b) {
T res = 1;
while(!b.is_zero()) {
if(b.deg() == 0) {return res * bpow(b.lead(), a.deg());}
int d = a.deg();
a %= b;
res *= bpow(b.lead(), d - a.deg()) * T((b.deg() & a.deg() & 1) ? -1 : 1);
std::swap(a, b);
}
return T(0);
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/recurrence.hpp"
#line 4 "cp-algo/math/poly/recurrence.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::poly::impl {
template<typename poly>
poly& inv_inplace(poly& q, int64_t k, size_t n) {
if(n == 0) {
q.a.clear();
return q;
}
assert(k >= 0 || uint64_t(-(k + 1)) < n);
using poly_t = std::decay_t<poly>;
using base = poly_t::base;
if(k <= std::max<int64_t>(n, size(q.a))) {
inv_inplace(q, size_t(k + int64_t(n)));
return q.div_xk_inplace(k);
}
if(k % 2) {
return inv_inplace(q, k - 1, n + 1).div_xk_inplace(1);
}
auto [q0, q1] = q.bisect();
auto qq = q0 * q0 - (q1 * q1).mul_xk_inplace(1);
inv_inplace(qq, k / 2 - q.deg() / 2, (n + 1) / 2 + q.deg() / 2);
size_t N = fft::com_size(size(q0.a), size(qq.a));
auto q0f = fft::spectrum<base>(q0.a, 2 * N);
auto q1f = fft::spectrum<base>(q1.a, 2 * N);
auto qqf = fft::spectrum<base>(qq.a, 2 * N);
size_t M = q0.deg() + (n + 1) / 2;
typename poly::Vector A, B;
A.resize((M + fft::flen - 1) / fft::flen * fft::flen);
B.resize((M + fft::flen - 1) / fft::flen * fft::flen);
std::move(q0f).multiply(qqf, A, M);
std::move(q1f).multiply(qqf, B, M);
q.a.resize(n + 1);
for(size_t i = 0; i < n; i += 2) {
q.a[i] = A[q0.deg() + i / 2];
q.a[i + 1] = -B[q0.deg() + i / 2];
}
q.a.pop_back();
q.normalize();
return q;
}
}
namespace cp_algo::math {
// Non-monic characteristic polynomial of a minimum recurrence.
template<typename T>
poly_t<T> min_rec(poly_t<T> const& p, size_t d) {
return poly::impl::min_rec(p, d);
}
// Coefficients [x^k] through [x^{k+n-1}] of 1/p; k+n must be nonnegative.
template<typename T>
poly_t<T> inv(poly_t<T> p, int64_t k, size_t n) {
poly::impl::inv_inplace(p, k, n);
return p;
}
// Find [x^k] P / Q
template<typename T>
T kth_rec(poly_t<T> P, poly_t<T> Q, int64_t k) {
assert(k >= 0 && Q[0] != T(0));
while(k > Q.deg()) {
size_t n = Q.a.size();
auto [Q0, Q1] = Q.bisect();
auto [P0, P1] = P.bisect();
size_t N = fft::com_size((n + 1) / 2, (n + 1) / 2);
auto Q0f = fft::spectrum<T>(Q0.a, 2 * N);
auto Q1f = fft::spectrum<T>(Q1.a, 2 * N);
auto P0f = fft::spectrum<T>(P0.a, 2 * N);
auto P1f = fft::spectrum<T>(P1.a, 2 * N);
// Both halves are squared before they are consumed, so those two copies are clones.
auto squared = [N](auto const& f) {
big_vector<T> res(2 * N);
f.clone().square(res, 2 * N);
return res;
};
auto times = [N](auto &&f, auto const& g) {
big_vector<T> res(2 * N);
std::move(f).multiply(g, res, 2 * N);
return res;
};
Q = poly_t<T>(squared(Q0f)) - poly_t<T>(squared(Q1f)).mul_xk_inplace(1);
if(k % 2) {
P = poly_t<T>(times(std::move(Q0f), P1f)) - poly_t<T>(times(std::move(Q1f), P0f));
} else {
P = poly_t<T>(times(std::move(Q0f), P0f)) - poly_t<T>(times(std::move(Q1f), P1f)).mul_xk_inplace(1);
}
k /= 2;
}
size_t n = size_t(k) + 1;
P.mul_truncate(inv(std::move(Q), n), n);
return P[(int)k];
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/transform.hpp"
#line 5 "cp-algo/math/poly/transform.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// Multiply coefficient k by c^k, i.e. substitute cx for x.
template<typename T>
poly_t<T> mulx(poly_t<T> p, std::type_identity_t<T> c) {
T cur = 1;
for(auto &x: p.a) {x *= cur; cur *= c;}
p.normalize();
return p;
}
// Multiply coefficient k by c^{k(k+1)/2} (chirp factors).
template<typename T>
poly_t<T> mulx_sq(poly_t<T> p, std::type_identity_t<T> c) {
T cur = 1, total = 1;
for(auto &x: p.a) {x *= total; cur *= c; total *= cur;}
p.normalize();
return p;
}
template<typename T>
poly_t<T> invborel(poly_t<T> p) { // a[k] *= k!
for(int i = 0; i <= p.deg(); i++) {p.a[i] *= fact<T>(i);}
return p;
}
template<typename T>
poly_t<T> borel(poly_t<T> p) { // a[k] /= k!
for(int i = 0; i <= p.deg(); i++) {p.a[i] *= rfact<T>(i);}
return p;
}
template<typename T>
poly_t<T> expx(size_t n) {return borel(poly_t<T>::ones(n));}
template<typename T>
poly_t<T> log1px(size_t n) {
typename poly_t<T>::Vector a(n);
for(size_t i = 1; i < n; i++) {a[i] = (i & 1 ? small_inv<T>(i) : -small_inv<T>(i));}
return a;
}
template<typename T>
poly_t<T> log1mx(size_t n) {
return n ? -integr(poly_t<T>::ones(n - 1)) : poly_t<T>{};
}
// Cross-correlation, with the reversed second argument.
template<typename T>
poly_t<T> corr(poly_t<T> a, poly_t<T> b) {
b.reverse();
a *= b;
return a;
}
// Coefficient k is sum_i a[i+k] b[i].
template<typename T>
poly_t<T> forward_corr(poly_t<T> a, poly_t<T> b) {
if(b.is_zero()) {return {};}
int d = b.deg();
return corr(std::move(a), std::move(b)).div_xk(d);
}
// Dot product of b with every full-length substring of a.
template<typename T>
poly_t<T> inner_corr(poly_t<T> a, poly_t<T> b) {
if(b.is_zero() || a.deg() < b.deg()) {return {};}
int d = b.deg(), n = a.deg() - d + 1;
size_t N = std::max(fft::flen, std::bit_ceil(a.a.size()));
std::ranges::reverse(b.a);
a.a.resize(N);
b.a.resize(N);
fft::cyclic_mul(a.a, b.a, N);
a.substr_inplace(d, n);
return a;
}
template<typename T>
poly_t<T> apply_diff(poly_t<T> p, poly_t<T> g) { // g(D) p(x)
return borel(forward_corr(invborel(std::move(p)), std::move(g)));
}
template<typename T>
poly_t<T> shift(poly_t<T> p, std::type_identity_t<T> c) { // p(x+c)
auto g = mulx(expx<T>(p.deg() + 1), c);
return apply_diff(std::move(p), std::move(g));
}
// q(0) = 0 and q(x+1) - q(x) = p(x).
template<typename T>
poly_t<T> prefix_sum(poly_t<T> p) {
auto g = inv(expx<T>(p.deg() + 2).div_xk(1), p.deg() + 1);
return integr(apply_diff(std::move(p), std::move(g)));
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/chirpz.hpp"
#line 4 "cp-algo/math/poly/chirpz.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// p(1), p(z), ..., p(z^{n-1}); needs convolution length n + 2 deg(p).
template<typename T>
poly_t<T> chirpz(poly_t<T> p, std::type_identity_t<T> z, int n) {
assert(n >= 0);
if(n == 0 || p.is_zero()) {return {};}
if(z == T(0)) {
typename poly_t<T>::Vector ans(n, p[0]);
ans[0] = p.eval(1);
return ans;
}
auto b = mulx_sq(poly_t<T>::ones(n + p.deg()), z);
auto a = mulx_sq(std::move(p), z.inv());
return mulx_sq(inner_corr(std::move(b), std::move(a)), z.inv());
}
namespace poly::impl {
// res[i] = product_{1 <= j <= i} 1/(1-z^j).
template<typename T>
big_vector<T> geometric_products_inv(T z, int n) {
big_vector<T> res(n, 1), zk(n, 1);
for(int i = 1; i < n; i++) {
zk[i] = zk[i - 1] * z;
res[i] = res[i - 1] * (T(1) - zk[i]);
}
res.back() = res.back().inv();
for(int i = n - 2; i >= 0; i--) {res[i] = (T(1) - zk[i + 1]) * res[i + 1];}
return res;
}
// product_{0 <= j < n} (1-z^j x).
template<typename T>
poly_t<T> geometric_product(T z, int n) {
if(n == 0) {return T(1);}
if(n == 1) {return typename poly_t<T>::Vector{1, -1};}
auto p = geometric_product(z, n / 2);
p *= mulx(p, bpow(z, n / 2));
if(n % 2) {p *= poly_t<T>(typename poly_t<T>::Vector{1, -bpow(z, n - 1)});}
return p;
}
}
// Interpolate from values at 1,z,...,z^{n-1}; nonzero nodes must be distinct.
template<typename T>
poly_t<T> chirpz_inverse(poly_t<T> p, std::type_identity_t<T> z, int n) {
assert(n >= 0);
if(n == 0 || p.is_zero()) {return {};}
if(z == T(0)) {
return n == 1 ? p.mod_xk(1) : poly_t<T>(typename poly_t<T>::Vector{p[1], p[0] - p[1]});
}
p.a.resize(n);
auto pos = poly::impl::geometric_products_inv(z, n);
auto neg = poly::impl::geometric_products_inv(z.inv(), n);
T zn = bpow(z, n - 1).inv(), znk = 1;
for(int i = 0; i < n; i++) {
p.a[i] *= znk * neg[i] * pos[n - 1 - i];
znk *= zn;
}
p = chirpz(std::move(p), z, n);
// q-binomial coefficients, using distinctness of 1,z,...,z^(n-1).
typename poly_t<T>::Vector product(n);
T numerator = 1, triangle = 1, power = 1, backwards = bpow(z, n), iz = z.inv();
for(int k = 0; k < n; k++) {
product[k] = numerator * triangle * pos[k];
numerator *= T(1) - backwards;
backwards *= iz;
triangle *= -power;
power *= z;
}
p.mul_truncate(poly_t<T>(std::move(product)), n);
p.mod_xk_inplace(n).reverse(n);
return p;
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/eval.hpp"
#line 5 "cp-algo/math/poly/eval.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::poly::impl {
template<typename T>
void build(big_vector<poly_t<T>> &tree, int v, auto l, auto r) {
if(r - l == 1) {
tree[v] = typename poly_t<T>::Vector{-*l, 1};
} else {
auto m = l + (r - l) / 2;
build(tree, 2 * v, l, m);
build(tree, 2 * v + 1, m, r);
tree[v] = tree[2 * v] * tree[2 * v + 1];
}
}
template<typename T>
void eval(poly_t<T> const& p, big_vector<poly_t<T>> const& tree, int v, auto l, auto r, auto out) {
if(r - l <= 32) {
for(; l != r; ++l, ++out) {*out = p.eval(*l);}
} else {
auto m = l + (r - l) / 2;
eval(p % tree[2 * v], tree, 2 * v, l, m, out);
eval(p % tree[2 * v + 1], tree, 2 * v + 1, m, r, out + (m - l));
}
}
template<typename T>
poly_t<T> inter(big_vector<poly_t<T>> const& tree, int v, auto l, auto r) {
if(r - l == 1) {return *l;}
auto m = l + (r - l) / 2;
auto a = inter(tree, 2 * v, l, m);
auto b = inter(tree, 2 * v + 1, m, r);
return std::move(a) * tree[2 * v + 1] + std::move(b) * tree[2 * v];
}
template<typename T>
poly_t<T> to_newton(poly_t<T> p, big_vector<poly_t<T>> const& tree, int v, size_t l, size_t r) {
if(r - l == 1) {return p;}
size_t m = l + (r - l) / 2;
auto [q, rem] = divmod(std::move(p), tree[2 * v]);
auto a = to_newton(std::move(rem), tree, 2 * v, l, m);
auto b = to_newton(std::move(q), tree, 2 * v + 1, m, r);
a += b.mul_xk_inplace(m - l);
return a;
}
}
namespace cp_algo::math {
// Evaluate at each point, preserving the order and length of x.
template<typename T>
big_vector<T> eval(poly_t<T> const& p, big_vector<T> const& x) {
big_vector<T> res(x.size());
if(x.empty() || p.is_zero()) {return res;}
big_vector<poly_t<T>> tree(4 * x.size());
poly::impl::build(tree, 1, begin(x), end(x));
poly::impl::eval(p, tree, 1, begin(x), end(x), begin(res));
return res;
}
// Interpolate at distinct x[i], with values y[i].
template<typename T>
poly_t<T> inter(big_vector<T> const& x, big_vector<T> const& y) {
assert(x.size() == y.size());
if(x.empty()) {return {};}
big_vector<poly_t<T>> tree(4 * x.size());
poly::impl::build(tree, 1, begin(x), end(x));
big_vector<T> values(x.size());
poly::impl::eval(deriv(tree[1]), tree, 1, begin(x), end(x), begin(values));
auto weights = bulk_invs<T>(values);
for(size_t i = 0; i < y.size(); i++) {weights[i] *= y[i];}
return poly::impl::inter(tree, 1, begin(weights), end(weights));
}
// Convert to the basis 1, (x-p[0]), (x-p[0])(x-p[1]), ... .
template<typename T>
poly_t<T> to_newton(poly_t<T> f, big_vector<T> const& p) {
assert(f.deg() < (int)p.size());
if(p.empty() || f.is_zero()) {return f;}
big_vector<poly_t<T>> tree(4 * p.size());
poly::impl::build(tree, 1, begin(p), end(p));
return poly::impl::to_newton(std::move(f), tree, 1, 0, p.size());
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/powmod.hpp"
#line 5 "cp-algo/math/poly/powmod.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// Reduce modulo x^m - 1.
template<typename T>
poly_t<T> circular_closure(poly_t<T> p, size_t m) {
assert(m > 0);
for(size_t i = p.a.size(); i > m; --i) {p.a[i - 1 - m] += p.a[i - 1];}
p.mod_xk_inplace(m);
return p;
}
template<typename T>
poly_t<T> powmod_circular(poly_t<T> p, int64_t k, size_t m) {
assert(k >= 0 && m > 0);
p = circular_closure(std::move(p), m);
return bpow(p, k, poly_t<T>(1), [m](auto const& a, auto const& b) {
auto product = a;
product *= &a == &b ? product : b;
return circular_closure(std::move(product), m);
});
}
// Nonnegative integer power modulo a nonzero polynomial.
template<typename T>
poly_t<T> powmod(poly_t<T> p, int64_t k, poly_t<T> const& md) {
assert(k >= 0 && !md.is_zero());
int d = md.deg();
if(d == 0) {return {};}
if(md == poly_t<T>::xk(d)) {return pow(std::move(p), k, d);}
if(md == poly_t<T>::xk(d) - poly_t<T>(1)) {return powmod_circular(std::move(p), k, d);}
auto mdri = inv(md.reversed(), d + 1);
return bpow(p % md, k, poly_t<T>(1), [&](auto const& a, auto const& b) {
auto product = a;
product *= &a == &b ? product : b;
auto [q, r] = poly::impl::divmod_hint(std::move(product), md, mdri);
return r;
});
}
}
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/compose.hpp"
#line 5 "cp-algo/math/poly/compose.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math {
// compute A(B(x)) mod x^n in O(n^2)
template<typename T>
poly_t<T> compose(poly_t<T> const& A, poly_t<T> const& B, int n) {
if(n <= 0) {return {};}
int q = (int)std::sqrt(n);
big_vector<poly_t<T>> Bk(q);
auto Bq = pow(B, q, n);
Bk[0] = poly_t<T>(T(1));
for(int i = 1; i < q; i++) {
Bk[i] = (Bk[i - 1] * B).mod_xk(n);
}
poly_t<T> Bqk(1);
poly_t<T> ans;
for(int i = 0; i <= A.deg() / q; i++) {
poly_t<T> cur;
for(int j = 0; j < q; j++) {
cur += Bk[j] * A[i * q + j];
}
ans += (Bqk * cur).mod_xk(n);
Bqk = (Bqk * Bq).mod_xk(n);
}
return ans;
}
// compute A(B(x)) mod x^n in O(sqrt(pqn log^3 n))
// preferrable when p = deg A and q = deg B
// are much less than n
template<typename T>
poly_t<T> compose_large(poly_t<T> A, poly_t<T> B, int n) {
if(n <= 0) {return {};}
if(B[0] != T(0)) {
return compose_large(shift(std::move(A), B[0]), B - B[0], n);
}
A.mod_xk_inplace(n);
int q = (int)std::sqrt(n);
auto [B0, B1] = std::make_pair(B.mod_xk(q), B.div_xk(q));
B0 = B0.div_xk(1);
big_vector<poly_t<T>> pw(A.deg() + 1);
auto getpow = [&](int k) -> poly_t<T> const& {
return pw[k].is_zero() ? pw[k] = pow(B0, k, n - k) : pw[k];
};
auto compose_dac = [&getpow](this auto const& self, poly_t<T> const& f, int m, int N) -> poly_t<T> {
if(f.deg() <= 0) {
return f;
}
int k = m / 2;
auto [f0, f1] = std::make_pair(f.mod_xk(k), f.div_xk(k));
auto [A, B] = std::make_pair(self(f0, k, N), self(f1, m - k, N - k));
return (A + (B.mod_xk(N - k) * getpow(k).mod_xk(N - k)).mul_xk(k)).mod_xk(N);
};
int r = n / q;
auto Ar = deriv(A, r);
auto AB0 = compose_dac(Ar, Ar.deg() + 1, n);
auto Bd = deriv(B0.mul_xk(1));
poly_t<T> ans = T(0);
big_vector<poly_t<T>> B1p(r + 1);
B1p[0] = poly_t<T>(T(1));
for(int i = 1; i <= r; i++) {
B1p[i] = (B1p[i - 1] * B1.mod_xk(n - i * q)).mod_xk(n - i * q);
}
while(r >= 0) {
ans += (AB0.mod_xk(n - r * q) * rfact<T>(r) * B1p[r]).mul_xk(r * q).mod_xk(n);
r--;
if(r >= 0) {
AB0 = (integr(AB0 * Bd) + A[r] * fact<T>(r)).mod_xk(n);
}
}
return ans;
}
}
#pragma GCC pop_options
#line 15 "cp-algo/math/poly.hpp"
#ifndef CP_ALGO_MATH_POLY_HPP
#define CP_ALGO_MATH_POLY_HPP
#include "poly/base.hpp"
#include "poly/div.hpp"
#include "poly/series.hpp"
#include "poly/series/sqrt.hpp"
#include "poly/sparse.hpp"
#include "poly/euclid.hpp"
#include "poly/recurrence.hpp"
#include "poly/transform.hpp"
#include "poly/chirpz.hpp"
#include "poly/eval.hpp"
#include "poly/powmod.hpp"
#include "poly/compose.hpp"
#endif
#line 1 "cp-algo/math/poly.hpp"
#line 1 "cp-algo/math/poly/base.hpp"
#line 1 "cp-algo/math/fft.hpp"
#line 1 "cp-algo/math/ring.hpp"
#line 1 "cp-algo/number_theory/discrete_sqrt.hpp"
#line 1 "cp-algo/number_theory/modint.hpp"
#line 1 "cp-algo/math/common.hpp"
#include <functional>
#include <cstdint>
#include <cassert>
#include <bit>
#include <vector>
#include <algorithm>
namespace cp_algo::math{
#ifdef CP_ALGO_MAXN
const int maxn=CP_ALGO_MAXN;
#else
const int maxn=1<<19;
#endif
const int magic=64; template<int window=1> auto bpow(auto const&x,auto n,auto const&one,auto op){ static_assert(window>=1&&window<=6); if constexpr(window>1){ if(n==0){return one;} int bits=std::bit_width(uint64_t(n)); auto low_bit= [&](int high){ int low=std::max(0,high-window+1); while(!((n>>low)&1)){low++;} return low; }; int first=low_bit(bits-1); int cost=(1<<(window-1))+first; for(int j=first-1;j>=0;){ if(!((n>>j)&1)){j--;} else{cost++;j=low_bit(j)-1;} } if(cost>=bits+std::popcount(uint64_t(n))-2){return bpow<1>(x,n,one,op);} using T=std::decay_t<decltype(x)>; std::vector<T>odd; odd.reserve(1<<(window-1)); odd.push_back(x); auto square=op(x,x); while(odd.size()<size_t(1<<(window-1))){odd.push_back(op(odd.back(),square));} auto ans=odd[(n>>first)/2]; for(int j=first-1;j>=0;){ if(!((n>>j)&1)){ans=op(ans,ans);j--;} else{ int low=low_bit(j),length=j-low+1; auto digit=(n>>low)&((1u<<length)-1); for(int i=0;i<length;i++){ans=op(ans,ans);} ans=op(ans,odd[digit/2]); j=low-1; } } return ans; }else{ if(n==0){ return one; } auto ans=x; for(int j=std::bit_width<uint64_t>(n)-2;~j;j--){ ans=op(ans,ans); if((n>>j)&1){ ans=op(ans,x); } } return ans; } } template<int window=1> auto bpow(auto x,auto n,auto ans){ return bpow<window>(x,n,ans,std::multiplies{}); } template<typename T> T bpow(T const&x,auto n){ return bpow(x,n,T(1)); } inline constexpr auto inv2(auto x){ assert(x%2); std::make_unsigned_t<decltype(x)>y=1; while(y*x!=1){ y*=2-x*y; } return y; } }
#line 4 "cp-algo/number_theory/modint.hpp"
#include <iostream>
#line 6 "cp-algo/number_theory/modint.hpp"
namespace cp_algo::math{ template<typename modint,typename _Int> struct modint_base{ using Int=_Int; using UInt=std::make_unsigned_t<Int>; static constexpr size_t bits=sizeof(Int)*8; using Int2=std::conditional_t<bits<=32,int64_t,__int128_t>; using UInt2=std::conditional_t<bits<=32,uint64_t,__uint128_t>; constexpr static Int mod(){ return modint::mod(); } constexpr static UInt remod(){ return modint::remod(); } constexpr static UInt2 modmod(){ return UInt2(mod())*mod(); } constexpr modint_base()=default; constexpr modint_base(Int2 rr){ to_modint().setr(UInt((rr+modmod())%mod())); } constexpr modint inv()const{ return bpow(to_modint(),mod()-2); } modint operator-()const{ modint neg; neg.r=std::min(-r,remod()-r); return neg; } modint&operator/=(const modint&t){ return to_modint()*=t.inv(); } modint&operator*=(const modint&t){ r=UInt(UInt2(r)*t.r%mod()); return to_modint(); } modint&operator+=(const modint&t){ r+=t.r; r=std::min(r,r-remod()); return to_modint(); } modint&operator-=(const modint&t){ r-=t.r; r=std::min(r,r+remod()); return to_modint(); } modint operator+(const modint&t)const{return modint(to_modint())+=t;} modint operator-(const modint&t)const{return modint(to_modint())-=t;} modint operator*(const modint&t)const{return modint(to_modint())*=t;} modint operator/(const modint&t)const{return modint(to_modint())/=t;} auto operator==(const modint&t)const{return to_modint().getr()==t.getr();} auto operator!=(const modint&t)const{return to_modint().getr()!=t.getr();} auto operator<=(const modint&t)const{return to_modint().getr()<=t.getr();} auto operator>=(const modint&t)const{return to_modint().getr()>=t.getr();} auto operator<(const modint&t)const{return to_modint().getr()<t.getr();} auto operator>(const modint&t)const{return to_modint().getr()>t.getr();} Int rem()const{ UInt R=to_modint().getr(); return R-(R>(UInt)mod()/2)*mod(); } constexpr void setr(UInt rr){ r=rr; } constexpr UInt getr()const{ return r; } static uint64_t modmod8(){return uint64_t(8*modmod());} void add_unsafe(UInt t){r+=t;} void pseudonormalize(){r=std::min(r,r-modmod8());} modint const&normalize(){ if(r>=(UInt)mod()){ r%=mod(); } return to_modint(); } void setr_direct(UInt rr){r=rr;} UInt getr_direct()const{return r;} protected: UInt r; private: constexpr modint&to_modint(){return static_cast<modint&>(*this);} constexpr modint const&to_modint()const{return static_cast<modint const&>(*this);} }; template<typename modint> concept modint_type=std::is_base_of_v<modint_base<modint,typename modint::Int>,modint>; template<modint_type modint> decltype(std::cin)&operator>>(decltype(std::cin)&in,modint&x){ typename modint::UInt r; auto&res=in>>r; x.setr(r); return res; } template<modint_type modint> decltype(std::cout)&operator<<(decltype(std::cout)&out,modint const&x){ return out<<x.getr(); } template<auto m> struct modint:modint_base<modint<m>,decltype(m)>{ using Base=modint_base<modint<m>,decltype(m)>; using Base::Base; static constexpr Base::Int mod(){return m;} static constexpr Base::UInt remod(){return m;} auto getr()const{return Base::r;} }; template<typename Int=int> struct dynamic_modint:modint_base<dynamic_modint<Int>,Int>{ using Base=modint_base<dynamic_modint<Int>,Int>; using Base::Base; [[gnu::noinline,gnu::cold]]static Base::UInt m_reduce_reduced(Base::UInt2 ab){ if(mod()%2==0){ return typename Base::UInt(ab%mod()); } typename Base::UInt q= -(typename Base::UInt(ab)*inverse); auto high=typename Base::UInt(ab>>Base::bits); auto low=typename Base::UInt(typename Base::UInt2(q)*typename Base::UInt(mod())>>Base::bits); return high>=low?high-low:high-low+mod(); } static Base::UInt m_reduce(Base::UInt2 ab){ if(imod()==0)[[unlikely]]{ return m_reduce_reduced(ab); } typename Base::UInt2 m=typename Base::UInt(ab)*imod(); return typename Base::UInt((ab+m*mod())>>Base::bits); } static Base::UInt m_transform(Base::UInt a){ if(mod()%2==0)[[unlikely]]{ return a; }else{ return m_reduce(a*pw128()); } } dynamic_modint&operator*=(const dynamic_modint&t){ Base::r=m_reduce(typename Base::UInt2(Base::r)*t.r); return*this; } void setr(Base::UInt rr){ Base::r=m_transform(rr); } Base::UInt getr()const{ typename Base::UInt res=m_reduce(Base::r); return std::min(res,res-mod()); } static Int mod(){return m;} static Base::UInt remod(){return rm;} static Base::UInt imod(){return im;} static Base::UInt2 pw128(){return r2;} static void switch_mod(Int nm){ m=nm; bool lazy=m%2&&typename Base::UInt(m)<=typename Base::UInt(-1)/4; rm=typename Base::UInt(m)*(lazy?2:1); inverse=m%2?inv2(-m):0; im=lazy?inverse:0; r2=static_cast<Base::UInt>(static_cast<Base::UInt2>(-1)%m+1); } auto static with_mod(Int tmp,auto callback){ struct scoped{ Int prev=mod(); ~scoped(){switch_mod(prev);} }_; switch_mod(tmp); return callback(); } private: static inline thread_local Int m=1; static inline thread_local Base::UInt im= -1,r2=0,inverse= -1,rm=2; }; }
#line 1 "cp-algo/random/rng.hpp"
#include <chrono>
#include <random>
namespace cp_algo::random{ std::mt19937_64 gen( std::chrono::steady_clock::now().time_since_epoch().count() ); uint64_t rng(){ return gen(); } }
#line 1 "cp-algo/math/affine.hpp"
#include <optional>
#include <utility>
#line 6 "cp-algo/math/affine.hpp"
#include <tuple>
namespace cp_algo::math{ template<typename base> struct lin{ base a=1,b=0; std::optional<base>c; lin(){} lin(base b):a(0),b(b){} lin(base a,base b):a(a),b(b){} lin(base a,base b,base _c):a(a),b(b),c(_c){} lin operator*(const lin&t){ assert(c&&t.c&& *c== *t.c); return{a*t.b+b*t.a,b*t.b+a*t.a*(*c),*c}; } lin apply(lin const&t)const{ return{a*t.a,a*t.b+b}; } void prepend(lin const&t){ *this=t.apply(*this); } base eval(base x)const{ return a*x+b; } }; template<typename base> struct linfrac{ base a,b,c,d; linfrac():a(1),b(0),c(0),d(1){} linfrac(base a):a(a),b(1),c(1),d(0){} linfrac(base a,base b,base c,base d):a(a),b(b),c(c),d(d){} linfrac operator*(linfrac t)const{ return t.prepend(linfrac(*this)); } linfrac operator-()const{ return{-a,-b,-c,-d}; } linfrac adj()const{ return{d,-b,-c,a}; } linfrac&prepend(linfrac const&t){ t.apply(a,c); t.apply(b,d); return*this; } void apply(base&A,base&B)const{ std::tie(A,B)=std::pair{a*A+b*B,c*A+d*B}; } }; }
#line 6 "cp-algo/number_theory/discrete_sqrt.hpp"
namespace cp_algo::math{ template<modint_type base> std::optional<base>sqrt(base b){ if(b==base(0)){ return base(0); }else if(bpow(b,(b.mod()-1)/2)!=base(1)){ return std::nullopt; }else{ while(true){ base z=random::rng(); if(z*z==b){ return z; } lin<base>x(1,z,b); x=bpow(x,(b.mod()-1)/2,lin<base>(0,1,b)); if(x.a!=base(0)){ return x.a.inv(); } } } } }
#line 1 "cp-algo/number_theory/primality.hpp"
#line 6 "cp-algo/number_theory/primality.hpp"
namespace cp_algo::math{ template<typename _Int> bool is_prime(_Int m){ using Int=std::make_signed_t<_Int>; using UInt=std::make_unsigned_t<Int>; if(m==1||m%2==0){ return m==2; } int s=std::countr_zero(UInt(m-1)); auto d=(m-1)>>s; using base=dynamic_modint<Int>; auto test= [&](base x){ x=bpow(x,d); if(std::abs(x.rem())<=1){ return true; } for(int i=1;i<s&&x!= -1;i++){ x*=x; } return x== -1; }; return base::with_mod(m,[&](){
#ifdef CP_ALGO_NUMBER_THEORY_PRIMALITY_BASES_HPP
uint16_t base2=7,base3=61; if(m!=uint32_t(m)){ base2=base_table1[uint32_t(m*0xAD625B89)>>18]; base3=base_table2[base2>>13]; } return test(2)&&test(base2)&&test(base3);
#else
return std::ranges::all_of(std::array{2,325,9375,28178,450775,9780504,1795265022},test);
#endif
}); } }
#line 1 "cp-algo/util/checkpoint.hpp"
#line 1 "cp-algo/util/big_alloc.hpp"
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#line 10 "cp-algo/util/big_alloc.hpp"
#include <string>
#include <cstddef>
#line 14 "cp-algo/util/big_alloc.hpp"
#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::has_single_bit(Align),"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; 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); 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>>; }
#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 1 "cp-algo/math/cvector.hpp"
#line 1 "cp-algo/util/simd.hpp"
#include <experimental/simd>
#line 6 "cp-algo/util/simd.hpp"
#include <memory>
#if defined(__x86_64__) && !defined(CP_ALGO_DISABLE_AVX2)
#define CP_ALGO_SIMD_AVX2_TARGET _Pragma("GCC target(\"avx2,fma\")")
#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 u8x16=simd<uint8_t,16>; 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 u32x8 reduce_once(u32x8 x,uint32_t mod){ auto y=x-mod; return x<y?x:y; } inline u64x4 reduce_once(u64x4 x,uint32_t mod){ return u64x4(reduce_once(u32x8(x),mod)); } 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/complex.hpp"
#line 4 "cp-algo/util/complex.hpp"
#include <cmath>
#include <type_traits>
#line 7 "cp-algo/util/complex.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo{ template<typename T> struct complex{ using value_type=T; T x,y; inline constexpr complex():x(),y(){} inline constexpr complex(T const&x):x(x),y(){} inline constexpr complex(T const&x,T const&y):x(x),y(y){} inline complex&operator*=(T const&t){x*=t;y*=t;return*this;} inline complex&operator/=(T const&t){x/=t;y/=t;return*this;} inline complex operator*(T const&t)const{return complex(*this)*=t;} inline complex operator/(T const&t)const{return complex(*this)/=t;} inline complex&operator+=(complex const&t){x+=t.x;y+=t.y;return*this;} inline complex&operator-=(complex const&t){x-=t.x;y-=t.y;return*this;} inline complex operator*(complex const&t)const{return{x*t.x-y*t.y,x*t.y+y*t.x};} inline complex operator/(complex const&t)const{return*this*t.conj()/t.norm();} inline complex operator+(complex const&t)const{return complex(*this)+=t;} inline complex operator-(complex const&t)const{return complex(*this)-=t;} inline complex&operator*=(complex const&t){return*this= *this*t;} inline complex&operator/=(complex const&t){return*this= *this/t;} inline complex operator-()const{return{-x,-y};} inline complex conj()const{return{x,-y};} inline T norm()const{return x*x+y*y;} inline T abs()const{return std::sqrt(norm());} inline T const real()const{return x;} inline T const imag()const{return y;} inline T&real(){return x;} inline T&imag(){return y;} inline static constexpr complex polar(T r,T theta){return{T(r*cos(theta)),T(r*sin(theta))};} inline auto operator<=>(complex const&t)const=default; }; template<typename T>inline complex<T>conj(complex<T>const&x){return x.conj();} template<typename T>inline T norm(complex<T>const&x){return x.norm();} template<typename T>inline T abs(complex<T>const&x){return x.abs();} template<typename T>inline T&real(complex<T> &x){return x.real();} template<typename T>inline T&imag(complex<T> &x){return x.imag();} template<typename T>inline T const real(complex<T>const&x){return x.real();} template<typename T>inline T const imag(complex<T>const&x){return x.imag();} template<typename T> inline constexpr complex<T>polar(T r,T theta){ return complex<T>::polar(r,theta); } template<typename T> inline std::ostream&operator<<(std::ostream&out,complex<T>const&x){ return out<<x.real()<<' '<<x.imag(); } }
#pragma GCC pop_options
#line 7 "cp-algo/math/cvector.hpp"
#include <immintrin.h>
#include <numbers>
#include <cstring>
#include <ranges>
#line 13 "cp-algo/math/cvector.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace stdx=std::experimental; namespace cp_algo::math::fft{ static constexpr size_t flen=4; using ftype=double; using vftype=dx4; using point=complex<ftype>; using vpoint=complex<vftype>; static constexpr vftype vz={}; vpoint vi(vpoint const&r){ return{-imag(r),real(r)}; } template<class T> struct spectrum_alloc:big_alloc<T>{ using big_alloc<T>::big_alloc; template<class U> struct rebind{ using other=spectrum_alloc<U>; }; template<class U> requires(std::is_same_v<U,vpoint>) void construct(U*)noexcept{} }; using spectrum_vector=std::vector<vpoint,spectrum_alloc<vpoint>>; struct cvector{ spectrum_vector r; cvector(size_t n){ n=std::max(flen,std::bit_ceil(n)); r.assign(n/flen,vpoint{}); prepare_roots(n/16); checkpoint("cvector create"); } vpoint&at(size_t k){return r[k/flen];} vpoint at(size_t k)const{return r[k/flen];} template<class pt=point> inline void set(size_t k,pt const&t){ if constexpr(std::is_same_v<pt,point>){ real(r[k/flen])[k%flen] =real(t); imag(r[k/flen])[k%flen] =imag(t); }else{ at(k)=t; } } template<class pt=point> inline pt get(size_t k)const{ if constexpr(std::is_same_v<pt,point>){ return{real(r[k/flen])[k%flen],imag(r[k/flen])[k%flen]}; }else{ return at(k); } } size_t size()const{ return flen*r.size(); } static constexpr size_t eval_arg(size_t n){ if(n<pre_evals){ return eval_args[n]; }else{ return eval_arg(n/2)|(n&1)<<(std::bit_width(n)-1); } } static constexpr point eval_point(size_t n){ if(n%2){ return-eval_point(n-1); }else if(n%4){ return eval_point(n-2)*point(0,1); }else if(n/4<pre_evals){ return evalp[n/4]; }else if(n/4-pre_evals<extra.size()){ return extra[n/4-pre_evals]; }else{ return polar<ftype>(1.,std::numbers::pi/(ftype)std::bit_floor(n)*(ftype)eval_arg(n)); } } static constexpr std::array<point,32>roots= [](){ std::array<point,32>res; for(size_t i=2;i<32;i++){ res[i] =polar<ftype>(1.,std::numbers::pi/(1ull<<(i-2))); } return res; }(); static constexpr point root(size_t n){ return roots[std::bit_width(n)]; } template<int step> static void exec_on_eval(size_t n,size_t k,auto&&callback){ callback(k,root(4*step*n)*eval_point(step*k)); } template<int step> static void exec_on_evals(size_t n,auto&&callback){ point factor=root(4*step*n); if constexpr(step==1||step==2||step==4){ prepare_roots((step*n+3)/4); size_t i=0; if constexpr(step==1){ for(;i+4<=n;i+=4){ size_t k=i/4; point e=k<pre_evals?evalp[k] :extra[k-pre_evals]; point v=factor*e; callback(i,v); callback(i+1,-v); point iv(-imag(v),real(v)); callback(i+2,iv); callback(i+3,-iv); } }else if constexpr(step==2){ for(;i+2<=n;i+=2){ size_t k=i/2; point e=k<pre_evals?evalp[k] :extra[k-pre_evals]; point v=factor*e; callback(i,v); callback(i+1,point(-imag(v),real(v))); } } for(;i<n;i++){ size_t index=step*i,k=index/4; point e=k<pre_evals?evalp[k] :extra[k-pre_evals]; if(index&2){ e=e*point(0,1); } if(index&1){ e= -e; } callback(i,factor*e); } }else{ for(size_t i=0;i<n;i++){ callback(i,factor*eval_point(step*i)); } } } static void do_dot_iter(point rt,vpoint&Bv,vpoint const&Av,vpoint&res){ res+=Av*Bv; real(Bv)=rotate_right(real(Bv)); imag(Bv)=rotate_right(imag(Bv)); auto x=real(Bv)[0],y=imag(Bv)[0]; real(Bv)[0] =x*real(rt)-y*imag(rt); imag(Bv)[0] =x*imag(rt)+y*real(rt); } template<size_t fixed=0> void dot(cvector const&t){ size_t n=fixed?fixed:this->size(); exec_on_evals<1>(n/flen,[&] [[gnu::always_inline]](size_t k,point rt){ k*=flen; auto[Ax,Ay] =at(k); auto Cv=t.at(k); vpoint vrt={vz+real(rt),vz+imag(rt)}; auto Cr=Cv*vrt; vpoint res=vz; auto iter= [&]<int i> [[gnu::always_inline]](){ auto wrap= [&](vftype original,vftype rotated){ if constexpr(i==0){ return original; }else{ return __builtin_shufflevector(rotated,original,4-i,5-i,6-i,7-i); } }; vpoint Cw={wrap(real(Cv),real(Cr)),wrap(imag(Cv),imag(Cr))}; vpoint Av={vz+Ax[i],vz+Ay[i]}; return Av*Cw; }; auto p0=iter.template operator()<0>(),p1=iter.template operator()<1>(); auto p2=iter.template operator()<2>(),p3=iter.template operator()<3>(); res=(p0+p1)+(p2+p3); set(k,res); }); checkpoint("dot"); } template<bool partial=true,bool normalize=true,size_t fixed=0> void ifft(){ size_t n=fixed?fixed:size(); if constexpr(!partial){ prepare_roots(n/4); point pi(0,1); exec_on_evals<4>(n/4,[&] [[gnu::always_inline]](size_t k,point rt){ k*=4; point v1=conj(rt); point v2=v1*v1; point v3=v1*v2; auto A=get(k); auto B=get(k+1); auto C=get(k+2); auto D=get(k+3); set(k,(A+B)+(C+D)); set(k+2,((A+B)-(C+D))*v2); set(k+1,((A-B)-pi*(C-D))*v1); set(k+3,((A-B)+pi*(C-D))*v3); }); } bool parity=std::countr_zero(n)%2; if(parity){ exec_on_evals<2>(n/(2*flen),[&] [[gnu::always_inline]](size_t k,point rt){ k*=2*flen; vpoint cvrt={vz+real(rt),vz-imag(rt)}; auto B=at(k)-at(k+flen); at(k)+=at(k+flen); at(k+flen)=B*cvrt; }); } transform<true,fixed>(n,parity); checkpoint("ifft"); if constexpr(normalize){ auto scale=vz+ftype(partial?flen:1)/ftype(n); for(size_t k=0;k<n;k+=flen){ set(k,get<vpoint>(k)*scale); } } } template<bool partial=true,size_t fixed=0> void fft(){ size_t n=fixed?fixed:size(); prepare_roots(n/(partial?16:4)); bool parity=std::countr_zero(n)%2; transform<false,fixed>(n,parity); if(parity){ exec_on_evals<2>(n/(2*flen),[&] [[gnu::always_inline]](size_t k,point rt){ k*=2*flen; vpoint vrt={vz+real(rt),vz+imag(rt)}; auto t=at(k+flen)*vrt; at(k+flen)=at(k)-t; at(k)+=t; }); } if constexpr(!partial){ prepare_roots(n/4); point pi(0,1); exec_on_evals<4>(n/4,[&] [[gnu::always_inline]](size_t k,point rt){ k*=4; point v1=rt; point v2=v1*v1; point v3=v1*v2; auto A=get(k); auto B=get(k+1)*v1; auto C=get(k+2)*v2; auto D=get(k+3)*v3; set(k,(A+C)+(B+D)); set(k+1,(A+C)-(B+D)); set(k+2,(A-C)+pi*(B-D)); set(k+3,(A-C)-pi*(B-D)); }); } checkpoint("fft"); } static std::array<vpoint,4>transpose(std::array<vpoint,4>const&a){ auto half= [](auto part){ auto a0=__m256d(part(0)),a1=__m256d(part(1)),a2=__m256d(part(2)),a3=__m256d(part(3)); auto t0=_mm256_unpacklo_pd(a0,a1),t1=_mm256_unpackhi_pd(a0,a1),t2=_mm256_unpacklo_pd(a2,a3), t3=_mm256_unpackhi_pd(a2,a3); return std::array<vftype,4>{ vftype(_mm256_permute2f128_pd(t0,t2,0x20)),vftype(_mm256_permute2f128_pd(t1,t3,0x20)), vftype(_mm256_permute2f128_pd(t0,t2,0x31)),vftype(_mm256_permute2f128_pd(t1,t3,0x31))}; }; auto re=half([&](int k){return real(a[k]);}),im=half([&](int k){return imag(a[k]);}); return{vpoint{re[0],im[0]},vpoint{re[1],im[1]},vpoint{re[2],im[2]},vpoint{re[3],im[3]}}; } static constexpr size_t dot_tile=64; template<size_t fixed,bool square=false> void dot_fused16(cvector const&t,size_t offset,size_t length){ constexpr size_t T=dot_tile; const size_t n=fixed?fixed:size(); point factor=root(n); auto fr=_mm256_set1_pd(real(factor)),fi=_mm256_set1_pd(imag(factor)); alignas(32)double tw[6][T]; const bool ahead=offset+length<n; auto cmadd= [] [[gnu::always_inline]](vpoint x,vpoint y,vpoint c){ auto re=_mm256_fmadd_pd(__m256d(real(x)),__m256d(real(y)), _mm256_fnmadd_pd(__m256d(imag(x)),__m256d(imag(y)),__m256d(real(c)))); auto im=_mm256_fmadd_pd(__m256d(real(x)),__m256d(imag(y)), _mm256_fmadd_pd(__m256d(imag(x)),__m256d(real(y)),__m256d(imag(c)))); return vpoint{vftype(re),vftype(im)}; }; auto cmcross= [] [[gnu::always_inline]](vpoint x,vpoint y,vpoint p,vpoint q){ auto re=_mm256_sub_pd(_mm256_fmsub_pd(__m256d(real(x)),__m256d(real(y)),__m256d(real(p))), _mm256_fmadd_pd(__m256d(imag(x)),__m256d(imag(y)),__m256d(real(q)))); auto im=_mm256_add_pd(_mm256_fmsub_pd(__m256d(real(x)),__m256d(imag(y)),__m256d(imag(p))), _mm256_fmsub_pd(__m256d(imag(x)),__m256d(real(y)),__m256d(imag(q)))); return vpoint{vftype(re),vftype(im)}; }; auto mulconj= [] [[gnu::always_inline]](vpoint z,vpoint v){ auto re=_mm256_fmadd_pd(__m256d(real(z)),__m256d(real(v)), _mm256_mul_pd(__m256d(imag(z)),__m256d(imag(v)))); auto im=_mm256_fmsub_pd(__m256d(imag(z)),__m256d(real(v)), _mm256_mul_pd(__m256d(real(z)),__m256d(imag(v)))); return vpoint{vftype(re),vftype(im)}; }; for(size_t tile=offset;tile<offset+length;tile+=16*T){ size_t k0=tile/16; auto const*e=reinterpret_cast<double const*>(k0<pre_evals?evalp.data()+k0 :extra.data()+(k0-pre_evals)); for(size_t g=0;g<T;g+=4){ auto x=_mm256_loadu_pd(e+2*g),y=_mm256_loadu_pd(e+2*g+4); auto er=_mm256_permute4x64_pd(_mm256_unpacklo_pd(x,y),0xD8), ei=_mm256_permute4x64_pd(_mm256_unpackhi_pd(x,y),0xD8); auto r1=_mm256_fmsub_pd(fr,er,_mm256_mul_pd(fi,ei)), i1=_mm256_fmadd_pd(fr,ei,_mm256_mul_pd(fi,er)); auto r2=_mm256_fmsub_pd(r1,r1,_mm256_mul_pd(i1,i1)), i2=_mm256_fmadd_pd(r1,i1,_mm256_mul_pd(i1,r1)); auto r3=_mm256_fmsub_pd(r1,r2,_mm256_mul_pd(i1,i2)), i3=_mm256_fmadd_pd(r1,i2,_mm256_mul_pd(i1,r2)); _mm256_store_pd(tw[0] +g,r1); _mm256_store_pd(tw[1] +g,i1); _mm256_store_pd(tw[2] +g,r2); _mm256_store_pd(tw[3] +g,i2); _mm256_store_pd(tw[4] +g,r3); _mm256_store_pd(tw[5] +g,i3); } for(size_t g=0;g<T;g++){ size_t pos=tile+16*g; if(ahead){ auto const*pa=reinterpret_cast<char const*>(r.data()+(pos+length)/flen); auto const*pb=reinterpret_cast<char const*>(t.r.data()+(pos+length)/flen); for(size_t c=0;c<4;c++){ _mm_prefetch(pa+64*c,_MM_HINT_T2); _mm_prefetch(pb+64*c,_MM_HINT_T2); } } auto bc= [&] [[gnu::always_inline]](size_t c){ return vftype(_mm256_broadcast_sd(tw[c] +g)); }; vpoint v1={bc(0),bc(1)},v2={bc(2),bc(3)},v3={bc(4),bc(5)}; auto forward= [&] [[gnu::always_inline]](cvector const&a){ auto A=a.at(pos),B=a.at(pos+4)*v1,C=a.at(pos+8)*v2,D=a.at(pos+12)*v3; return std::array<vpoint,4>{(A+C)+(B+D),(A+C)-(B+D),(A-C)+vi(B-D), (A-C)-vi(B-D)}; }; auto a=transpose(forward(*this)); auto b= [&]{ if constexpr(square){ return a; }else{ return transpose(forward(t)); } }(); const auto flip_re=_mm256_set_pd(0.,-0.,-0.,0.),flip_im=_mm256_set_pd(-0.,0.,-0.,0.); vpoint w={ vftype(_mm256_xor_pd(_mm256_blend_pd(__m256d(real(v1)),__m256d(imag(v1)),0b1100),flip_re)), vftype(_mm256_xor_pd(_mm256_blend_pd(__m256d(imag(v1)),__m256d(real(v1)),0b1100),flip_im))}; auto mul= [&] [[gnu::always_inline]](vpoint a0,vpoint a1,vpoint b0,vpoint b1){ auto p=a0*b0,q=a1*b1; return std::array<vpoint,2>{cmadd(w,q,p),cmcross(a0+a1,b0+b1,p,q)}; }; auto p=mul(a[0],a[2],b[0],b[2]),q=mul(a[1],a[3],b[1],b[3]); auto m=mul(a[0] +a[1],a[2] +a[3],b[0] +b[1],b[2] +b[3]); auto c=transpose({cmadd(w,q[1],p[0]),(m[0] -p[0])-q[0],p[1] +q[0],(m[1] -p[1])-q[1]}); auto A=c[0],B=c[1],C=c[2],D=c[3]; at(pos)=(A+B)+(C+D); at(pos+8)=mulconj((A+B)-(C+D),v2); at(pos+4)=mulconj((A-B)-vi(C-D),v1); at(pos+12)=mulconj((A-B)+vi(C-D),v3); } } } void forward(){ if(!fused_leaves()){ return fft(); } size_t n=size(),part=fused_part(); if(part<n){ vpoint vrt={vz+real(roots[4]),vz+imag(roots[4])}; for(size_t k=0;k<part;k+=flen){ auto t=at(k+part)*vrt; at(k+part)=at(k)-t; at(k)+=t; } } for(size_t offset=0;offset<n;offset+=part){ transform<false,0,0,true>(n,false,offset,part); } checkpoint("fft"); } void multiply(cvector const&t){ if(!fused_leaves()){ dot(t); return ifft<true,false>(); } size_t n=size(),part=fused_part(); dot_fused16<0>(t,0,n); checkpoint("dot"); for(size_t offset=0;offset<n;offset+=part){ transform<true,0,0,true>(n,false,offset,part); } if(part<n){ vpoint cvrt={vz+real(roots[4]),vz-imag(roots[4])}; for(size_t k=0;k<part;k+=flen){ auto t=at(k)-at(k+part); at(k)+=at(k+part); at(k+part)=t*cvrt; } } checkpoint("ifft"); } struct fuse_args{ const uint32_t*src=nullptr; size_t count=0; uint64_t seed=0; double a=0,b=0,a_over_p=0,b_over_p=0; }; static constexpr size_t sweep_tile=256; template<bool inverse,int Mode,size_t Tile,int Fuse=0,bool Neg=false> void sweep8(fuse_args const&fa={}){ constexpr size_t n=1<<24; static const std::array<std::array<point,7>,9>weights= [](){ std::array<std::array<point,7>,9>table; for(size_t count:{size_t(1),size_t(8)}){ for(size_t k=0;k<count;k++){ size_t rev=0,x=k; for(size_t c=count;c>1;c>>=1){ rev=(rev<<1)|(x&1); x>>=1; } long double angle=std::numbers::pi_v<long double> *(1+4*rev)/(16*count); if(k&1){ angle-=std::numbers::pi_v<long double> /4; } for(size_t j=1;j<8;j++){ long double a=j*angle; if constexpr(Mode==0){ table[(count-1)/7+k][j-1] ={double(cosl(a)),double(sinl(a))}; }else{ table[(count-1)/7+k][j-1] ={double(sinl(a)),double(tanl(a/2))}; } } } } return table; }(); auto lift= [&] [[gnu::always_inline]](size_t idx)->vpoint{ i32x4 bits{}; if(idx+4<=fa.count){ std::memcpy(&bits,fa.src+idx,sizeof(bits)); }else if(idx<fa.count){ for(size_t j=0;j<fa.count-idx;j++){ bits[j] =int32_t(fa.src[idx+j]); } }else{ return vpoint{vz,vz}; } auto x=__builtin_convertvector(bits,vftype); u32x4 h= u32x4{uint32_t(idx),uint32_t(idx+1),uint32_t(idx+2),uint32_t(idx+3)}^uint32_t(fa.seed); h*=0x9E3779B1u; h^=h>>15; h*=0x85EBCA77u; h^=h>>13; h*=0xC2B2AE3Du; h^=h>>16; auto noise=__builtin_convertvector(i32x4(h),vftype)*0x1p-32; auto q=round(x*fa.a_over_p+noise),t=round(x*fa.b_over_p+noise); auto re=x-q*fa.a-t*fa.b,im=t*fa.a-q*fa.b; return vpoint{re,Neg?-im:im}; }; auto stage= [&]<bool top> [[gnu::always_inline]](size_t offset,size_t length,size_t begin,size_t end){ size_t step=length/8,k=offset/length; auto const&w=weights[(n/length-1)/7+k]; auto rot= [&]<size_t J> [[gnu::always_inline]](vpoint z){ auto c=w[J-1]; if constexpr(Mode==0){ return z*vpoint{vz+real(c),inverse?vz-imag(c):vz+imag(c)}; }else{ auto s=inverse?vz-real(c):vz+real(c),t=inverse?vz-imag(c):vz+imag(c); auto x=vftype(_mm256_fnmadd_pd(__m256d(t),__m256d(imag(z)),__m256d(real(z)))); auto y=vftype(_mm256_fmadd_pd(__m256d(s),__m256d(x),__m256d(imag(z)))); return vpoint{vftype(_mm256_fnmadd_pd(__m256d(t),__m256d(y),__m256d(x))),y}; } }; auto add= [] [[gnu::always_inline]](vpoint a,vpoint b){ if constexpr(Mode!=2){ return a+b; }else{ return vpoint{vftype(_mm256_fmadd_pd(__m256d(real(a)),_mm256_set1_pd(1),__m256d(real(b)))), vftype(_mm256_fmadd_pd(__m256d(imag(a)),_mm256_set1_pd(1),__m256d(imag(b))))}; } }; auto sub= [] [[gnu::always_inline]](vpoint a,vpoint b){ if constexpr(Mode!=2){ return a-b; }else{ return vpoint{vftype(_mm256_fmsub_pd(__m256d(real(a)),_mm256_set1_pd(1),__m256d(real(b)))), vftype(_mm256_fmsub_pd(__m256d(imag(a)),_mm256_set1_pd(1),__m256d(imag(b))))}; } }; auto d4= [] [[gnu::always_inline]](vpoint a,vpoint b,vpoint c,vpoint d){ auto s=a+c,t=a-c,u=b+d,v=vi(b-d); if constexpr(inverse){ return std::array<vpoint,4>{s+u,t-v,s-u,t+v}; }else{ return std::array<vpoint,4>{s+u,t+v,s-u,t-v}; } }; constexpr double q=0.707106781186547524400844362104849039; auto r1= [] [[gnu::always_inline]](vpoint z){ if constexpr(inverse){ return vpoint{(real(z)+imag(z))*q,(imag(z)-real(z))*q}; }else{ return vpoint{(real(z)-imag(z))*q,(real(z)+imag(z))*q}; } }; auto r3= [] [[gnu::always_inline]](vpoint z){ if constexpr(inverse){ return vpoint{(imag(z)-real(z))*q,(-imag(z)-real(z))*q}; }else{ return vpoint{(-real(z)-imag(z))*q,(real(z)-imag(z))*q}; } }; std::array<vpoint*,8>input,output; for(size_t j=0;j<8;j++){ input[j] =output[j] =r.data()+(offset+begin+j*step)/flen; } if(k&1){ constexpr std::array<size_t,8>perm={7,6,4,5,0,1,2,3}; for(size_t j=0;j<8;j++){ if constexpr(inverse){ input[j] =output[perm[j]]; }else{ output[j] =input[perm[j]]; } } } constexpr bool fused_in=top&&Fuse==1&& !inverse; for(size_t j=0;j<(end-begin)/flen;j++){ size_t base=offset+begin+j*flen; auto in= [&]<size_t S> [[gnu::always_inline]](){ if constexpr(fused_in){ return lift(base+S*step); }else{ return input[S][j]; } }; if constexpr(!inverse){ auto E= d4(in.template operator()<0>(),rot.template operator()<2>(in.template operator()<2>()), rot.template operator()<4>(in.template operator()<4>()), rot.template operator()<6>(in.template operator()<6>())); auto O=d4(rot.template operator()<1>(in.template operator()<1>()), rot.template operator()<3>(in.template operator()<3>()), rot.template operator()<5>(in.template operator()<5>()), rot.template operator()<7>(in.template operator()<7>())); auto a=O[0],b=r1(O[1]),c=vi(O[2]),d=r3(O[3]); output[0][j] =add(E[0],a); output[1][j] =sub(E[0],a); output[2][j] =add(E[2],c); output[3][j] =sub(E[2],c); output[4][j] =add(E[1],b); output[5][j] =sub(E[1],b); output[6][j] =add(E[3],d); output[7][j] =sub(E[3],d); }else{ auto E=d4(add(input[0][j],input[1][j]),add(input[4][j],input[5][j]), add(input[2][j],input[3][j]),add(input[6][j],input[7][j])); auto O=d4(sub(input[0][j],input[1][j]),r1(sub(input[4][j],input[5][j])), -vi(sub(input[2][j],input[3][j])),r3(sub(input[6][j],input[7][j]))); output[0][j] =E[0]; output[1][j] =rot.template operator()<1>(O[0]); output[2][j] =rot.template operator()<2>(E[1]); output[3][j] =rot.template operator()<3>(O[1]); output[4][j] =rot.template operator()<4>(E[2]); output[5][j] =rot.template operator()<5>(O[2]); output[6][j] =rot.template operator()<6>(E[3]); output[7][j] =rot.template operator()<7>(O[3]); } } }; constexpr size_t h=n/64; for(size_t j=0;j<h;j+=Tile){ size_t end=std::min(h,j+Tile); auto first= [&](){ for(size_t t=0;t<8;t++){ stage.template operator()<true>(0,n,j+t*h,end+t*h); } }; auto second= [&](){ for(size_t k=0;k<8;k++){ stage.template operator()<false>(k*n/8,n/8,j,end); } }; if constexpr(inverse){ second(); first(); }else{ first(); second(); } } } static constexpr bool fuse_forward=false; template<bool Neg,bool Fused=fuse_forward,bool Square=false> void cache_product(cvector&b,fuse_args const&fa={},fuse_args const&fb={}){ constexpr size_t n=1<<24,block=1<<18; prepare_roots(n/16); prepare_shear_roots(); if constexpr(Fused){ sweep8<false,2,sweep_tile,1,Neg>(fa); if constexpr(!Square){ b.sweep8<false,2,sweep_tile,1,Neg>(fb); } }else{ sweep8<false,2,sweep_tile>(); if constexpr(!Square){ b.sweep8<false,2,sweep_tile>(); } } checkpoint("sweep forward"); for(size_t offset=0;offset<n;offset+=block){ transform<false,n,block,true>(n,false,offset,block); if constexpr(!Square){ b.transform<false,n,block,true>(n,false,offset,block); } dot_fused16<n,Square>(b,offset,block); transform<true,n,block,true>(n,false,offset,block); } checkpoint("blocks"); sweep8<true,2,sweep_tile>(); checkpoint("sweep inverse"); } static constexpr size_t pre_evals=1<<16; static const std::array<size_t,pre_evals>eval_args; static const std::array<point,pre_evals>evalp; private: size_t fused_part()const{ return size()>>(std::countr_zero(size())%2); } bool fused_leaves()const{ return fused_part()>=16*dot_tile; } template<bool inverse,size_t fixed=0,size_t range_fixed=0,bool omit16=false> void transform(size_t input_n,bool parity,size_t range_offset=0,size_t range_length=0){ if constexpr(range_fixed){ range_length=range_fixed; } const size_t n=fixed?fixed:input_n; if constexpr(!range_fixed){ prepare_roots(n/16); if constexpr(fixed==(1<<24)){ prepare_shear_roots(); } } size_t log_n=std::countr_zero(n); auto butterfly= [&] [[gnu::always_inline]](size_t offset,size_t length,size_t begin,size_t end){ if constexpr(omit16){ if(length==16){ return; } } size_t step=length/4,log_length=std::countr_zero(length),k=offset>>log_length; auto*p0=r.data()+(offset+begin)/flen,*p1=r.data()+(offset+begin+step)/flen, *p2=r.data()+(offset+begin+2*step)/flen, *p3=r.data()+(offset+begin+3*step)/flen; auto run= [&]<bool shear> [[gnu::always_inline]](){ vpoint v1,v2,v3; vftype t1{},t2{},t3{}; if constexpr(shear&&fixed==(1<<24)){ auto const&c=shear_roots[((n>>log_length)-1)/3+k]; v1={vz,inverse?vz-real(c[0]):vz+real(c[0])}; v2={vz,inverse?vz-real(c[1]):vz+real(c[1])}; v3={vz,inverse?vz-real(c[2]):vz+real(c[2])}; t1=inverse?vz-imag(c[0]):vz+imag(c[0]); t2=inverse?vz-imag(c[1]):vz+imag(c[1]); t3=inverse?vz-imag(c[2]):vz+imag(c[2]); }else{ point e=k<pre_evals?evalp[k] :extra[k-pre_evals]; point rt=roots[log_n+5-log_length] *e; v1={vz+real(rt),inverse?vz-imag(rt):vz+imag(rt)}; if constexpr(shear){ if(k&1){ if constexpr(inverse){ v1=vi(v1); }else{ v1= -vi(v1); } } } v2=v1*v1; v3=v1*v2; if constexpr(shear){ t1=imag(v1)/(vz+1.0+real(v1)); t2=imag(v2)/(vz+1.0+real(v2)); t3=imag(v3)/(vz+1.0+real(v3)); } } auto rotate= [] [[gnu::always_inline]](vpoint z,vpoint v,vftype t){ if constexpr(!shear){ return z*v; }else{ auto x=vftype(_mm256_fnmadd_pd(__m256d(t),__m256d(imag(z)),__m256d(real(z)))); auto y=vftype(_mm256_fmadd_pd(__m256d(imag(v)),__m256d(x),__m256d(imag(z)))); return vpoint{vftype(_mm256_fnmadd_pd(__m256d(t),__m256d(y),__m256d(x))),y}; } }; auto*i0=p0,*i1=p1,*i2=p2,*i3=p3,*o0=p0,*o1=p1,*o2=p2,*o3=p3; if constexpr(shear){ if(k&1){ if constexpr(inverse){ i0=p3; i1=p2; i2=p0; i3=p1; }else{ o0=p3; o1=p2; o2=p0; o3=p1; } } } for(size_t j=0;j<(end-begin)/flen;j++){ auto A=i0[j],B=i1[j],C=i2[j],D=i3[j]; if constexpr(inverse){ o0[j] =(A+B)+(C+D); o2[j] =rotate((A+B)-(C+D),v2,t2); o1[j] =rotate((A-B)-vi(C-D),v1,t1); o3[j] =rotate((A-B)+vi(C-D),v3,t3); }else{ B=rotate(B,v1,t1); C=rotate(C,v2,t2); D=rotate(D,v3,t3); o0[j] =(A+C)+(B+D); o1[j] =(A+C)-(B+D); o2[j] =(A-C)+vi(B-D); o3[j] =(A-C)-vi(B-D); } } }; if(length>=shear_min<fixed>){ run.template operator()<true>(); }else{ run.template operator()<false>(); } }; auto recurse= [&](this auto const&self,size_t offset,size_t length)->void{ if(length<4*flen){ return; } if(length>=(1<<15)){ size_t step=length/16; if constexpr(inverse){ for(size_t t=0;t<16;t++){ self(offset+t*step,step); } } for(size_t j=0;j<step;j+=256){ size_t end=std::min(step,j+256); if constexpr(inverse){ for(size_t t=0;t<4;t++){ butterfly(offset+t*length/4,length/4,j,end); } for(size_t t=0;t<4;t++){ butterfly(offset,length,j+t*step,end+t*step); } }else{ for(size_t t=0;t<4;t++){ butterfly(offset,length,j+t*step,end+t*step); } for(size_t t=0;t<4;t++){ butterfly(offset+t*length/4,length/4,j,end); } } } if constexpr(!inverse){ for(size_t t=0;t<16;t++){ self(offset+t*step,step); } } }else if(length>=(size_t(1)<<(6+parity))){ auto finish= [&]<bool par>(){ constexpr size_t chunk=size_t(1)<<(6+par); constexpr size_t bottom=size_t(1)<<(4+par); auto small= [&]<size_t L> [[gnu::always_inline]](this auto const&self,size_t pos)->void{ if constexpr(inverse&&L>bottom){ for(size_t q=0;q<4;q++){ self.template operator()<L/4>(pos+q*(L/4)); } } butterfly(pos,L,0,L/4); if constexpr(!inverse&&L>bottom){ for(size_t q=0;q<4;q++){ self.template operator()<L/4>(pos+q*(L/4)); } } }; for(size_t leaf=offset;leaf<offset+length;leaf+=chunk){ if constexpr(inverse){ small.template operator()<chunk>(leaf); size_t level= std::min<size_t>(std::countr_one(leaf+chunk-1),std::countr_zero(length)); for(size_t lvl=6+2+par;lvl<=level;lvl+=2){ size_t len=size_t(1)<<lvl; butterfly(leaf& ~(len-1),len,0,len/4); } }else{ size_t level=std::min<size_t>(std::countr_zero(n+leaf),std::countr_zero(length)); level-=level%2!=par; for(size_t lvl=level;lvl>=6+2+par;lvl-=2){ size_t len=size_t(1)<<lvl; butterfly(leaf& ~(len-1),len,0,len/4); } small.template operator()<chunk>(leaf); } } }; if(parity){ finish.template operator()<true>(); }else{ finish.template operator()<false>(); } }else{ if constexpr(inverse){ for(size_t leaf=offset+3*flen;leaf<offset+length;leaf+=4*flen){ size_t level=std::min<size_t>(std::countr_one(leaf+3),std::countr_zero(length)); for(size_t lvl=4+parity;lvl<=level;lvl+=2){ size_t len=size_t(1)<<lvl; butterfly(leaf& ~(len-1),len,0,len/4); } } }else{ for(size_t leaf=offset;leaf<offset+length;leaf+=4*flen){ size_t level=std::min<size_t>(std::countr_zero(n+leaf),std::countr_zero(length)); level-=level%2!=parity; for(size_t lvl=level;lvl>=4;lvl-=2){ size_t len=size_t(1)<<lvl; butterfly(leaf& ~(len-1),len,0,len/4); } } } } }; recurse(range_offset,range_length?range_length:n); } template<size_t fixed> static constexpr size_t shear_min=fixed==(1<<24)?64:256; static big_vector<std::array<point,3>>shear_roots; static void prepare_shear_roots(){ constexpr size_t n=1<<24; if(!shear_roots.empty()){ return; } shear_roots.resize((n/16-1)/3,std::array<point,3>{}); for(size_t len=n;len>=shear_min<n>;len/=4){ size_t count=n/len,base=(count-1)/3; point factor=roots[29-std::countr_zero(len)]; for(size_t k=0;k<count;k++){ point rt=factor*(k<pre_evals?evalp[k] :extra[k-pre_evals]); vpoint v1={vz+real(rt),vz+imag(rt)}; if(k&1){ v1= -vi(v1); } vpoint v2=v1*v1,v3=v1*v2; vftype t1=imag(v1)/(vz+1.0+real(v1)),t2=imag(v2)/(vz+1.0+real(v2)), t3=imag(v3)/(vz+1.0+real(v3)); shear_roots[base+k] ={point(imag(v1)[0],t1[0]),point(imag(v2)[0],t2[0]), point(imag(v3)[0],t3[0])}; } } } static big_vector<point>extra; static void prepare_roots(size_t n){ if(n<=pre_evals+extra.size()){ return; } size_t old=extra.size(); extra.resize(std::bit_ceil(n)-pre_evals); static const std::array<point,256>coarse= [](){ std::array<point,256>out; for(size_t i=0;i<256;i++){ out[i] =polar<ftype>(1.,std::numbers::pi*double((eval_args[256+i] -1)/2)/512.0); } return out; }(); for(size_t h=pre_evals+old;h<pre_evals+extra.size();h*=2){ for(size_t i=h;i<2*h;i+=256){ point fine=polar<ftype>(1.,std::numbers::pi/double(4*h)*double(eval_arg(4*i))); for(size_t j=0;j<256;j++){ extra[i+j-pre_evals] =coarse[j] *fine; } } } } }; big_vector<std::array<point,3>>cvector::shear_roots; big_vector<point>cvector::extra; const std::array<size_t,cvector::pre_evals>cvector::eval_args= [](){ std::array<size_t,pre_evals>res={}; for(size_t i=1;i<pre_evals;i++){ res[i] =res[i>>1] |(i&1)<<(std::bit_width(i)-1); } return res; }(); const std::array<point,cvector::pre_evals>cvector::evalp= [](){ std::array<point,pre_evals>res={}; res[0] =1; for(size_t n=1;n<pre_evals;n++){ res[n] =polar<ftype>(1.,std::numbers::pi*ftype(eval_args[n])/ftype(4*std::bit_floor(n))); } return res; }(); }
#pragma GCC pop_options
#line 12 "cp-algo/math/ring.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::fft{ size_t com_size(size_t as,size_t bs){ if(!as|| !bs){ return 0; } return std::max(flen,std::bit_ceil(as+bs-1)/2); } constexpr size_t short_tail=32; bool has_short_tail(size_t as,size_t bs){ size_t n=com_size(as,bs); return as+bs-1-n<=short_tail&&as<=n&&bs<=n; } constexpr uint64_t pow_mod(uint64_t x,uint64_t e,uint64_t p){ uint64_t res=1; for(x%=p;e;e>>=1,x=x*x%p){ if(e&1){ res=res*x%p; } } return res; } constexpr uint32_t least_negated_residue(uint64_t p){ for(uint32_t d=1;d<256&&d<p;d++){ if(pow_mod(p-d,(p-1)/2,p)==1){ return d; } } return 0; } template<modint_type base> struct quadratic{ static inline bool ready=false,available=false; static inline uint32_t d=0,root=0,prime=0; static inline int32_t a=0,b=0,a2=0,b2=0; static constexpr bool fixed_mod=requires{typename std::bool_constant<(base::mod(),true)>;}; static constexpr uint32_t fixed_d= []{ if constexpr(fixed_mod){ return base::mod()>2&&base::mod()%2?least_negated_residue(base::mod()):0u; }else{ return 0u; } }(); static void init(){ if(ready&&prime==uint32_t(base::mod())){ return; } ready=true; available=false; uint64_t p=base::mod(); prime=uint32_t(p); if(p<3||p>=(uint64_t(1)<<31)|| !is_prime(p)){ return; } d=least_negated_residue(p); auto s=d?cp_algo::math::sqrt(base(-int64_t(d))):std::nullopt; if(!s){ return; } root=s->getr(); using wide=__int128; wide x1=p,y1=0,x2= -wide(root),y2=1; auto norm= [&](wide x,wide y){ return x*x+d*y*y; }; while(true){ if(norm(x1,y1)>norm(x2,y2)){ std::swap(x1,x2); std::swap(y1,y2); } wide dot=x1*x2+d*y1*y2,len=norm(x1,y1); wide m=(2*dot+(dot>=0?len: -len))/(2*len); if(m==0||norm(x2-m*x1,y2-m*y1)>=norm(x2,y2)){ break; } x2-=m*x1; y2-=m*y1; } a=int32_t(x1); b=int32_t(y1); if(d==1){ a2=b; b2= -a; }else{ a2=int32_t(x2); b2=int32_t(y2); } assert(base(a)+base(b)*base(root)==base(0)&&base(a2)+base(b2)*base(root)==base(0)); assert(std::abs(int64_t(a)*b2-int64_t(a2)*b)==int64_t(p)); available=true; } static size_t length(size_t as,size_t bs){ return(d==1?com_size(as,bs):2*com_size(as,bs))>>has_short_tail(as,bs); } static bool usable(size_t as,size_t bs){ init(); if(!available||std::min(as,bs)<size_t(magic)){ return false; } size_t need=as+bs-1,n=length(as,bs); if(n>(1<<24)||(n==(1<<24)&&d==1&&std::max(as,bs)>n)){ return false; } using wide=unsigned __int128; return wide(need)*d*base::mod()*base::mod()<=wide(1)<<90; } struct lattice{ double a,b,a2,b2,to_q,to_t,c,e,inv_e,root,scale,inv_scale,p; lattice():a(quadratic::a),b(quadratic::b),a2(quadratic::a2),b2(quadratic::b2),p(base::mod()){ double det=a*b2-a2*b; to_q=b2/det; to_t= -b/det; bool first=std::abs(b)>=std::abs(b2); c=first?a:a2; e=first?b:b2; inv_e=1.0/e; root=quadratic::root; scale=std::sqrt(double(d)); inv_scale=1.0/scale; } }; template<bool unit> static u32x4 project(vpoint value,bool negative,lattice const&L){ const double p=L.p; auto R=round(real(value)),I=negative?-imag(value):imag(value); if constexpr(unit){ I=round(I); }else{ I=round(I*L.inv_scale); } auto q=round(I*L.inv_e); auto U=vftype(_mm256_fnmadd_pd(__m256d(q),_mm256_set1_pd(L.c),__m256d(R))); auto V=vftype(_mm256_fnmadd_pd(__m256d(q),_mm256_set1_pd(L.e),__m256d(I))); auto h=vftype(_mm256_fmadd_pd(__m256d(V),_mm256_set1_pd(L.root),__m256d(U))); q=round(h*(1.0/p)); auto out=vftype(_mm256_fnmadd_pd(__m256d(q),_mm256_set1_pd(p),__m256d(h))); out=out<0?out+p:out; return u32x4(_mm256_cvttpd_epi32(__m256d(out))); } template<bool stream,bool unit> static void fill(cvector&c,auto const&x,auto const&upper,size_t n,bool negative,u64x4 state, bool transform=true){ const lattice L; auto const*src=reinterpret_cast<const uint32_t*>(std::data(x)); size_t count=std::size(x); assert(count<=n&&(std::empty(upper)||(unit&& !stream&&count==n))); c.r.resize(n/flen); auto*dst=reinterpret_cast<double*>(c.r.data()); u32x8 words{}; auto emit= [&]<bool wrap=false> [[gnu::always_inline]](size_t i,i32x4 bits){ auto v=__builtin_convertvector(bits,vftype); if(i%8==0){ state^=state<<13; state^=state>>7; state^=state<<17; words=u32x8(state); } i32x4 small=i%8?i32x4(__builtin_shufflevector(words,words,1,3,5,7)) :i32x4(__builtin_shufflevector(words,words,0,2,4,6)); auto noise=__builtin_convertvector(small,vftype)*0x1p-32; auto q=round(v*L.to_q+noise),t=round(v*L.to_t+noise); auto re=v-q*L.a-t*L.a2,im= -(q*L.b)-t*L.b2; if constexpr(!unit){ im=im*L.scale; } if constexpr(stream){ _mm256_stream_pd(dst+2*i,__m256d(re)); _mm256_stream_pd(dst+2*i+flen,__m256d(negative?-im:im)); }else if constexpr(wrap){ c.r[(i-n)/flen] +=vpoint{negative?im: -im,re}; }else{ c.r[i/flen] =vpoint{re,negative?-im:im}; } }; size_t full=count/flen*flen; for(size_t i=0;i<full;i+=flen){ i32x4 bits; std::memcpy(&bits,src+i,sizeof(bits)); emit(i,bits); } if(full<count){ i32x4 bits{}; for(size_t j=full;j<count;j++){ bits[j-full] =int32_t(src[j]); } emit(full,bits); } if constexpr(stream){ _mm_sfence(); } std::fill(c.r.begin()+(count+flen-1)/flen,c.r.end(),vpoint{}); auto const*wrapped=reinterpret_cast<const uint32_t*>(std::data(upper)); for(size_t i=0;i<std::size(upper);i+=flen){ i32x4 bits{}; for(size_t j=i;j<std::min(i+flen,std::size(upper));j++){ bits[j-i] =int32_t(wrapped[j]); } emit.template operator()<true>(n+i,bits); } checkpoint("quadratic init"); if constexpr(!stream){ if(transform){ c.forward(); } } } template<bool unit> static void lift(cvector&c,auto const&x,size_t n,bool negative,u64x4 state){ const lattice L; size_t total=std::size(x),count=std::min(n,total); assert(total<=2*n&&(d==1||total<=n)); c.r.resize(n/flen); u32x8 words{}; size_t step=0; auto emit= [&]<bool wrap> [[gnu::always_inline]](size_t i,i32x4 bits){ auto v=__builtin_convertvector(bits,vftype); if(step%2==0){ state^=state<<13; state^=state>>7; state^=state<<17; words=u32x8(state); } i32x4 small=step++ %2?i32x4(__builtin_shufflevector(words,words,1,3,5,7)) :i32x4(__builtin_shufflevector(words,words,0,2,4,6)); auto noise=__builtin_convertvector(small,vftype)*0x1p-32; auto q=round(v*L.to_q+noise),t=round(v*L.to_t+noise); auto re=v-q*L.a-t*L.a2,im= -(q*L.b)-t*L.b2; if constexpr(!unit){ im=im*L.scale; } if constexpr(wrap){ c.r[(i-n)/flen] +=vpoint{negative?im: -im,re}; }else{ c.r[i/flen] =vpoint{re,negative?-im:im}; } }; constexpr bool plain=fixed_mod&&std::ranges::contiguous_range<std::decay_t<decltype(x)>>; constexpr bool raw=plain&&sizeof(base)==4; constexpr bool wide=plain&&sizeof(base)==8; auto load= [&](size_t i,size_t upto){ i32x4 bits{}; if constexpr(raw||wide){ if(i+flen<=upto){ if constexpr(raw){ std::memcpy(&bits,reinterpret_cast<const uint32_t*>(std::data(x))+i,sizeof(bits)); }else{ u64x4 words; std::memcpy(&words,reinterpret_cast<const uint64_t*>(std::data(x))+i,sizeof(words)); bits=__builtin_convertvector(words,i32x4); } return bits; } } for(size_t j=i;j<std::min(i+flen,upto);j++){ bits[j-i] =int32_t(x[j].getr()); } return bits; }; for(size_t i=0;i<count;i+=flen){ emit.template operator()<false>(i,load(i,count)); } std::fill(c.r.begin()+(count+flen-1)/flen,c.r.end(),vpoint{}); for(size_t i=n;i<total;i+=flen){ emit.template operator()<true>(i,load(i,total)); } checkpoint("quadratic init"); } static void recover(std::array<cvector,2>const&parts,size_t n,double factor,auto&out,size_t k){ constexpr bool plain=fixed_mod&&std::ranges::contiguous_range<std::decay_t<decltype(out)>>; constexpr bool raw=plain&&sizeof(base)==4; constexpr bool wide=plain&&sizeof(base)==8; const lattice L; auto scale=vz+factor; size_t low=std::min(k,n); auto project_at= [&](cvector const&part,size_t i,bool negative){ if(d==1){ return project<true>(part.at(i)*scale,negative,L); }else{ return project<false>(part.at(i)*scale,negative,L); } }; auto project8= [&](cvector const&part,size_t i,bool negative,size_t count){ auto lo8=project_at(part,i,negative); auto hi8=count>flen?project_at(part,i+flen,negative):u32x4{}; return __builtin_shufflevector(lo8,hi8,0,1,2,3,4,5,6,7); }; auto store8= [&](size_t idx,u32x8 v,size_t count){ if constexpr(raw){ if(count==8){ std::memcpy(reinterpret_cast<uint32_t*>(std::data(out))+idx,&v,sizeof(v)); return; } }else if constexpr(wide){ if(count==8){ auto words=reinterpret_cast<uint64_t*>(std::data(out))+idx; auto lo4=__builtin_convertvector(u32x4{v[0],v[1],v[2],v[3]},u64x4); auto hi4=__builtin_convertvector(u32x4{v[4],v[5],v[6],v[7]},u64x4); std::memcpy(words,&lo4,sizeof(lo4)); std::memcpy(words+flen,&hi4,sizeof(hi4)); return; } } for(size_t l=0;l<count;l++){ out[idx+l].setr(typename base::UInt(v[l])); } }; auto load8= [&](size_t idx,size_t count){ u32x8 v{}; if constexpr(raw){ if(count==8){ std::memcpy(&v,reinterpret_cast<uint32_t const*>(std::data(out))+idx,sizeof(v)); return v; } }else if constexpr(wide){ if(count==8){ auto words=reinterpret_cast<uint64_t const*>(std::data(out))+idx; u64x4 lo4,hi4; std::memcpy(&lo4,words,sizeof(lo4)); std::memcpy(&hi4,words+flen,sizeof(hi4)); auto lo=__builtin_convertvector(lo4,u32x4),hi=__builtin_convertvector(hi4,u32x4); return u32x8(__builtin_shufflevector(lo,hi,0,1,2,3,4,5,6,7)); } } for(size_t l=0;l<count;l++){ v[l] =uint32_t(out[idx+l].getr()); } return v; }; for(size_t i=0;i<low;i+=8){ store8(i,project8(parts[0],i,false,std::min<size_t>(8,low-i)),std::min<size_t>(8,low-i)); } if(d>1){ checkpoint("quadratic recover"); return; } const uint32_t mod32=uint32_t(base::mod()),imod32= -inv2<uint32_t>(base::mod()); auto highmul=u32x8{}+uint32_t(((base(2)*base(root)).inv()*bpow(base(2),32)).getr()); for(size_t i=0;i<low;i+=8){ size_t count=std::min<size_t>(8,low-i); auto minus=project8(parts[1],i,true,count); auto plus=load8(i,count); auto lo8=reduce_once(plus+minus,mod32); lo8=(lo8+(lo8&1)*mod32)>>1; auto hi8=reduce_once(montgomery_mul(plus+mod32-minus,highmul,mod32,imod32),mod32); store8(i,lo8,count); if(n+i<k){ store8(n+i,hi8,std::min<size_t>(count,k-n-i)); } } checkpoint("quadratic recover"); } static void cyclic(auto&a,auto const&b,size_t k){ init(); assert(available&&std::popcount(k)==1&&std::size(a)==k&&std::size(b)==k); bool square=(void const*)std::data(a)==(void const*)std::data(b); size_t groups=k/flen; size_t fine_bits=std::min<size_t>(8,std::countr_zero(std::max<size_t>(groups,1))); size_t fine=size_t(1)<<fine_bits; big_vector<point>low(fine),high((groups+fine-1)>>fine_bits),step(groups); auto w= [&](size_t j){ return polar<ftype>(1.,-std::numbers::pi*ftype(j)/ftype(2*k)); }; for(size_t t=0;t<low.size();t++){ low[t] =w(flen*t); } for(size_t c=0;c<high.size();c++){ high[c] =w(flen*(c<<fine_bits)); } for(size_t c=0;c<groups;c++){ step[c] =high[c>>fine_bits] *low[c&(fine-1)]; } vpoint quarter,quarter_conj; for(size_t l=0;l<flen;l++){ point v=w(l); real(quarter)[l] =real(v); imag(quarter)[l] =imag(v); real(quarter_conj)[l] =real(v); imag(quarter_conj)[l] = -imag(v); } auto twiddle= [&](size_t j,bool inverse,ftype scale){ point t=step[j/flen]; vpoint head={vz+real(t)*scale,vz+(inverse?-imag(t):imag(t))*scale}; return head*(inverse?quarter_conj:quarter); }; auto run= [&]<bool unit>(){ const lattice L; cvector A(k),B(0); lift<unit>(A,a,k,false,seed()); if(!square){ lift<unit>(B,b,k,false,seed()); } for(size_t j=0;j<k;j+=flen){ auto w=twiddle(j,false,1); A.at(j)*=w; if(!square){ B.at(j)*=w; } } A.forward(); if(!square){ B.forward(); } A.multiply(square?A:B); for(size_t j=0;j<k;j+=flen){ auto v=project<unit>(A.at(j)*twiddle(j,true,ftype(flen)/ftype(k)),false,L); for(size_t l=0;l<flen;l++){ a[j+l].setr(typename base::UInt(v[l])); } } }; if(d==1){ run.template operator()<true>(); }else{ run.template operator()<false>(); } checkpoint("quadratic recover"); } static u64x4 seed(){ return u64x4{random::rng()|1,random::rng()|1,random::rng()|1,random::rng()|1}; } static void mul_branches(auto&a,auto const&b,bool square){ size_t as=std::size(a),bs=square?as:std::size(b),need=as+bs-1; size_t n=length(as,bs); assert(available&&d==1); std::array<uint32_t,short_tail>high{}; size_t tail=need>2*n?need-2*n:0; for(size_t i=0;i<tail;i++){ auto const*x=reinterpret_cast<const uint32_t*>(std::data(a)); auto const*y=square?x:reinterpret_cast<const uint32_t*>(std::data(b)); uint64_t sum=0; for(size_t j=2*n+i-bs+1;j<as;j++){ sum=(sum+uint64_t(x[j])*y[2*n+i-j])%prime; } high[i] =uint32_t(sum); } const uint32_t mod32=uint32_t(base::mod()),imod32= -inv2<uint32_t>(base::mod()); base r32=bpow(base(2),32); auto highmul=u32x8{}+uint32_t(((base(2)*base(root)).inv()*r32).getr()); u64x4 seed_a=seed(),seed_b=seed(); const lattice L; big_vector<base>a_upper(std::begin(a)+std::min(as,n),std::end(a)); a.resize(2*n); std::span<base const>a_lower=std::span(a).first(std::min(as,n)); auto b_lower=square?a_lower:std::span<base const>(b).first(std::min(bs,n)); auto b_upper=square?std::span<base const>(a_upper):std::span<base const>(b).subspan(b_lower.size()); cvector A(0),B(0); auto*out=reinterpret_cast<uint32_t*>(std::data(a)); for(bool negative:{false,true}){ if(n==(1<<24)){ if constexpr(cvector::fuse_forward){ cvector::fuse_args fa{ out,as,seed_a[0],L.a,L.b,L.a/double(base::mod()),L.b/double(base::mod())}; cvector::fuse_args fb{reinterpret_cast<const uint32_t*>(std::data(b)), bs, seed_b[0], fa.a, fa.b, fa.a_over_p, fa.b_over_p}; if(negative){ A.template cache_product<true>(B,fa,fb); }else{ A.template cache_product<false>(B,fa,fb); } }else{ fill<true,true>(A,a_lower,a_upper,n,negative,seed_a); if(square&&__uint128_t(need)*d*prime*prime<=(__uint128_t(1)<<88)){ if(negative){ A.template cache_product<true,false,true>(A); }else{ A.template cache_product<false,false,true>(A); } }else{ fill<true,true>(B,b_lower,b_upper,n,negative,seed_b); if(negative){ A.template cache_product<true>(B); }else{ A.template cache_product<false>(B); } } } }else{ fill<false,true>(A,a_lower,a_upper,n,negative,seed_a); if(!square){ fill<false,true>(B,b_lower,b_upper,n,negative,seed_b); } A.multiply(square?A:B); } auto scale=vz+double(flen)/double(n); for(size_t i=0;i<n;i+=8){ auto sum0=project<true>(A.at(i)*scale,negative,L); auto sum1=project<true>(A.at(i+4)*scale,negative,L); auto sum=__builtin_shufflevector(sum0,sum1,0,1,2,3,4,5,6,7); if(negative){ u32x8 plus; std::memcpy(&plus,out+n+i,sizeof(plus)); auto lo=plus+sum; lo=reduce_once(lo,mod32); lo=(lo+(lo&1)*base::mod())>>1; auto hi=reduce_once(montgomery_mul(plus+base::mod()-sum,highmul,mod32,imod32),mod32); std::memcpy(out+i,&lo,sizeof(lo)); std::memcpy(out+n+i,&hi,sizeof(hi)); }else{ std::memcpy(out+n+i,&sum,sizeof(sum)); } } checkpoint("quadratic recover"); } a.resize(need); out=reinterpret_cast<uint32_t*>(std::data(a)); for(size_t i=0;i<tail;i++){ uint32_t sum=out[i] +high[i]; out[i] =std::min(sum,sum-prime); out[2*n+i] =high[i]; } } static void mul_single(auto&a,auto const&b,bool square){ size_t as=std::size(a),bs=square?as:std::size(b),need=as+bs-1; size_t n=length(as,bs),tail=need>n?need-n:0; assert(available&&d>1); const lattice L; cvector A(0),B(0); std::span<base const>none; std::array<point,short_tail>high{}; auto wrapped= [&](cvector const&rhs){ for(size_t i=0;i<tail;i++){ for(size_t j=n+i-bs+1;j<as;j++){ high[i] +=A.template get<point>(j)*rhs.template get<point>(n+i-j); } } }; if(n==(1<<24)){ fill<true,false>(A,a,none,n,false,seed()); if(square&&__uint128_t(need)*d*prime*prime<=(__uint128_t(1)<<88)){ wrapped(A); A.template cache_product<false,false,true>(A); }else{ if(square){ fill<true,false>(B,a,none,n,false,seed()); }else{ fill<true,false>(B,b,none,n,false,seed()); } wrapped(B); A.template cache_product<false>(B); } }else{ fill<false,false>(A,a,none,n,false,seed(),!tail); if(!square){ fill<false,false>(B,b,none,n,false,seed(),!tail); } if(tail){ wrapped(square?A:B); A.forward(); if(!square){ B.forward(); } } A.multiply(square?A:B); } for(size_t i=0;i<tail;i++){ A.set(i,A.template get<point>(i)-point(0,1)*high[i] *(double(n)/double(flen))); } a.resize(n); auto*out=reinterpret_cast<uint32_t*>(std::data(a)); auto scale=vz+double(flen)/double(n); for(size_t i=0;i<n;i+=flen){ auto sum=project<false>(A.at(i)*scale,false,L); std::memcpy(out+i,&sum,sizeof(sum)); } checkpoint("quadratic recover"); a.resize(need); out=reinterpret_cast<uint32_t*>(std::data(a)); for(size_t i=0;i<tail;i+=flen){ vpoint lanes={vz,vz}; for(size_t j=i;j<std::min(tail,i+flen);j++){ real(lanes)[j-i] =real(high[j]); imag(lanes)[j-i] =imag(high[j]); } auto sum=project<false>(lanes,false,L); for(size_t j=i;j<std::min(tail,i+flen);j++){ out[n+j] =sum[j-i]; } } } static void mul(auto&a,auto const&b,bool square){ static_assert(sizeof(std::decay_t<decltype(a[0])>)==4); auto run= [&](auto const&rhs){ if constexpr(fixed_d==1){ mul_branches(a,rhs,square); }else if constexpr(fixed_d>1){ mul_single(a,rhs,square); }else if(d==1){ mul_branches(a,rhs,square); }else{ mul_single(a,rhs,square); } }; if constexpr(fixed_mod){ run(b); }else{ big_vector<base>plain; if(!square){ plain.assign(std::begin(b),std::end(b)); } for(auto&x:plain){ x.setr_direct(x.getr()); } for(auto&x:a){ x.setr_direct(x.getr()); } run(plain); for(auto&x:a){ x.setr(x.getr_direct()); } } } }; template<modint_type base,bool complete=false> struct spectrum{ using ring=quadratic<base>; spectrum(auto const&a,size_t capacity):cap(std::max(2*flen,std::bit_ceil(capacity))){ ring::init(); assert(ring::available&&std::size(a)<=cap); auto state=ring::seed(); for(size_t j=0;j<branches();j++){ if(ring::d==1){ ring::template lift<true>(parts[j],a,points(),j==1,state); }else{ ring::template lift<false>(parts[j],a,points(),false,state); } if constexpr(complete){ parts[j].template fft<false>(); }else{ parts[j].forward(); } } } spectrum(spectrum&&)=default; spectrum&operator=(spectrum&&)=default; spectrum clone()const{return*this;} size_t capacity()const{return cap;} void multiply(spectrum const&other,auto&out,size_t k)&&requires(!complete){ assert(other.cap==cap&&k<=cap); for(size_t j=0;j<branches();j++){ parts[j].multiply(other.parts[j]); } ring::recover(parts,points(),double(flen)/double(points()),out,k); } void square(auto&out,size_t k)&&requires(!complete){ std::move(*this).multiply(*this,out,k); } private: spectrum(spectrum const&)=default; size_t branches()const{return ring::d==1?2:1;} size_t points()const{return ring::d==1?cap/2:cap;} template<modint_type> friend struct product; std::array<cvector,2>parts={cvector(0),cvector(0)}; size_t cap; }; template<modint_type base> struct product{ using ring=quadratic<base>; using operand=spectrum<base,true>; explicit product(size_t capacity):cap(std::max(2*flen,std::bit_ceil(capacity))){ ring::init(); assert(ring::available); for(size_t j=0;j<branches();j++){ acc[j] =cvector(points()); } } void add(operand const&x,operand const&y){ assert(x.cap==cap&&y.cap==cap); for(size_t j=0;j<branches();j++){ for(size_t i=0;i<points();i+=flen){ acc[j].at(i)+=x.parts[j].at(i)*y.parts[j].at(i); } } checkpoint("dot"); } void recover(auto&out,size_t k)&&{ assert(k<=cap); for(size_t j=0;j<branches();j++){ acc[j].template ifft<false>(); } ring::recover(acc,points(),1.0,out,k); } private: size_t branches()const{return ring::d==1?2:1;} size_t points()const{return ring::d==1?cap/2:cap;} std::array<cvector,2>acc={cvector(0),cvector(0)}; size_t cap; }; }
#pragma GCC pop_options
#line 6 "cp-algo/math/fft.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::fft{ void mul_slow(auto&a,auto const&b,size_t k){ using base=std::decay_t<decltype(a[0])>; if(!std::empty(a)&&std::data(a)==std::data(b)){ size_t n=std::min(k,std::size(a)),m=std::min(k,std::size(b)); if(!m){ a.clear(); return; } a.resize(k); for(size_t j=k;j-- >0;){ base sum=0; size_t lo=j>=n?j+1-n:0,hi=std::min(j+1,m); for(size_t i=lo;i<hi;i++){ if(n==m&&i>j-i){ break; } auto term=a[i] *a[j-i]; sum+=n==m&&i!=j-i?term+term:term; } a[j] =sum; } return; } if(std::empty(a)||std::empty(b)){ a.clear(); }else{ size_t n=std::min(k,std::size(a)); size_t m=std::min(k,std::size(b)); size_t had=std::size(a); a.resize(k); if(k>had){ std::fill(std::begin(a)+had,std::end(a),base(0)); } for(int j=int(k-1);j>=0;j--){ a[j] *=b[0]; for(int i=std::max(j-(int)n,0)+1;i<std::min(j+1,(int)m);i++){ a[j] +=a[j-i] *b[i]; } } } } bool same_storage(auto const&a,auto const&b){ if constexpr(std::ranges::contiguous_range<decltype(b)>){ return!std::empty(a)&&(void const*)std::data(a)==(void const*)std::data(b); }else{ return false; } } void mul_truncate(auto&a,auto const&b,size_t k){ using base=std::decay_t<decltype(a[0])>; if(std::min({k,std::size(a),std::size(b)})<magic){ mul_slow(a,b,k); return; } size_t as=std::min(k,std::size(a)),bs=std::min(k,std::size(b)); assert(quadratic<base>::usable(as,bs)&& "the ring needs an odd prime modulus below 2^31 and a product within its rounding bound"); bool aliased=same_storage(a,b),square=aliased&&as==bs; size_t need=as+bs-1,keep=std::min(k,need); auto pad= [&]{ if(k>need){ std::fill(std::begin(a)+need,std::end(a),base(0)); } }; if constexpr(sizeof(base)==4&&std::ranges::contiguous_range<decltype(b)>){ if(aliased&& !square){ big_vector<base>copy(std::data(b),std::data(b)+bs); a.resize(as); quadratic<base>::mul(a,copy,false); }else{ auto prefix=std::span<base const>(std::data(b),bs); a.resize(as); quadratic<base>::mul(a,prefix,square); } a.resize(k); pad(); }else{ size_t cap=std::bit_ceil(need); auto A=spectrum<base>(a|std::views::take(as),cap); a.resize(k); if(square){ std::move(A).square(a,keep); }else{ std::move(A).multiply(spectrum<base>(b|std::views::take(bs),cap),a,keep); } pad(); } } void cyclic_mul(auto&a,auto const&b,size_t k){ using base=std::decay_t<decltype(a[0])>; quadratic<base>::cyclic(a,b,k); } namespace impl{ void mul_unbalanced(auto&a,auto const&b){ using base=std::decay_t<decltype(a[0])>; auto x=std::span<base const>(a),y=std::span<base const>(b); if(x.size()<y.size()){ std::swap(x,y); } constexpr size_t length=1<<15; size_t step=length-y.size()+1; auto fixed=spectrum<base>(y,length); std::decay_t<decltype(a)>result; result.assign(x.size()+y.size()-1,base(0)); big_vector<base>work(length); for(size_t start=0;start<x.size();start+=step){ size_t count=std::min(step,x.size()-start); size_t need=count+y.size()-1; spectrum<base>(x.subspan(start,count),length).multiply(fixed,work,need); for(size_t i=0;i<need;i++){ result[start+i] +=work[i]; } } a=std::move(result); } } void mul(auto&a,auto const&b){ if(std::empty(a)||std::empty(b)){ a.clear(); return; } size_t small=std::min(std::size(a),std::size(b)); size_t large=std::max(std::size(a),std::size(b)); if(small>=magic&&small<=4096&&large>=(1<<20)&&large/small>=64){ return impl::mul_unbalanced(a,b); } mul_truncate(a,b,std::size(a)+std::size(b)-1); } }
#pragma GCC pop_options
#line 4 "cp-algo/math/poly/base.hpp"
#include <array>
#line 7 "cp-algo/math/poly/base.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T>struct poly_t; template<typename T> std::array<poly_t<T>,2>divmod(poly_t<T>p,poly_t<T>const&q); template<typename T> struct poly_t{ using Vector=big_vector<T>; using base=T; Vector a; poly_t&normalize(){ while(deg()>=0&&lead()==base(0)){ a.pop_back(); } return*this; } poly_t()=default; poly_t(T a0):a{a0}{normalize();} poly_t(Vector const&t):a(t){normalize();} poly_t(Vector&&t):a(std::move(t)){normalize();} poly_t&negate_inplace(){ std::ranges::transform(a,begin(a),std::negate{}); return*this; } friend poly_t operator-(poly_t p){p.negate_inplace();return p;} poly_t&operator+=(poly_t const&t){ a.resize(std::max(size(a),size(t.a))); std::ranges::transform(a,t.a,begin(a),std::plus{}); return normalize(); } poly_t&operator-=(poly_t const&t){ a.resize(std::max(size(a),size(t.a))); std::ranges::transform(a,t.a,begin(a),std::minus{}); return normalize(); } friend poly_t operator+(poly_t p,poly_t const&t){p+=t;return p;} friend poly_t operator-(poly_t p,poly_t const&t){p-=t;return p;} poly_t&mod_xk_inplace(size_t k){ a.resize(std::min(size(a),k)); return normalize(); } poly_t&mul_xk_inplace(size_t k){ if(is_zero()){return*this;} a.insert(begin(a),k,T(0)); return normalize(); } poly_t&div_xk_inplace(int64_t k){ if(k<0){ return mul_xk_inplace(-k); } a.erase(begin(a),begin(a)+std::min<size_t>(k,size(a))); return normalize(); } poly_t&substr_inplace(size_t l,size_t k){ return mod_xk_inplace(l+k).div_xk_inplace(l); } poly_t mod_xk(size_t k)const&{return substr(0,k);} poly_t mod_xk(size_t k)&&{mod_xk_inplace(k);return std::move(*this);} poly_t mul_xk(size_t k)const&{auto p= *this;p.mul_xk_inplace(k);return p;} poly_t mul_xk(size_t k)&&{mul_xk_inplace(k);return std::move(*this);} poly_t div_xk(int64_t k)const&{return k<0?mul_xk(-k):substr(k,a.size());} poly_t div_xk(int64_t k)&&{div_xk_inplace(k);return std::move(*this);} poly_t substr(size_t l,size_t k)const&{ l=std::min(l,a.size()); k=std::min(k,a.size()-l); return Vector(begin(a)+l,begin(a)+l+k); } poly_t substr(size_t l,size_t k)&&{substr_inplace(l,k);return std::move(*this);} poly_t&operator*=(const poly_t&t){fft::mul(a,t.a);normalize();return*this;} friend poly_t operator*(poly_t p,const poly_t&t){p*=t;return p;} poly_t&operator/=(const poly_t&t){ assert(!t.is_zero()); if(this== &t){return*this=T(1);} auto[q,r] =divmod(std::move(*this),t); return*this=std::move(q); } poly_t&operator%=(const poly_t&t){ assert(!t.is_zero()); if(this== &t){a.clear();return*this;} auto[q,r] =divmod(std::move(*this),t); return*this=std::move(r); } friend poly_t operator/(poly_t p,poly_t const&t){p/=t;return p;} friend poly_t operator%(poly_t p,poly_t const&t){p%=t;return p;} poly_t&operator*=(T const&x){ for(auto&it:a){ it*=x; } return normalize(); } poly_t&operator/=(T const&x){return*this*=x.inv();} friend poly_t operator*(poly_t p,T const&x){p*=x;return p;} friend poly_t operator/(poly_t p,T const&x){p/=x;return p;} poly_t&reverse(size_t n){ a.resize(n); std::ranges::reverse(a); return normalize(); } poly_t&reverse(){return reverse(size(a));} poly_t reversed(size_t n)const&{auto p= *this;p.reverse(n);return p;} poly_t reversed(size_t n)&&{reverse(n);return std::move(*this);} poly_t reversed()const&{return reversed(a.size());} poly_t reversed()&&{reverse();return std::move(*this);} friend poly_t operator*(T const&x,poly_t p){p*=x;return p;} poly_t negx()const{ auto res= *this; for(int i=1;i<=deg();i+=2){ res.a[i] = -res[i]; } return res; } void print(int n)const{ for(int i=0;i<n;i++){ std::cout<<(*this)[i] <<' '; } std::cout<<"\n"; } void print()const{ print(deg()+1); } T eval(T x)const{ T res(0); for(int i=deg();i>=0;i--){ res*=x; res+=a[i]; } return res; } T lead()const{ assert(!is_zero()); return a.back(); } int deg()const{ return(int)a.size()-1; } bool is_zero()const{ return a.empty(); } T operator[](int idx)const{ return idx<0||idx>deg()?T(0):a[idx]; } T&coef(size_t idx){ return a[idx]; } bool operator==(const poly_t&t)const=default; size_t trailing_xk()const{ if(is_zero()){ return-1; } int res=0; while(a[res] ==T(0)){ res++; } return res; } poly_t&mul_truncate(poly_t const&t,size_t k){ fft::mul_truncate(a,t.a,k); return normalize(); } static poly_t xk(size_t n){ return poly_t(T(1)).mul_xk(n); } static poly_t ones(size_t n){ return Vector(n,1); } poly_t x2()const{ Vector res(2*a.size()); for(size_t i=0;i<a.size();i++){ res[2*i] =a[i]; } return res; } std::array<poly_t,2>bisect(size_t n)const{ n=std::min(n,size(a)); Vector res[2]; for(size_t i=0;i<n;i++){ res[i%2].push_back(a[i]); } return{std::move(res[0]),std::move(res[1])}; } std::array<poly_t,2>bisect()const{ return bisect(size(a)); } }; }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/div.hpp"
#line 1 "cp-algo/math/poly/impl/div.hpp"
#line 1 "cp-algo/math/poly/series/inv.hpp"
#line 4 "cp-algo/math/poly/series/inv.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::poly::impl{ template<typename poly> poly&inv_inplace(poly&p,size_t n){ using base=poly::base; if(n==0){ p.a.clear(); return p; } assert(p[0] !=base(0)); if(n<magic){ typename poly::Vector q(n); q[0] =base(1)/p[0]; for(size_t i=1;i<n;i++){ for(size_t j=1;j<=std::min(i,p.a.size()-1);j++){ q[i] -=p.a[j] *q[i-j]; } q[i] *=q[0]; } return p=std::move(q); } size_t m=std::bit_floor(size_t(magic-1)); auto q=p.mod_xk(m); inv_inplace(q,m); for(;m<n;m*=2){ size_t k=std::min(2*m,n); typename poly::Vector error((k+fft::flen-1)/fft::flen*fft::flen); auto Q=fft::spectrum<base>(q.a,2*m); fft::spectrum<base>(p.a|std::views::take(k),2*m).multiply(Q,error,k); auto E=fft::spectrum<base>(error|std::views::drop(m)|std::views::take(k-m),2*m); std::move(Q).multiply(E,error,k-m); q.a.resize(k); for(size_t i=m;i<k;i++){ q.a[i] = -error[i-m]; } } p=std::move(q); p.normalize(); return p; } } namespace cp_algo::math{ template<typename T> poly_t<T>inv(poly_t<T>p,size_t n){ poly::impl::inv_inplace(p,n); return p; } }
#pragma GCC pop_options
#line 4 "cp-algo/math/poly/impl/div.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::poly::impl{ template<typename T> std::array<poly_t<T>,2>divmod_slow(poly_t<T>p,poly_t<T>const&q){ poly_t<T>d; auto qi=q.lead()==T(1)?T(1):q.lead().inv(); while(p.deg()>=q.deg()){ d.a.push_back(p.lead()*qi); if(d.lead()!=T(0)){ for(size_t i=1;i<=q.a.size();i++){ p.a[p.a.size()-i] -=d.lead()*q.a[q.a.size()-i]; } } p.a.pop_back(); } std::ranges::reverse(d.a); p.normalize(); return{std::move(d),std::move(p)}; } template<typename T> std::array<poly_t<T>,2>divmod_hint(poly_t<T>p,poly_t<T>const&q,poly_t<T>const&qri){ assert(!q.is_zero()); int n=p.deg()-q.deg(); if(std::min(n,q.deg())<magic){ return divmod_slow(std::move(p),q); } poly_t<T>d(typename poly_t<T>::Vector(p.a.rbegin(),p.a.rbegin()+n+1)); d.mul_truncate(qri,n+1).reverse(n+1); auto low=d.mod_xk(q.deg()); low.mul_truncate(q,q.deg()); p.mod_xk_inplace(q.deg()); p-=low; return{std::move(d),std::move(p)}; } }
#pragma GCC pop_options
#line 4 "cp-algo/math/poly/div.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> std::array<poly_t<T>,2>divmod(poly_t<T>p,poly_t<T>const&q){ assert(!q.is_zero()); int n=p.deg()-q.deg(); if(std::min(n,q.deg())<magic){ return poly::impl::divmod_slow(std::move(p),q); } auto qi=inv(q.reversed(),n+1); return poly::impl::divmod_hint(std::move(p),q,qi); } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/series.hpp"
#line 1 "cp-algo/math/poly/series/log.hpp"
#line 1 "cp-algo/math/poly/calculus.hpp"
#line 1 "cp-algo/math/factorials.hpp"
#line 1 "cp-algo/util/bump_alloc.hpp"
#line 5 "cp-algo/util/bump_alloc.hpp"
namespace cp_algo{ template<class T,size_t max_len> struct bump_alloc{ static inline char*buf=big_alloc<char>().allocate(max_len*sizeof(T)); static inline size_t buf_ind=max_len*sizeof(T); using value_type=T; template<class U>struct rebind{using other=bump_alloc<U,max_len>;}; constexpr bool operator==(const bump_alloc&)const=default; bump_alloc()=default; template<class U>bump_alloc(const U&){} T*allocate(size_t n){ buf_ind-=n*sizeof(T); buf_ind&=0-alignof(T); return(T*)(buf+buf_ind); } void deallocate(T*,size_t){} }; }
#line 1 "cp-algo/math/combinatorics.hpp"
#line 7 "cp-algo/math/combinatorics.hpp"
namespace cp_algo::math{ template<typename T> T fact(auto n){ static const auto F= []{ big_vector<T>F(maxn); F[0] =T(1); for(int i=1;i<maxn;i++){ F[i] =F[i-1] *T(i); } return F; }(); return F[n]; } template<typename T> T rfact(auto n){ static const auto F= []{ big_vector<T>F(maxn); int t=(int)std::min<int64_t>(T::mod(),maxn)-1; F[t] =T(1)/fact<T>(t); for(int i=t-1;i>=0;i--){ F[i] =F[i+1] *T(i+1); } return F; }(); return F[n]; } template<typename T,int base> T pow_fixed(int n){ static const auto prec= []{ big_vector<T>prec_low(1<<16),prec_high(1<<16); prec_low[0] =prec_high[0] =T(1); T step_low=T(base); T step_high=bpow(T(base),1<<16); for(int i=1;i<(1<<16);i++){ prec_low[i] =prec_low[i-1] *step_low; prec_high[i] =prec_high[i-1] *step_high; } return std::pair{std::move(prec_low),std::move(prec_high)}; }(); return prec.first[n&0xFFFF] *prec.second[n>>16]; } template<typename T> big_vector<T>bulk_invs(auto const&args){ big_vector<T>res(std::size(args),args[0]); for(size_t i=1;i<std::size(args);i++){ res[i] =res[i-1] *args[i]; } auto all_invs=T(1)/res.back(); for(size_t i=std::size(args)-1;i>0;i--){ res[i] =all_invs*res[i-1]; all_invs*=args[i]; } res[0] =all_invs; return res; } template<typename T> T small_inv(auto n){ static auto F=bulk_invs<T>(std::views::iota(1,maxn)); return F[n-1]; } template<typename T> T binom_large(T n,auto r){ assert(r<maxn); T ans=1; for(decltype(r)i=0;i<r;i++){ ans=ans*T(n-i)*small_inv<T>(i+1); } return ans; } template<typename T> T binom(auto n,auto r){ if(r<0||r>n){ return T(0); }else if(n>=maxn){ return binom_large(T(n),r); }else{ return fact<T>(n)*rfact<T>(r)*rfact<T>(n-r); } } }
#line 9 "cp-algo/math/factorials.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<bool use_bump_alloc=false,int maxn= -1> auto facts(auto const&args){ static_assert(!use_bump_alloc||maxn>0,"maxn must be set if use_bump_alloc is true"); constexpr int max_mod=1'000'000'000; constexpr int accum=4; constexpr int simd_size=8; constexpr int block=1<<18; constexpr int subblock=block/simd_size; using base=std::decay_t<decltype(args[0])>; static_assert(modint_type<base>,"Base type must be a modint type"); using T=std::array<int,2>; using alloc=std::conditional_t<use_bump_alloc, bump_alloc<T,30*maxn>, big_alloc<T>>; std::basic_string<T,std::char_traits<T>,alloc>odd_args_per_block[max_mod/subblock]; std::basic_string<T,std::char_traits<T>,alloc>reg_args_per_block[max_mod/subblock]; constexpr int limit_reg=max_mod/64; int limit_odd=0; big_vector<base>res(size(args),1); const int mod=base::mod(); const int imod= -math::inv2(mod); for(auto[i,xy]:std::views::zip(args,res)|std::views::enumerate){ auto[x,y] =xy; int t=x.getr(); if(t>=mod/2){ t=mod-t-1; y=t%2?1:mod-1; } auto pw=32ull*(t+1); while(t>limit_reg){ limit_odd=std::max(limit_odd,(t-1)/2); odd_args_per_block[(t-1)/2/subblock].push_back({int(i),(t-1)/2}); t/=2; pw+=t; } reg_args_per_block[t/subblock].push_back({int(i),t}); y*=pow_fixed<base,2>(int(pw%(mod-1))); } checkpoint("init"); base bi2x32=pow_fixed<base,2>(32).inv(); auto process= [&](int limit,auto&args_per_block,auto step,auto&&proj){ base fact=1; for(int b=0;b<=limit;b+=accum*block){ u32x8 cur[accum]; static std::array<u32x8,subblock>prods[accum]; for(int z=0;z<accum;z++){ for(int j=0;j<simd_size;j++){
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
cur[z][j] =uint32_t(b+z*block+j*subblock); cur[z][j] =proj(cur[z][j]); prods[z][0][j] =cur[z][j] + !cur[z][j]; prods[z][0][j] =uint32_t(uint64_t(prods[z][0][j])*bi2x32.getr()%mod);
#pragma GCC diagnostic pop
} } for(int i=1;i<block/simd_size;i++){ for(int z=0;z<accum;z++){ cur[z] +=step; prods[z][i] =montgomery_mul(prods[z][i-1],cur[z],mod,imod); } } checkpoint("inner loop"); for(int z=0;z<accum;z++){ for(int j=0;j<simd_size;j++){ int bl=b+z*block+j*subblock; for(auto[i,x]:args_per_block[bl/subblock]){ res[i] *=fact*prods[z][x-bl][j]; } fact*=base(prods[z].back()[j]); } } checkpoint("mul ans"); } }; process(limit_reg,reg_args_per_block,1,std::identity{}); process(limit_odd,odd_args_per_block,2,[](uint32_t x){return 2*x+1;}); auto invs=bulk_invs<base>(res); for(auto[i,x]:res|std::views::enumerate){ if(args[i] >=mod/2){ x=invs[i]; } } checkpoint("inv ans"); return res; } }
#pragma GCC pop_options
#line 5 "cp-algo/math/poly/calculus.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>deriv(poly_t<T>p,int k=1){ assert(k>=0); if(k>p.deg()){return k==0?p:poly_t<T>{};} if(k==0){return p;} if(k==1){ for(int i=1;i<=p.deg();i++){p.a[i-1] =T(i)*p.a[i];} p.a.pop_back(); p.normalize(); return p; } for(int i=k;i<=p.deg();i++){ p.a[i-k] =fact<T>(i)*rfact<T>(i-k)*p.a[i]; } p.a.resize(p.a.size()-k); p.normalize(); return p; } template<typename T> poly_t<T>integr(poly_t<T>p){ if(p.is_zero()){return p;} p.a.push_back(0); for(int i=p.deg()-1;i>=0;i--){ p.a[i+1] =p.a[i] *small_inv<T>(i+1); } p.a[0] =0; return p; } }
#pragma GCC pop_options
#line 5 "cp-algo/math/poly/series/log.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>log(poly_t<T>p,size_t n){ if(n==0){ return{}; } assert(p[0] ==T(1)); p.mod_xk_inplace(n); auto dp=deriv(p); size_t k=n-1; if(k<magic){ poly::impl::inv_inplace(p,k); p.mul_truncate(dp,k); return integr(std::move(p)); } size_t m=std::bit_floor(k-1),t=k-m; auto r=inv(p,m); auto R=fft::spectrum<T>(r.a,2*m); typename poly_t<T>::Vector work(2*m); fft::spectrum<T>(dp.a|std::views::take(m),2*m).multiply(R,work,m); poly_t<T>q(typename poly_t<T>::Vector(begin(work),begin(work)+m)); { auto Q=fft::spectrum<T>(q.a,2*m); fft::spectrum<T>(p.a|std::views::take(k),2*m).multiply(Q,work,k); } for(size_t i=0;i<t;i++){ work[m+i] =dp[int(m+i)] -work[m+i]; } fft::spectrum<T>(work|std::views::drop(m)|std::views::take(t),2*m).multiply(R,work,t); q.a.resize(k); std::copy_n(begin(work),t,begin(q.a)+m); q.normalize(); return integr(std::move(q)); } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/series/exp.hpp"
#line 5 "cp-algo/math/poly/series/exp.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>exp(poly_t<T>p,size_t n){ if(n==0){ return{}; } assert(p[0] ==T(0)); p.mod_xk_inplace(n); if(p.is_zero()){ return T(1); } size_t m=std::min(n,std::bit_floor(size_t(magic-1))); typename poly_t<T>::Vector seed(m); seed[0] =1; for(size_t i=1;i<m;i++){ for(size_t j=1;j<=i&&j<p.a.size();j++){ seed[i] +=T(j)*p.a[j] *seed[i-j]; } seed[i] *=small_inv<T>(i); } poly_t<T>q(std::move(seed)); if(m==n){ return q; } auto r=inv(q,m),dp=deriv(p); for(;m<n;m*=2){ size_t k=std::min(2*m,n),t=k-m; auto Q=fft::spectrum<T>(q.a,2*m),R=fft::spectrum<T>(r.a,2*m); typename poly_t<T>::Vector work(2*m); fft::spectrum<T>(dp.a|std::views::take(k-1),2*m).multiply(Q,work,k-1); fft::spectrum<T>(work|std::views::drop(m-1)|std::views::take(t),2*m).multiply(R,work,t); for(size_t i=0;i<t;i++){ work[i] *=small_inv<T>(m+i); } auto d=typename poly_t<T>::Vector(begin(work),begin(work)+t); fft::spectrum<T>(d,2*m).multiply(Q,work,t); q.a.resize(k); std::copy_n(begin(work),t,begin(q.a)+m); if(k==n){ break; } std::move(Q).multiply(R,work,k); for(size_t i=0;i<t;i++){ work[m+i] +=d[i]; } fft::spectrum<T>(work|std::views::drop(m)|std::views::take(t),2*m).multiply(R,work,t); r.a.resize(k); for(size_t i=0;i<t;i++){ r.a[m+i] = -work[i]; } } q.normalize(); return q; } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/series/pow.hpp"
#line 5 "cp-algo/math/poly/series/pow.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ namespace poly::impl{ template<typename T> poly_t<T>pow_slow(poly_t<T>const&p,int64_t k,size_t n){ typename poly_t<T>::Vector q(n); q[0] =bpow(p[0],k); auto a0inv=p[0].inv(); for(int i=1;i<(int)n;i++){ for(int j=1;j<=std::min(p.deg(),i);j++){ q[i] +=p[j] *q[i-j] *(T(k)*T(j)-T(i-j)); } q[i] *=small_inv<T>(i)*a0inv; } return q; } } template<typename T> poly_t<T>pow(poly_t<T>p,int64_t k,size_t n){ assert(k>=0); if(n==0){return{};} if(k==0){return T(1);} p.mod_xk_inplace(n); if(p.is_zero()){return p;} size_t shift=p.trailing_xk(); if(shift){ if(uint64_t(k)>(n-1)/shift){return{};} p.div_xk_inplace(shift); return pow(std::move(p),k,n-shift*k).mul_xk(shift*k); } if(std::min(p.deg(),(int)n)<=magic){ return poly::impl::pow_slow(p,k,n); } if(k<=magic){ auto t=pow(p,k/2,n); t.mul_truncate(t,n); if(k%2){t.mul_truncate(p,n);} return t; } T c=p[0]; p/=c; return bpow(c,k)*exp(log(std::move(p),n)*T(k),n); } }
#pragma GCC pop_options
#line 6 "cp-algo/math/poly/series.hpp"
#line 1 "cp-algo/math/poly/series/sqrt.hpp"
#line 5 "cp-algo/math/poly/series/sqrt.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> std::optional<poly_t<T>>sqrt(poly_t<T>p,size_t n){ if(n==0){ return poly_t<T>{}; } p.mod_xk_inplace(n); if(p.is_zero()){ return p; } size_t shift=p.trailing_xk(); if(shift%2){ return std::nullopt; } if(shift){ p.div_xk_inplace(shift); auto ans=sqrt(std::move(p),n-shift); if(ans){ ans->mul_xk_inplace(shift/2); } return ans; } auto c=math::sqrt(p[0]); if(!c){ return std::nullopt; } size_t m=std::min(n,std::bit_floor(size_t(magic-1))); typename poly_t<T>::Vector seed(m); T half_inv=T(1)/(T(2)* *c); seed[0] = *c; for(size_t i=1;i<m;i++){ seed[i] =p[int(i)]; for(size_t j=1;j<i;j++){ seed[i] -=seed[j] *seed[i-j]; } seed[i] *=half_inv; } poly_t<T>ans(std::move(seed)); if(m==n){ return ans; } auto r=inv(ans,m); T half=T(1)/T(2); for(;m<n;m*=2){ size_t k=std::min(2*m,n),t=k-m; auto R=fft::spectrum<T>(r.a,2*m); typename poly_t<T>::Vector work(2*m); fft::spectrum<T>(ans.a,2*m).square(work,k); for(size_t i=0;i<t;i++){ work[m+i] =(p[int(m+i)] -work[m+i])*half; } fft::spectrum<T>(work|std::views::drop(m)|std::views::take(t),2*m).multiply(R,work,t); ans.a.resize(k); std::copy_n(begin(work),t,begin(ans.a)+m); if(k==n){ break; } fft::spectrum<T>(ans.a,2*m).multiply(R,work,k); fft::spectrum<T>(work|std::views::drop(m)|std::views::take(t),2*m).multiply(R,work,t); r.a.resize(k); for(size_t i=0;i<t;i++){ r.a[m+i] = -work[i]; } } ans.normalize(); return ans; } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/sparse.hpp"
#line 1 "cp-algo/math/poly/sparse/inv.hpp"
#line 1 "cp-algo/math/poly/sparse/impl.hpp"
#line 5 "cp-algo/math/poly/sparse/impl.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::poly::impl{ template<typename T> auto sparse_terms(poly_t<T>const&p,size_t n,size_t shift=0){ std::vector<std::pair<size_t,T>>terms; for(size_t i=shift+1;i<p.a.size()&&i-shift<n;i++){ if(p.a[i] !=T(0)){terms.emplace_back(i-shift,p.a[i]);} } return terms; } template<typename T> T sparse_dot(auto const&terms,typename poly_t<T>::Vector const&q,size_t i){ if constexpr(T::bits<=32){ if(T::mod()<(1<<30)&&terms.size()<=16){ uint64_t sum=0; for(auto[j,a]:terms){ if(j>i){break;} sum+=uint64_t(a.getr())*q[i-j].getr(); } return T(sum%T::mod()); } } T sum=0; for(auto[j,a]:terms){ if(j>i){break;} sum+=a*q[i-j]; } return sum; } template<typename T> poly_t<T>pow_sparse_unit(poly_t<T>const&p,T k,size_t n,size_t shift){ auto terms=sparse_terms(p,n,shift); T a0inv=T(1)/p.a[shift]; for(auto&[j,a]:terms){a*=a0inv;} std::vector<T>weights; for(auto[j,a]:terms){weights.push_back((k+T(1))*T(j));} typename poly_t<T>::Vector q(n); q[0] =1; if constexpr(T::bits<=32){ if(T::mod()<(1<<30)&&terms.size()>=6&&terms.size()<=16){ for(size_t t=0;t<terms.size();t++){weights[t] *=terms[t].second;} for(size_t i=1;i<n;i++){ uint64_t plain=0,weighted=0; for(size_t t=0;t<terms.size();t++){ auto[j,a] =terms[t]; if(j>i){break;} auto v=q[i-j].getr(); plain+=uint64_t(a.getr())*v; weighted+=uint64_t(weights[t].getr())*v; } q[i] =(T(weighted%T::mod())-T(i)*T(plain%T::mod()))*small_inv<T>(i); } return q; } } for(size_t i=1;i<n;i++){ T index=T(i); for(size_t t=0;t<terms.size();t++){ auto[j,a] =terms[t]; if(j>i){break;} q[i] +=a*q[i-j] *(weights[t] -index); } q[i] *=small_inv<T>(i); } return q; } }
#pragma GCC pop_options
#line 4 "cp-algo/math/poly/sparse/inv.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>inv_sparse(poly_t<T>const&p,size_t n){ if(n==0){return{};} assert(p[0] !=T(0)); auto terms=poly::impl::sparse_terms(p,n); T a0inv=T(1)/p[0]; for(auto&[j,a]:terms){a*= -a0inv;} typename poly_t<T>::Vector q(n); q[0] =a0inv; for(size_t i=1;i<n;i++){ q[i] =poly::impl::sparse_dot<T>(terms,q,i); } return q; } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/sparse/exp.hpp"
#line 4 "cp-algo/math/poly/sparse/exp.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>exp_sparse(poly_t<T>const&p,size_t n){ if(n==0){return{};} assert(p[0] ==T(0)); auto terms=poly::impl::sparse_terms(p,n); for(auto&[j,a]:terms){a*=T(j);} typename poly_t<T>::Vector q(n); q[0] =1; for(size_t i=1;i<n;i++){ q[i] =poly::impl::sparse_dot<T>(terms,q,i); q[i] *=small_inv<T>(i); } return q; } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/sparse/log.hpp"
#line 4 "cp-algo/math/poly/sparse/log.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>log_sparse(poly_t<T>const&p,size_t n){ if(n==0){return{};} assert(p[0] ==T(1)); auto terms=poly::impl::sparse_terms(p,n); typename poly_t<T>::Vector q(n); for(size_t i=1;i<n;i++){ q[i] =T(i)*p[int(i)]; q[i] -=poly::impl::sparse_dot<T>(terms,q,i); } for(size_t i=1;i<n;i++){q[i] *=small_inv<T>(i);} return q; } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/sparse/pow.hpp"
#line 4 "cp-algo/math/poly/sparse/pow.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>pow_sparse(poly_t<T>const&p,int64_t k,size_t n){ assert(k>=0); if(n==0){return{};} if(k==0){return T(1);} if(p.is_zero()){return{};} size_t shift=p.trailing_xk(); if(shift&&uint64_t(k)>(n-1)/shift){return{};} size_t offset=shift*uint64_t(k); auto q=poly::impl::pow_sparse_unit(p,T(k),n-offset,shift); q*=bpow(p.a[shift],k); q.mul_xk_inplace(offset); return q; } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/sparse/sqrt.hpp"
#line 5 "cp-algo/math/poly/sparse/sqrt.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> std::optional<poly_t<T>>sqrt_sparse(poly_t<T>const&p,size_t n){ if(n==0||p.is_zero()){return poly_t<T>{};} size_t shift=p.trailing_xk(); if(shift>=n){return poly_t<T>{};} if(shift%2){return std::nullopt;} auto c=math::sqrt(p.a[shift]); if(!c){return std::nullopt;} auto q=poly::impl::pow_sparse_unit(p,T(1)/T(2),n-shift,shift); q*= *c; q.mul_xk_inplace(shift/2); return q; } }
#pragma GCC pop_options
#line 8 "cp-algo/math/poly/sparse.hpp"
#line 1 "cp-algo/math/poly/euclid.hpp"
#line 1 "cp-algo/math/poly/impl/euclid.hpp"
#line 8 "cp-algo/math/poly/impl/euclid.hpp"
#include <numeric>
#line 12 "cp-algo/math/poly/impl/euclid.hpp"
#include <list>
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::poly::impl{ template<typename poly> using gcd_result=std::pair< std::list<std::decay_t<poly>>, linfrac<std::decay_t<poly>>>; template<bool quotients=true,typename poly> gcd_result<poly>half_gcd(poly&&A,poly&&B){ assert(A.deg()>=B.deg()); size_t m=size(A.a)/2; if(B.deg()<(int)m){ return{}; } auto[ai,R] =divmod(A,B); A=std::move(B); B=std::move(R); std::list<std::decay_t<poly>>a; if constexpr(quotients){a.push_back(ai);} auto T= -linfrac(ai).adj(); auto advance= [&](size_t k){ auto[ak,Tk] =half_gcd<quotients>(A.div_xk(k),B.div_xk(k)); a.splice(end(a),ak); T.prepend(Tk); return Tk; }; advance(m).apply(A,B); if constexpr(std::is_reference_v<poly>){ advance(2*m-A.deg()).apply(A,B); }else{ advance(2*m-A.deg()); } return{std::move(a),std::move(T)}; } template<bool extended=true,bool quotients=true,typename poly> gcd_result<poly>full_gcd(poly&&A,poly&&B){ using poly_t=std::decay_t<poly>; std::list<poly_t>ak; big_vector<linfrac<poly_t>>trs; while(!B.is_zero()){ auto[a0,R] =divmod(A,B); if constexpr(extended){trs.push_back(-linfrac(a0).adj());} if constexpr(quotients){ak.push_back(std::move(a0));} A=std::move(B); B=std::move(R); auto[a,Tr] =half_gcd<quotients>(A,B); ak.splice(end(ak),a); if constexpr(extended){trs.push_back(std::move(Tr));} } if constexpr(extended){ return{std::move(ak),std::accumulate(rbegin(trs),rend(trs),linfrac<poly_t>{},std::multiplies{})}; }else{ return{std::move(ak),{}}; } } auto convergent(auto L,auto R){ using poly=decltype(L)::value_type; if(L==R){ return linfrac<poly>{}; }else if(R==next(L)){ return linfrac(*L); }else{ int s=std::transform_reduce(L,R,0,std::plus{},std::mem_fn(&poly::deg)); auto M=next(L); for(int c=L->deg();next(M)!=R&&2*c<s;c+=M->deg(),++M){} return convergent(L,M)*convergent(M,R); } } template<typename poly> poly min_rec(poly const&p,size_t d){ auto R2=p.mod_xk(d).reversed(d),R1=poly::xk(d); if(R2.is_zero()){ return poly(1); } auto[a,Tr] =half_gcd(R1,R2); if(!R2.is_zero()){a.push_back(divmod(R1,R2)[0]);} a.emplace_back(); auto pref=begin(a); for(int delta=(int)d-a.front().deg();next(pref)!=end(a)&&delta>=0;pref++){ delta-=pref->deg()+next(pref)->deg(); } return convergent(begin(a),pref).a; } template<typename poly> std::optional<poly>inv_mod(poly p,poly q){ assert(!q.is_zero()); auto[a,Tr] =full_gcd<true,false>(q,p); if(q.deg()!=0){ return std::nullopt; } return std::move(Tr.b)/q[0]; } }
#pragma GCC pop_options
#line 4 "cp-algo/math/poly/euclid.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>gcd(poly_t<T>a,poly_t<T>b){ poly::impl::full_gcd<false,false>(a,b); return a; } template<typename T> std::optional<poly_t<T>>inv_mod(poly_t<T>p,poly_t<T>q){ return poly::impl::inv_mod(std::move(p),std::move(q)); } template<typename T> T resultant(poly_t<T>a,poly_t<T>b){ T res=1; while(!b.is_zero()){ if(b.deg()==0){return res*bpow(b.lead(),a.deg());} int d=a.deg(); a%=b; res*=bpow(b.lead(),d-a.deg())*T((b.deg()&a.deg()&1)?-1:1); std::swap(a,b); } return T(0); } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/recurrence.hpp"
#line 4 "cp-algo/math/poly/recurrence.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::poly::impl{ template<typename poly> poly&inv_inplace(poly&q,int64_t k,size_t n){ if(n==0){ q.a.clear(); return q; } assert(k>=0||uint64_t(-(k+1))<n); using poly_t=std::decay_t<poly>; using base=poly_t::base; if(k<=std::max<int64_t>(n,size(q.a))){ inv_inplace(q,size_t(k+int64_t(n))); return q.div_xk_inplace(k); } if(k%2){ return inv_inplace(q,k-1,n+1).div_xk_inplace(1); } auto[q0,q1] =q.bisect(); auto qq=q0*q0-(q1*q1).mul_xk_inplace(1); inv_inplace(qq,k/2-q.deg()/2,(n+1)/2+q.deg()/2); size_t N=fft::com_size(size(q0.a),size(qq.a)); auto q0f=fft::spectrum<base>(q0.a,2*N); auto q1f=fft::spectrum<base>(q1.a,2*N); auto qqf=fft::spectrum<base>(qq.a,2*N); size_t M=q0.deg()+(n+1)/2; typename poly::Vector A,B; A.resize((M+fft::flen-1)/fft::flen*fft::flen); B.resize((M+fft::flen-1)/fft::flen*fft::flen); std::move(q0f).multiply(qqf,A,M); std::move(q1f).multiply(qqf,B,M); q.a.resize(n+1); for(size_t i=0;i<n;i+=2){ q.a[i] =A[q0.deg()+i/2]; q.a[i+1] = -B[q0.deg()+i/2]; } q.a.pop_back(); q.normalize(); return q; } } namespace cp_algo::math{ template<typename T> poly_t<T>min_rec(poly_t<T>const&p,size_t d){ return poly::impl::min_rec(p,d); } template<typename T> poly_t<T>inv(poly_t<T>p,int64_t k,size_t n){ poly::impl::inv_inplace(p,k,n); return p; } template<typename T> T kth_rec(poly_t<T>P,poly_t<T>Q,int64_t k){ assert(k>=0&&Q[0] !=T(0)); while(k>Q.deg()){ size_t n=Q.a.size(); auto[Q0,Q1] =Q.bisect(); auto[P0,P1] =P.bisect(); size_t N=fft::com_size((n+1)/2,(n+1)/2); auto Q0f=fft::spectrum<T>(Q0.a,2*N); auto Q1f=fft::spectrum<T>(Q1.a,2*N); auto P0f=fft::spectrum<T>(P0.a,2*N); auto P1f=fft::spectrum<T>(P1.a,2*N); auto squared= [N](auto const&f){ big_vector<T>res(2*N); f.clone().square(res,2*N); return res; }; auto times= [N](auto&&f,auto const&g){ big_vector<T>res(2*N); std::move(f).multiply(g,res,2*N); return res; }; Q=poly_t<T>(squared(Q0f))-poly_t<T>(squared(Q1f)).mul_xk_inplace(1); if(k%2){ P=poly_t<T>(times(std::move(Q0f),P1f))-poly_t<T>(times(std::move(Q1f),P0f)); }else{ P=poly_t<T>(times(std::move(Q0f),P0f))-poly_t<T>(times(std::move(Q1f),P1f)).mul_xk_inplace(1); } k/=2; } size_t n=size_t(k)+1; P.mul_truncate(inv(std::move(Q),n),n); return P[(int)k]; } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/transform.hpp"
#line 5 "cp-algo/math/poly/transform.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>mulx(poly_t<T>p,std::type_identity_t<T>c){ T cur=1; for(auto&x:p.a){x*=cur;cur*=c;} p.normalize(); return p; } template<typename T> poly_t<T>mulx_sq(poly_t<T>p,std::type_identity_t<T>c){ T cur=1,total=1; for(auto&x:p.a){x*=total;cur*=c;total*=cur;} p.normalize(); return p; } template<typename T> poly_t<T>invborel(poly_t<T>p){ for(int i=0;i<=p.deg();i++){p.a[i] *=fact<T>(i);} return p; } template<typename T> poly_t<T>borel(poly_t<T>p){ for(int i=0;i<=p.deg();i++){p.a[i] *=rfact<T>(i);} return p; } template<typename T> poly_t<T>expx(size_t n){return borel(poly_t<T>::ones(n));} template<typename T> poly_t<T>log1px(size_t n){ typename poly_t<T>::Vector a(n); for(size_t i=1;i<n;i++){a[i] =(i&1?small_inv<T>(i): -small_inv<T>(i));} return a; } template<typename T> poly_t<T>log1mx(size_t n){ return n?-integr(poly_t<T>::ones(n-1)):poly_t<T>{}; } template<typename T> poly_t<T>corr(poly_t<T>a,poly_t<T>b){ b.reverse(); a*=b; return a; } template<typename T> poly_t<T>forward_corr(poly_t<T>a,poly_t<T>b){ if(b.is_zero()){return{};} int d=b.deg(); return corr(std::move(a),std::move(b)).div_xk(d); } template<typename T> poly_t<T>inner_corr(poly_t<T>a,poly_t<T>b){ if(b.is_zero()||a.deg()<b.deg()){return{};} int d=b.deg(),n=a.deg()-d+1; size_t N=std::max(fft::flen,std::bit_ceil(a.a.size())); std::ranges::reverse(b.a); a.a.resize(N); b.a.resize(N); fft::cyclic_mul(a.a,b.a,N); a.substr_inplace(d,n); return a; } template<typename T> poly_t<T>apply_diff(poly_t<T>p,poly_t<T>g){ return borel(forward_corr(invborel(std::move(p)),std::move(g))); } template<typename T> poly_t<T>shift(poly_t<T>p,std::type_identity_t<T>c){ auto g=mulx(expx<T>(p.deg()+1),c); return apply_diff(std::move(p),std::move(g)); } template<typename T> poly_t<T>prefix_sum(poly_t<T>p){ auto g=inv(expx<T>(p.deg()+2).div_xk(1),p.deg()+1); return integr(apply_diff(std::move(p),std::move(g))); } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/chirpz.hpp"
#line 4 "cp-algo/math/poly/chirpz.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>chirpz(poly_t<T>p,std::type_identity_t<T>z,int n){ assert(n>=0); if(n==0||p.is_zero()){return{};} if(z==T(0)){ typename poly_t<T>::Vector ans(n,p[0]); ans[0] =p.eval(1); return ans; } auto b=mulx_sq(poly_t<T>::ones(n+p.deg()),z); auto a=mulx_sq(std::move(p),z.inv()); return mulx_sq(inner_corr(std::move(b),std::move(a)),z.inv()); } namespace poly::impl{ template<typename T> big_vector<T>geometric_products_inv(T z,int n){ big_vector<T>res(n,1),zk(n,1); for(int i=1;i<n;i++){ zk[i] =zk[i-1] *z; res[i] =res[i-1] *(T(1)-zk[i]); } res.back()=res.back().inv(); for(int i=n-2;i>=0;i--){res[i] =(T(1)-zk[i+1])*res[i+1];} return res; } template<typename T> poly_t<T>geometric_product(T z,int n){ if(n==0){return T(1);} if(n==1){return typename poly_t<T>::Vector{1,-1};} auto p=geometric_product(z,n/2); p*=mulx(p,bpow(z,n/2)); if(n%2){p*=poly_t<T>(typename poly_t<T>::Vector{1,-bpow(z,n-1)});} return p; } } template<typename T> poly_t<T>chirpz_inverse(poly_t<T>p,std::type_identity_t<T>z,int n){ assert(n>=0); if(n==0||p.is_zero()){return{};} if(z==T(0)){ return n==1?p.mod_xk(1):poly_t<T>(typename poly_t<T>::Vector{p[1],p[0] -p[1]}); } p.a.resize(n); auto pos=poly::impl::geometric_products_inv(z,n); auto neg=poly::impl::geometric_products_inv(z.inv(),n); T zn=bpow(z,n-1).inv(),znk=1; for(int i=0;i<n;i++){ p.a[i] *=znk*neg[i] *pos[n-1-i]; znk*=zn; } p=chirpz(std::move(p),z,n); typename poly_t<T>::Vector product(n); T numerator=1,triangle=1,power=1,backwards=bpow(z,n),iz=z.inv(); for(int k=0;k<n;k++){ product[k] =numerator*triangle*pos[k]; numerator*=T(1)-backwards; backwards*=iz; triangle*= -power; power*=z; } p.mul_truncate(poly_t<T>(std::move(product)),n); p.mod_xk_inplace(n).reverse(n); return p; } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/eval.hpp"
#line 5 "cp-algo/math/poly/eval.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math::poly::impl{ template<typename T> void build(big_vector<poly_t<T>> &tree,int v,auto l,auto r){ if(r-l==1){ tree[v] =typename poly_t<T>::Vector{-*l,1}; }else{ auto m=l+(r-l)/2; build(tree,2*v,l,m); build(tree,2*v+1,m,r); tree[v] =tree[2*v] *tree[2*v+1]; } } template<typename T> void eval(poly_t<T>const&p,big_vector<poly_t<T>>const&tree,int v,auto l,auto r,auto out){ if(r-l<=32){ for(;l!=r;++l,++out){*out=p.eval(*l);} }else{ auto m=l+(r-l)/2; eval(p%tree[2*v],tree,2*v,l,m,out); eval(p%tree[2*v+1],tree,2*v+1,m,r,out+(m-l)); } } template<typename T> poly_t<T>inter(big_vector<poly_t<T>>const&tree,int v,auto l,auto r){ if(r-l==1){return*l;} auto m=l+(r-l)/2; auto a=inter(tree,2*v,l,m); auto b=inter(tree,2*v+1,m,r); return std::move(a)*tree[2*v+1] +std::move(b)*tree[2*v]; } template<typename T> poly_t<T>to_newton(poly_t<T>p,big_vector<poly_t<T>>const&tree,int v,size_t l,size_t r){ if(r-l==1){return p;} size_t m=l+(r-l)/2; auto[q,rem] =divmod(std::move(p),tree[2*v]); auto a=to_newton(std::move(rem),tree,2*v,l,m); auto b=to_newton(std::move(q),tree,2*v+1,m,r); a+=b.mul_xk_inplace(m-l); return a; } } namespace cp_algo::math{ template<typename T> big_vector<T>eval(poly_t<T>const&p,big_vector<T>const&x){ big_vector<T>res(x.size()); if(x.empty()||p.is_zero()){return res;} big_vector<poly_t<T>>tree(4*x.size()); poly::impl::build(tree,1,begin(x),end(x)); poly::impl::eval(p,tree,1,begin(x),end(x),begin(res)); return res; } template<typename T> poly_t<T>inter(big_vector<T>const&x,big_vector<T>const&y){ assert(x.size()==y.size()); if(x.empty()){return{};} big_vector<poly_t<T>>tree(4*x.size()); poly::impl::build(tree,1,begin(x),end(x)); big_vector<T>values(x.size()); poly::impl::eval(deriv(tree[1]),tree,1,begin(x),end(x),begin(values)); auto weights=bulk_invs<T>(values); for(size_t i=0;i<y.size();i++){weights[i] *=y[i];} return poly::impl::inter(tree,1,begin(weights),end(weights)); } template<typename T> poly_t<T>to_newton(poly_t<T>f,big_vector<T>const&p){ assert(f.deg()<(int)p.size()); if(p.empty()||f.is_zero()){return f;} big_vector<poly_t<T>>tree(4*p.size()); poly::impl::build(tree,1,begin(p),end(p)); return poly::impl::to_newton(std::move(f),tree,1,0,p.size()); } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/powmod.hpp"
#line 5 "cp-algo/math/poly/powmod.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>circular_closure(poly_t<T>p,size_t m){ assert(m>0); for(size_t i=p.a.size();i>m;--i){p.a[i-1-m] +=p.a[i-1];} p.mod_xk_inplace(m); return p; } template<typename T> poly_t<T>powmod_circular(poly_t<T>p,int64_t k,size_t m){ assert(k>=0&&m>0); p=circular_closure(std::move(p),m); return bpow(p,k,poly_t<T>(1),[m](auto const&a,auto const&b){ auto product=a; product*= &a== &b?product:b; return circular_closure(std::move(product),m); }); } template<typename T> poly_t<T>powmod(poly_t<T>p,int64_t k,poly_t<T>const&md){ assert(k>=0&& !md.is_zero()); int d=md.deg(); if(d==0){return{};} if(md==poly_t<T>::xk(d)){return pow(std::move(p),k,d);} if(md==poly_t<T>::xk(d)-poly_t<T>(1)){return powmod_circular(std::move(p),k,d);} auto mdri=inv(md.reversed(),d+1); return bpow(p%md,k,poly_t<T>(1),[&](auto const&a,auto const&b){ auto product=a; product*= &a== &b?product:b; auto[q,r] =poly::impl::divmod_hint(std::move(product),md,mdri); return r; }); } }
#pragma GCC pop_options
#line 1 "cp-algo/math/poly/compose.hpp"
#line 5 "cp-algo/math/poly/compose.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::math{ template<typename T> poly_t<T>compose(poly_t<T>const&A,poly_t<T>const&B,int n){ if(n<=0){return{};} int q=(int)std::sqrt(n); big_vector<poly_t<T>>Bk(q); auto Bq=pow(B,q,n); Bk[0] =poly_t<T>(T(1)); for(int i=1;i<q;i++){ Bk[i] =(Bk[i-1] *B).mod_xk(n); } poly_t<T>Bqk(1); poly_t<T>ans; for(int i=0;i<=A.deg()/q;i++){ poly_t<T>cur; for(int j=0;j<q;j++){ cur+=Bk[j] *A[i*q+j]; } ans+=(Bqk*cur).mod_xk(n); Bqk=(Bqk*Bq).mod_xk(n); } return ans; } template<typename T> poly_t<T>compose_large(poly_t<T>A,poly_t<T>B,int n){ if(n<=0){return{};} if(B[0] !=T(0)){ return compose_large(shift(std::move(A),B[0]),B-B[0],n); } A.mod_xk_inplace(n); int q=(int)std::sqrt(n); auto[B0,B1] =std::make_pair(B.mod_xk(q),B.div_xk(q)); B0=B0.div_xk(1); big_vector<poly_t<T>>pw(A.deg()+1); auto getpow= [&](int k)->poly_t<T>const&{ return pw[k].is_zero()?pw[k] =pow(B0,k,n-k):pw[k]; }; auto compose_dac= [&getpow](this auto const&self,poly_t<T>const&f,int m,int N)->poly_t<T>{ if(f.deg()<=0){ return f; } int k=m/2; auto[f0,f1] =std::make_pair(f.mod_xk(k),f.div_xk(k)); auto[A,B] =std::make_pair(self(f0,k,N),self(f1,m-k,N-k)); return(A+(B.mod_xk(N-k)*getpow(k).mod_xk(N-k)).mul_xk(k)).mod_xk(N); }; int r=n/q; auto Ar=deriv(A,r); auto AB0=compose_dac(Ar,Ar.deg()+1,n); auto Bd=deriv(B0.mul_xk(1)); poly_t<T>ans=T(0); big_vector<poly_t<T>>B1p(r+1); B1p[0] =poly_t<T>(T(1)); for(int i=1;i<=r;i++){ B1p[i] =(B1p[i-1] *B1.mod_xk(n-i*q)).mod_xk(n-i*q); } while(r>=0){ ans+=(AB0.mod_xk(n-r*q)*rfact<T>(r)*B1p[r]).mul_xk(r*q).mod_xk(n); r--; if(r>=0){ AB0=(integr(AB0*Bd)+A[r] *fact<T>(r)).mod_xk(n); } } return ans; } }
#pragma GCC pop_options
#line 15 "cp-algo/math/poly.hpp"