This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "src/FFT/fps_sqrt.hpp"
#pragma once
#include "src/FFT/fps_inv.hpp"
#include "src/Math/mod_sqrt.hpp"
namespace math_internal {
template <size_t LM, class mod_t> void sqrt_base(const mod_t p[], int n, mod_t r[], int l, mod_t v[], mod_t iv[]) {
static constexpr int t= nttarr_cat<mod_t, LM>, TH= (int[]){64, 64, 256, 256, 256, 256}[t];
using GNA1= GlobalNTTArray<mod_t, LM, 1>;
using GNA2= GlobalNTTArray<mod_t, LM, 2>;
using GNA3= GlobalNTTArray<mod_t, LM, 3>;
auto gt1= GlobalNTTArray2D<mod_t, LM, 7, 1>::bf;
assert(n > 1);
const int m= min(n, TH);
const mod_t miv= mod_t(mod_t::mod() >> 1) / r[0];
int i= 2;
for ((r[1]-= p[1])*= miv; i < m; r[i]*= miv, ++i) {
for (int j= (i + 1) / 2; --j;) r[i]+= r[j] * r[i - j];
if (r[i]+= r[i]; !(i & 1)) r[i]+= r[i >> 1] * r[i >> 1];
if (i < l) r[i]-= p[i];
}
if (i == n) return;
int skip= (__builtin_ctz(n / i) + 2) % 3 + 1;
v[0]= -r[0] - r[0], iv[0]= miv;
for (int ed= (1 << skip) - 1, u= 1; i < n; ed= 7) {
mod_t* rr= r;
const mod_t* pp= p;
const int s= i, e= s << 1;
for (int j= u; j < s; ++j) v[j]= -r[j] - r[j];
inv_base<LM>(v, s, iv, u), u= s;
GNA1::bf.set(iv, 0, s), GNA1::bf.zeros(s, e), GNA1::bf.dft(0, e);
for (int k= 0, j; i < n && k < ed; ++k, i+= s) {
gt1[k].set(rr, 0, s), gt1[k].zeros(s, e), gt1[k].dft(0, e);
for (GNA2::bf.zeros(0, e), j= k >> 1; j--;) GNA3::bf.add(gt1[j + 1], gt1[j], 0, s), GNA3::bf.dif(gt1[j + 1], gt1[j], s, e), GNA3::bf.mul(gt1[k - j], 0, e), GNA2::bf.add(GNA3::bf, 0, e);
if (j= (k + 1) >> 1; k & 1) GNA3::bf.mul(gt1[j], gt1[k - j], 0, e), GNA2::bf.add(GNA3::bf, 0, s), GNA2::bf.dif(GNA3::bf, s, e);
if (k) GNA2::bf.add(GNA2::bf, 0, e);
GNA3::bf.mul(gt1[j], gt1[j], 0, e);
k & 1 ? GNA2::bf.add(GNA3::bf, s, e) : GNA2::bf.dif(GNA3::bf, s, e);
GNA2::bf.add(GNA3::bf, 0, s), GNA2::bf.idft(0, e), GNA2::bf.zeros(s, e), GNA2::bf.get(rr+= s, 0, s);
if (j= min(s, l - s * k); j > 0)
for (pp+= s; j--;) rr[j]-= pp[j];
GNA2::bf.set(rr, 0, s), GNA2::bf.dft(0, e), GNA2::bf.mul(GNA1::bf, 0, e), GNA2::bf.idft(0, e), GNA2::bf.get(rr, 0, min(s, n - s * k));
}
}
}
template <class mod_t, size_t LM= 1 << 22> vector<mod_t> sqrt(const vector<mod_t>& p) {
mod_t *r= GlobalArray<mod_t, LM, 1>::bf, *v= GlobalArray<mod_t, LM, 2>::bf, *iv= GlobalArray<mod_t, LM, 3>::bf;
static constexpr size_t LM2= LM >> 2;
int n= p.size(), cnt= 0;
while (cnt < n && p[cnt] == mod_t()) cnt++;
if (cnt == n) return p;
if (cnt & 1) return {}; // no solution
const int nn= n - (cnt >> 1), l= n - cnt;
const mod_t* pp= p.data() + cnt;
mod_t* rr= r + (cnt >> 1);
fill_n(r, n, mod_t());
if (rr[0]= mod_sqrt(pp[0].val(), mod_t::mod()); rr[0] * rr[0] != pp[0]) return {}; // no solution
sqrt_base<LM2, mod_t>(pp, nn, rr, l, v, iv);
return vector(r, r + n);
}
}
using math_internal::sqrt;
#line 2 "src/FFT/fps_inv.hpp"
#include <vector>
#include <algorithm>
#include <cassert>
#line 2 "src/FFT/NTT.hpp"
#include <array>
#include <limits>
#line 2 "src/Internal/Remainder.hpp"
namespace math_internal {
using namespace std;
using u8= unsigned char;
using u32= unsigned;
using i64= long long;
using u64= unsigned long long;
using u128= __uint128_t;
struct MP_Na { // mod < 2^32
u32 mod;
constexpr MP_Na(): mod(0) {}
constexpr MP_Na(u32 m): mod(m) {}
constexpr inline u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; }
constexpr inline u32 set(u32 n) const { return n; }
constexpr inline u32 get(u32 n) const { return n; }
constexpr inline u32 norm(u32 n) const { return n; }
constexpr inline u32 plus(u64 l, u32 r) const { return l+= r, l < mod ? l : l - mod; }
constexpr inline u32 diff(u64 l, u32 r) const { return l-= r, l >> 63 ? l + mod : l; }
};
template <class u_t, class du_t, u8 B> struct MP_Mo { // mod < 2^32, mod < 2^62
u_t mod;
constexpr MP_Mo(): mod(0), iv(0), r2(0) {}
constexpr MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {}
constexpr inline u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); }
constexpr inline u_t set(u_t n) const { return mul(n, r2); }
constexpr inline u_t get(u_t n) const { return n= reduce(n), n >= mod ? n - mod : n; }
constexpr inline u_t norm(u_t n) const { return n >= mod ? n - mod : n; }
constexpr inline u_t plus(u_t l, u_t r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
constexpr inline u_t diff(u_t l, u_t r) const { return l-= r, l >> (B - 1) ? l + (mod << 1) : l; }
private:
u_t iv, r2;
static constexpr u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; }
constexpr inline u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); }
};
using MP_Mo32= MP_Mo<u32, u64, 32>;
using MP_Mo64= MP_Mo<u64, u128, 64>;
struct MP_Br { // 2^20 < mod <= 2^41
u64 mod;
constexpr MP_Br(): mod(0), x(0) {}
constexpr MP_Br(u64 m): mod(m), x((u128(1) << 84) / m) {}
constexpr inline u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); }
static constexpr inline u64 set(u64 n) { return n; }
constexpr inline u64 get(u64 n) const { return n >= mod ? n - mod : n; }
constexpr inline u64 norm(u64 n) const { return n >= mod ? n - mod : n; }
constexpr inline u64 plus(u64 l, u64 r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
constexpr inline u64 diff(u64 l, u64 r) const { return l-= r, l >> 63 ? l + (mod << 1) : l; }
private:
u64 x;
constexpr inline u128 quo(const u128 &n) const { return (n * x) >> 84; }
constexpr inline u64 rem(const u128 &n) const { return n - quo(n) * mod; }
};
template <class du_t, u8 B> struct MP_D2B1 { // mod < 2^63, mod < 2^64
u64 mod;
constexpr MP_D2B1(): mod(0), s(0), d(0), v(0) {}
constexpr MP_D2B1(u64 m): mod(m), s(__builtin_clzll(m)), d(m << s), v(u128(-1) / d) {}
constexpr inline u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; }
constexpr inline u64 set(u64 n) const { return n; }
constexpr inline u64 get(u64 n) const { return n; }
constexpr inline u64 norm(u64 n) const { return n; }
constexpr inline u64 plus(du_t l, u64 r) const { return l+= r, l < mod ? l : l - mod; }
constexpr inline u64 diff(du_t l, u64 r) const { return l-= r, l >> B ? l + mod : l; }
private:
u8 s;
u64 d, v;
constexpr inline u64 rem(const u128 &u) const {
u128 q= (u >> 64) * v + u;
u64 r= u64(u) - (q >> 64) * d - d;
if (r > u64(q)) r+= d;
if (r >= d) r-= d;
return r;
}
};
using MP_D2B1_1= MP_D2B1<u64, 63>;
using MP_D2B1_2= MP_D2B1<u128, 127>;
template <class u_t, class MP> constexpr u_t pow(u_t x, u64 k, const MP &md) {
for (u_t ret= md.set(1);; x= md.mul(x, x))
if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret;
}
}
#line 3 "src/NumberTheory/is_prime.hpp"
namespace math_internal {
template <class Uint, class MP, u32... args> constexpr bool miller_rabin(Uint n) {
const MP md(n);
const Uint s= __builtin_ctzll(n - 1), d= n >> s, one= md.set(1), n1= md.norm(md.set(n - 1));
for (u32 a: (u32[]){args...})
if (Uint b= a % n; b)
if (Uint p= md.norm(pow(md.set(b), d, md)); p != one)
for (int i= s; p != n1; p= md.norm(md.mul(p, p)))
if (!(--i)) return 0;
return 1;
}
}
constexpr bool is_prime(unsigned long long n) {
if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3;
if (n < (1 << 30)) return math_internal::miller_rabin<unsigned, math_internal::MP_Mo32, 2, 7, 61>(n);
if (n < (1ull << 62)) return math_internal::miller_rabin<unsigned long long, math_internal::MP_Mo64, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
if (n < (1ull << 63)) return math_internal::miller_rabin<unsigned long long, math_internal::MP_D2B1_1, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
return math_internal::miller_rabin<unsigned long long, math_internal::MP_D2B1_2, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
}
#line 2 "src/Math/ModInt.hpp"
#include <iostream>
#line 2 "src/Math/mod_inv.hpp"
#include <utility>
#include <type_traits>
#line 5 "src/Math/mod_inv.hpp"
template <class Uint> constexpr inline Uint mod_inv(Uint a, Uint mod) {
std::make_signed_t<Uint> x= 1, y= 0, z= 0;
for (Uint q= 0, b= mod, c= 0; b;) z= x, x= y, y= z - y * (q= a / b), c= a, a= b, b= c - b * q;
return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod;
}
#line 3 "src/Internal/modint_traits.hpp"
namespace math_internal {
struct m_b {};
struct s_b: m_b {};
}
template <class mod_t> constexpr bool is_modint_v= std::is_base_of_v<math_internal::m_b, mod_t>;
template <class mod_t> constexpr bool is_staticmodint_v= std::is_base_of_v<math_internal::s_b, mod_t>;
#line 6 "src/Math/ModInt.hpp"
namespace math_internal {
template <class MP, u64 MOD> struct SB: s_b {
protected:
static constexpr MP md= MP(MOD);
};
template <class U, class B> struct MInt: public B {
using Uint= U;
static constexpr inline auto mod() { return B::md.mod; }
constexpr MInt(): x(0) {}
template <class T, typename= enable_if_t<is_modint_v<T> && !is_same_v<T, MInt>>> constexpr MInt(T v): x(B::md.set(v.val() % B::md.mod)) {}
constexpr MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {}
constexpr MInt operator-() const { return MInt() - *this; }
#define FUNC(name, op) \
constexpr MInt name const { \
MInt ret; \
return ret.x= op, ret; \
}
FUNC(operator+(const MInt & r), B::md.plus(x, r.x))
FUNC(operator-(const MInt & r), B::md.diff(x, r.x))
FUNC(operator*(const MInt & r), B::md.mul(x, r.x))
FUNC(pow(u64 k), math_internal::pow(x, k, B::md))
#undef FUNC
constexpr MInt operator/(const MInt &r) const { return *this * r.inv(); }
constexpr MInt &operator+=(const MInt &r) { return *this= *this + r; }
constexpr MInt &operator-=(const MInt &r) { return *this= *this - r; }
constexpr MInt &operator*=(const MInt &r) { return *this= *this * r; }
constexpr MInt &operator/=(const MInt &r) { return *this= *this / r; }
constexpr bool operator==(const MInt &r) const { return B::md.norm(x) == B::md.norm(r.x); }
constexpr bool operator!=(const MInt &r) const { return !(*this == r); }
constexpr bool operator<(const MInt &r) const { return B::md.norm(x) < B::md.norm(r.x); }
constexpr inline MInt inv() const { return mod_inv<U>(val(), B::md.mod); }
constexpr inline Uint val() const { return B::md.get(x); }
friend ostream &operator<<(ostream &os, const MInt &r) { return os << r.val(); }
friend istream &operator>>(istream &is, MInt &r) {
i64 v;
return is >> v, r= MInt(v), is;
}
private:
Uint x;
};
template <u64 MOD> using MP_B= conditional_t < (MOD < (1 << 30)) & MOD, MP_Mo32, conditional_t < MOD < (1ull << 32), MP_Na, conditional_t<(MOD < (1ull << 62)) & MOD, MP_Mo64, conditional_t<MOD<(1ull << 41), MP_Br, conditional_t<MOD<(1ull << 63), MP_D2B1_1, MP_D2B1_2>>>>>;
template <u64 MOD> using ModInt= MInt < conditional_t<MOD<(1 << 30), u32, u64>, SB<MP_B<MOD>, MOD>>;
}
using math_internal::ModInt;
#line 6 "src/FFT/NTT.hpp"
template <class mod_t, size_t LM> mod_t get_inv(int n) {
static_assert(is_modint_v<mod_t>);
static const auto m= mod_t::mod();
static mod_t* dat= new mod_t[LM];
static int l= 1;
if (l == 1) dat[l++]= 1;
for (; l <= n; ++l) dat[l]= dat[m % l] * (m - m / l);
return dat[n];
}
namespace math_internal {
#define CE constexpr
#define ST static
#define TP template
#define BSF(_, n) __builtin_ctz##_(n)
TP<class mod_t> struct NTT {
#define _DFT(a, b, c, ...) \
mod_t r, u, *x0, *x1; \
for (int a= n, b= 1, s, i; a>>= 1; b<<= 1) \
for (s= 0, r= I, x0= x;; r*= c[BSF(, s)], x0= x1 + p) { \
for (x1= x0 + (i= p); i--;) __VA_ARGS__; \
if (++s == e) break; \
}
ST inline void dft(int n, mod_t x[]) { _DFT(p, e, r2, x1[i]= x0[i] - (u= r * x1[i]), x0[i]+= u); }
ST inline void idft(int n, mod_t x[]) {
_DFT(e, p, ir2, u= x0[i] - x1[i], x0[i]+= x1[i], x1[i]= r * u)
for (const mod_t iv= I / n; n--;) x[n]*= iv;
}
#undef _DFT
ST inline void even_dft(int n, mod_t x[]) {
for (int i= 0, j= 0; i < n; i+= 2) x[j++]= iv2 * (x[i] + x[i + 1]);
}
ST inline void odd_dft(int n, mod_t x[], mod_t r= iv2) {
for (int i= 0, j= 0;; r*= ir2[BSF(, ++j)])
if (x[j]= r * (x[i] - x[i + 1]); (i+= 2) == n) break;
}
ST inline void dft_doubling(int n, mod_t x[], int i= 0) {
mod_t k= I, t= rt[BSF(, n << 1)];
for (copy_n(x, n, x + n), idft(n, x + n); i < n; ++i) x[n + i]*= k, k*= t;
dft(n, x + n);
}
protected:
ST CE u64 md= mod_t::mod();
static_assert(md & 1);
static_assert(is_prime(md));
ST CE u8 E= BSF(ll, md - 1);
ST CE mod_t w= [](u8 e) {
for (mod_t r= 2;; r+= 1)
if (auto s= r.pow((md - 1) / 2); s != 1 && s * s == 1) return r.pow((md - 1) >> e);
return mod_t();
}(E);
static_assert(w != mod_t());
ST CE mod_t I= 1, iv2= (md + 1) / 2, iw= w.pow((1ULL << E) - 1);
ST CE auto roots(mod_t w) {
array<mod_t, E + 1> x= {};
for (u8 e= E; e; w*= w) x[e--]= w;
return x[0]= w, x;
}
TP<u32 N> ST CE auto ras(const array<mod_t, E + 1>& rt, const array<mod_t, E + 1>& irt, int i= N) {
array<mod_t, E + 1 - N> x= {};
for (mod_t ro= 1; i <= E; ro*= irt[i++]) x[i - N]= rt[i] * ro;
return x;
}
ST CE auto rt= roots(w), irt= roots(iw);
ST CE auto r2= ras<2>(rt, irt), ir2= ras<2>(irt, rt);
};
TP<class T, u8 t, class B> struct NI: public B {
using B::B;
#define FUNC(op, name, HG, ...) \
inline void name(__VA_ARGS__) { \
HG(op, 1); \
if CE (t > 1) HG(op, 2); \
if CE (t > 2) HG(op, 3); \
if CE (t > 3) HG(op, 4); \
if CE (t > 4) HG(op, 5); \
}
#define REP for (int i= b; i < e; ++i)
#define DFT(fft, _) B::ntt##_::fft(e - b, this->dt##_ + b)
#define ZEROS(op, _) fill_n(this->dt##_ + b, e - b, typename B::m##_())
#define SET(op, _) copy(x + b, x + e, this->dt##_ + b)
#define SET_S(op, _) this->dt##_[i]= x;
#define SUBST(op, _) copy(r.dt##_ + b, r.dt##_ + e, this->dt##_ + b)
#define ASGN(op, _) REP this->dt##_[i] op##= r.dt##_[i]
#define ASN(nm, op) TP<class C> FUNC(op, nm, ASGN, const NI<T, t, C>& r, int b, int e)
#define BOP(op, _) REP this->dt##_[i]= l.dt##_[i] op r.dt##_[i]
#define OP(nm, op) TP<class C, class D> FUNC(op, nm, BOP, const NI<T, t, C>& l, const NI<T, t, D>& r, int b, int e)
OP(add, +) OP(dif, -) OP(mul, *) ASN(add, +) ASN(dif, -) ASN(mul, *) FUNC(dft, dft, DFT, int b, int e) FUNC(idft, idft, DFT, int b, int e) FUNC(__, zeros, ZEROS, int b, int e) FUNC(__, set, SET, const T x[], int b, int e) FUNC(__, set, SET_S, int i, T x) TP<class C> FUNC(__, subst, SUBST, const NI<T, t, C>& r, int b, int e) inline void get(T x[], int b, int e) const {
if CE (t == 1) copy(this->dt1 + b, this->dt1 + e, x + b);
else REP x[i]= get(i);
}
#define TMP(_) B::iv##_##1 * (this->dt##_[i] - r1)
inline T get(int i) const {
if CE (t > 1) {
u64 r1= this->dt1[i].val(), r2= (TMP(2)).val();
T a= 0;
if CE (t > 2) {
u64 r3= (TMP(3) - B::iv32 * r2).val();
if CE (t > 3) {
u64 r4= (TMP(4) - B::iv42 * r2 - B::iv43 * r3).val();
if CE (t > 4) a= T(B::m4::mod()) * (TMP(5) - B::iv52 * r2 - B::iv53 * r3 - B::iv54 * r4).val();
a= (a + r4) * B::m3::mod();
}
a= (a + r3) * B::m2::mod();
}
return (a + r2) * B::m1::mod() + r1;
} else return this->dt1[i];
}
#undef TMP
#undef DFT
#undef ZEROS
#undef SET
#undef SET_S
#undef SUBST
#undef ASGN
#undef ASN
#undef BOP
#undef OP
#undef FUNC
#undef REP
};
#define ARR(_) \
using m##_= ModInt<M##_>; \
using ntt##_= NTT<m##_>; \
m##_* dt##_= new m##_[LM];
#define IV2 ST CE m2 iv21= m2(1) / m1::mod();
#define IV3 ST CE m3 iv32= m3(1) / m2::mod(), iv31= iv32 / m1::mod();
#define IV4 ST CE m4 iv43= m4(1) / m3::mod(), iv42= iv43 / m2::mod(), iv41= iv42 / m1::mod();
#define IV5 ST CE m5 iv54= m5(1) / m4::mod(), iv53= iv54 / m3::mod(), iv52= iv53 / m2::mod(), iv51= iv52 / m1::mod();
TP<u8 t, u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM, bool v> struct NB {
ARR(1)
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<2, M1, M2, M3, M4, M5, LM, 0> {
ARR(1) ARR(2) IV2
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<3, M1, M2, M3, M4, M5, LM, 0> {
ARR(1) ARR(2) ARR(3) IV2 IV3
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<4, M1, M2, M3, M4, M5, LM, 0> {
ARR(1) ARR(2) ARR(3) ARR(4) IV2 IV3 IV4
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<5, M1, M2, M3, M4, M5, LM, 0> {
ARR(1) ARR(2) ARR(3) ARR(4) ARR(5) IV2 IV3 IV4 IV5
};
#undef ARR
#define VC(_) \
using m##_= ModInt<M##_>; \
using ntt##_= NTT<m##_>; \
vector<m##_> bf##_; \
m##_* dt##_;
#define RS resize
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<1, M1, M2, M3, M4, M5, LM, 1> {
NB(): dt1(bf1.data()) {}
void RS(int n) { bf1.RS(n), dt1= bf1.data(); }
u32 size() const { return bf1.size(); }
VC(1)
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<2, M1, M2, M3, M4, M5, LM, 1> {
NB(): dt1(bf1.data()), dt2(bf2.data()) {}
void RS(int n) { bf1.RS(n), dt1= bf1.data(), bf2.RS(n), dt2= bf2.data(); }
u32 size() const { return bf1.size(); }
VC(1) VC(2) IV2
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<3, M1, M2, M3, M4, M5, LM, 1> {
NB(): dt1(bf1.data()), dt2(bf2.data()), dt3(bf3.data()) {}
void RS(int n) { bf1.RS(n), dt1= bf1.data(), bf2.RS(n), dt2= bf2.data(), bf3.RS(n), dt3= bf3.data(); }
u32 size() const { return bf1.size(); }
VC(1) VC(2) VC(3) IV2 IV3
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<4, M1, M2, M3, M4, M5, LM, 1> {
NB(): dt1(bf1.data()), dt2(bf2.data()), dt3(bf3.data()), dt4(bf4.data()) {}
void RS(int n) { bf1.RS(n), dt1= bf1.data(), bf2.RS(n), dt2= bf2.data(), bf3.RS(n), dt3= bf3.data(), bf4.RS(n), dt4= bf4.data(); }
u32 size() const { return bf1.size(); }
VC(1) VC(2) VC(3) VC(4) IV2 IV3 IV4
};
TP<u64 M1, u32 M2, u32 M3, u32 M4, u32 M5, u32 LM> struct NB<5, M1, M2, M3, M4, M5, LM, 1> {
NB(): dt1(bf1.data()), dt2(bf2.data()), dt3(bf3.data()), dt4(bf4.data()), dt5(bf5.data()) {}
void RS(int n) { bf1.RS(n), dt1= bf1.data(), bf2.RS(n), dt2= bf2.data(), bf3.RS(n), dt3= bf3.data(), bf4.RS(n), dt4= bf4.data(), bf5.RS(n), dt5= bf5.data(); }
u32 size() const { return bf1.size(); }
VC(1) VC(2) VC(3) VC(4) VC(5) IV2 IV3 IV4 IV5
};
#undef VC
#undef IV2
#undef IV3
#undef IV4
#undef IV5
TP<class T, u32 LM> CE bool is_nttfriend() {
if CE (!is_staticmodint_v<T>) return 0;
else return (T::mod() & is_prime(T::mod())) && LM <= (1ULL << BSF(ll, T::mod() - 1));
}
TP<class T, enable_if_t<is_arithmetic_v<T>, nullptr_t> = nullptr> CE u64 mv() { return numeric_limits<T>::max(); }
TP<class T, enable_if_t<is_staticmodint_v<T>, nullptr_t> = nullptr> CE u64 mv() { return T::mod(); }
TP<class T, u32 LM, u32 M1, u32 M2, u32 M3, u32 M4> CE u8 nt() {
if CE (!is_nttfriend<T, LM>()) {
CE u128 m= mv<T>(), mm= m * m;
if CE (mm <= M1 / LM) return 1;
else if CE (mm <= u64(M1) * M2 / LM) return 2;
else if CE (mm <= u128(M1) * M2 * M3 / LM) return 3;
else if CE (mm <= u128(M1) * M2 * M3 * M4 / LM) return 4;
else return 5;
} else return 1;
}
#undef BSF
#undef RS
CE u32 MOD1= 998244353, MOD2= 897581057, MOD3= 880803841, MOD4= 754974721, MOD5= 645922817;
TP<class T, u32 LM> CE u8 nttarr_type= nt<T, LM, MOD1, MOD2, MOD3, MOD4>();
TP<class T, u32 LM> CE u8 nttarr_cat= is_nttfriend<T, LM>() && (mv<T>() > (1 << 30)) ? 0 : nttarr_type<T, LM>;
TP<class T, u32 LM, bool v> using NTTArray= NI<T, nttarr_type<T, LM>, conditional_t<is_nttfriend<T, LM>(), NB<1, mv<T>(), 0, 0, 0, 0, LM, v>, NB<nttarr_type<T, LM>, MOD1, MOD2, MOD3, MOD4, MOD5, LM, v>>>;
#undef CE
#undef ST
#undef TP
}
using math_internal::is_nttfriend, math_internal::nttarr_type, math_internal::nttarr_cat, math_internal::NTT, math_internal::NTTArray;
template <class T, size_t LM, int id= 0> struct GlobalNTTArray {
static inline NTTArray<T, LM, 0> bf;
};
template <class T, size_t LM, size_t LM2, int id= 0> struct GlobalNTTArray2D {
static inline NTTArray<T, LM, 0>* bf= new NTTArray<T, LM, 0>[LM2];
};
template <class T, size_t LM, int id= 0> struct GlobalArray {
static inline T* bf= new T[LM];
};
constexpr unsigned pw2(unsigned n) { return --n, n|= n >> 1, n|= n >> 2, n|= n >> 4, n|= n >> 8, n|= n >> 16, ++n; }
#line 6 "src/FFT/fps_inv.hpp"
namespace math_internal {
template <u32 LM, class mod_t> inline void inv_base(const mod_t p[], int n, mod_t r[], int i= 1, int l= -1) {
static constexpr int t= nttarr_cat<mod_t, LM>, TH= (int[]){64, 64, 128, 256, 512, 512}[t];
if (n <= i) return;
if (l < 0) l= n;
assert(((n & -n) == n)), assert(i && ((i & -i) == i));
const mod_t miv= -r[0];
for (int j, m= min(n, TH); i < m; r[i++]*= miv)
for (r[i]= mod_t(), j= min(i + 1, l); --j;) r[i]+= r[i - j] * p[j];
static constexpr int lnR= 2 + (!t), R= (1 << lnR) - 1;
using GNA1= GlobalNTTArray<mod_t, LM, 1>;
using GNA2= GlobalNTTArray<mod_t, LM, 2>;
for (auto gt1= GlobalNTTArray2D<mod_t, LM, R, 1>::bf, gt2= GlobalNTTArray2D<mod_t, LM, R, 2>::bf; i < n;) {
mod_t* rr= r;
const mod_t* pp= p;
const int s= i, e= s << 1, ss= (l - 1) / s;
for (int k= 0, j; i < n && k < R; ++k, i+= s, pp+= s) {
if (j= min(e, l - k * s); j > 0) gt2[k].set(pp, 0, j), gt2[k].zeros(j, e), gt2[k].dft(0, e);
for (gt1[k].set(rr, 0, s), gt1[k].zeros(s, e), gt1[k].dft(0, e), GNA2::bf.mul(gt1[k], gt2[0], 0, e), j= min(k, ss) + 1; --j;) GNA1::bf.mul(gt1[k - j], gt2[j], 0, e), GNA2::bf.add(GNA1::bf, 0, e);
GNA2::bf.idft(0, e), GNA2::bf.zeros(0, s);
if constexpr (!is_nttfriend<mod_t, LM>()) GNA2::bf.get(rr, s, e), GNA2::bf.set(rr, s, e);
for (GNA2::bf.dft(0, e), GNA2::bf.mul(gt1[0], 0, e), GNA2::bf.idft(0, e), GNA2::bf.get(rr, s, e), rr+= j= s; j--;) rr[j]= -rr[j];
}
}
}
template <u32 lnR, class mod_t, u32 LM= 1 << 22> void inv_(const mod_t p[], int n, mod_t r[]) {
static constexpr u32 R= (1 << lnR) - 1, LM2= LM >> (lnR - 1);
using GNA1= GlobalNTTArray<mod_t, LM2, 1>;
using GNA2= GlobalNTTArray<mod_t, LM2, 2>;
auto gt1= GlobalNTTArray2D<mod_t, LM2, R, 1>::bf, gt2= GlobalNTTArray2D<mod_t, LM2, R, 2>::bf;
assert(n > 0), assert(p[0] != mod_t());
const int m= pw2(n) >> lnR, m2= m << 1, ed= (n - 1) / m;
inv_base<LM2>(p, m, r);
for (int k= 0, l; k < ed; p+= m) {
for (gt2[k].set(p, 0, l= min(m2, n - m * k)), gt2[k].zeros(l, m2), gt2[k].dft(0, m2), gt1[k].set(r, 0, m), gt1[k].zeros(m, m2), gt1[k].dft(0, m2), GNA2::bf.mul(gt1[k], gt2[0], 0, m2), l= k; l--;) GNA1::bf.mul(gt1[l], gt2[k - l], 0, m2), GNA2::bf.add(GNA1::bf, 0, m2);
GNA2::bf.idft(0, m2), GNA2::bf.zeros(0, m);
if constexpr (!is_nttfriend<mod_t, LM>()) GNA2::bf.get(r, m, m2), GNA2::bf.set(r, m, m2);
for (GNA2::bf.dft(0, m2), GNA2::bf.mul(gt1[0], 0, m2), GNA2::bf.idft(0, m2), GNA2::bf.get(r, m, m + (l= min(m, n - m * ++k))), r+= m; l--;) r[l]= -r[l];
}
}
template <class mod_t, u32 LM= 1 << 22> vector<mod_t> inv(const vector<mod_t>& p) {
static constexpr int t= nttarr_cat<mod_t, LM>, TH= (int[]){234, 106, 280, 458, 603, 861}[t];
mod_t *pp= GlobalArray<mod_t, LM, 1>::bf, *r= GlobalArray<mod_t, LM, 2>::bf;
const int n= p.size();
copy_n(p.begin(), n, pp), assert(n > 0), assert(p[0] != mod_t());
if (const mod_t miv= -(r[0]= mod_t(1) / p[0]); n > TH) {
const int l= pw2(n), l1= l >> 1, k= (n - l1 - 1) / (l1 >> 3), bl= __builtin_ctz(l1);
int a= 4;
if constexpr (!t) a= bl < 8 ? k > 5 ? 1 : 3 : bl < 9 ? k & 1 ? 3 : 4 : bl < 10 ? k & 1 && k > 4 ? 3 : 4 : bl < 11 ? k > 6 ? 3 : 4 : 4;
else if constexpr (t < 2) a= bl < 7 ? 2 : bl < 9 ? k ? 3 : 4 : k & 1 ? 3 : 4;
else if constexpr (t < 3) a= bl < 9 ? k > 5 ? 1 : k ? 3 : 4 : k & 1 ? 3 : 4;
else if constexpr (t < 4) a= bl < 9 ? 1 : bl < 10 ? k > 5 ? 1 : !k ? 4 : k & 2 ? 2 : 3 : k & 1 ? 3 : 4;
else if constexpr (t < 5) a= bl < 10 ? k & 2 ? 2 : 3 : k & 1 ? 3 : 4;
else a= bl < 10 ? 1 : bl < 11 ? k > 5 ? 1 : !k ? 4 : k & 2 ? 2 : 3 : k & 1 ? 3 : 4;
(a < 2 ? inv_<1, mod_t, LM> : a < 3 ? inv_<2, mod_t, LM> : a < 4 ? inv_<3, mod_t, LM> : inv_<4, mod_t, LM>)(pp, n, r);
} else
for (int j, i= 1; i < n; r[i++]*= miv)
for (r[j= i]= mod_t(); j--;) r[i]+= r[j] * pp[i - j];
return vector(r, r + n);
}
}
using math_internal::inv_base, math_internal::inv;
#line 4 "src/Math/mod_sqrt.hpp"
namespace math_internal {
template <class Int, class MP> constexpr i64 inner_sqrt(Int a, Int p) {
const MP md(p);
Int e= (p - 1) >> 1, one= md.set(1);
if (a= md.set(a); md.norm(pow(a, e, md)) != one) return -1;
Int b= 0, d= md.diff(0, a), ret= one, r2= 0, b2= one;
while (md.norm(pow(d, e, md)) == one) b= md.plus(b, one), d= md.diff(md.mul(b, b), a);
auto mult= [&md, d](Int &u1, Int &u2, Int v1, Int v2) {
Int tmp= md.plus(md.mul(u1, v1), md.mul(md.mul(u2, v2), d));
u2= md.plus(md.mul(u1, v2), md.mul(u2, v1)), u1= tmp;
};
for (++e;; mult(b, b2, b, b2)) {
if (e & 1) mult(ret, r2, b, b2);
if (!(e>>= 1)) return ret= md.get(ret), ret * 2 < p ? ret : p - ret;
}
}
}
constexpr long long mod_sqrt(long long a, long long p) {
assert(p > 0), assert(a >= 0), assert(is_prime(p)), a%= p;
if (a <= 1 || p == 2) return a;
if (p < (1 << 30)) return math_internal::inner_sqrt<unsigned, math_internal::MP_Mo32>(a, p);
if (p < (1ll << 62)) return math_internal::inner_sqrt<unsigned long long, math_internal::MP_Mo64>(a, p);
return math_internal::inner_sqrt<unsigned long long, math_internal::MP_D2B1_1>(a, p);
}
#line 4 "src/FFT/fps_sqrt.hpp"
namespace math_internal {
template <size_t LM, class mod_t> void sqrt_base(const mod_t p[], int n, mod_t r[], int l, mod_t v[], mod_t iv[]) {
static constexpr int t= nttarr_cat<mod_t, LM>, TH= (int[]){64, 64, 256, 256, 256, 256}[t];
using GNA1= GlobalNTTArray<mod_t, LM, 1>;
using GNA2= GlobalNTTArray<mod_t, LM, 2>;
using GNA3= GlobalNTTArray<mod_t, LM, 3>;
auto gt1= GlobalNTTArray2D<mod_t, LM, 7, 1>::bf;
assert(n > 1);
const int m= min(n, TH);
const mod_t miv= mod_t(mod_t::mod() >> 1) / r[0];
int i= 2;
for ((r[1]-= p[1])*= miv; i < m; r[i]*= miv, ++i) {
for (int j= (i + 1) / 2; --j;) r[i]+= r[j] * r[i - j];
if (r[i]+= r[i]; !(i & 1)) r[i]+= r[i >> 1] * r[i >> 1];
if (i < l) r[i]-= p[i];
}
if (i == n) return;
int skip= (__builtin_ctz(n / i) + 2) % 3 + 1;
v[0]= -r[0] - r[0], iv[0]= miv;
for (int ed= (1 << skip) - 1, u= 1; i < n; ed= 7) {
mod_t* rr= r;
const mod_t* pp= p;
const int s= i, e= s << 1;
for (int j= u; j < s; ++j) v[j]= -r[j] - r[j];
inv_base<LM>(v, s, iv, u), u= s;
GNA1::bf.set(iv, 0, s), GNA1::bf.zeros(s, e), GNA1::bf.dft(0, e);
for (int k= 0, j; i < n && k < ed; ++k, i+= s) {
gt1[k].set(rr, 0, s), gt1[k].zeros(s, e), gt1[k].dft(0, e);
for (GNA2::bf.zeros(0, e), j= k >> 1; j--;) GNA3::bf.add(gt1[j + 1], gt1[j], 0, s), GNA3::bf.dif(gt1[j + 1], gt1[j], s, e), GNA3::bf.mul(gt1[k - j], 0, e), GNA2::bf.add(GNA3::bf, 0, e);
if (j= (k + 1) >> 1; k & 1) GNA3::bf.mul(gt1[j], gt1[k - j], 0, e), GNA2::bf.add(GNA3::bf, 0, s), GNA2::bf.dif(GNA3::bf, s, e);
if (k) GNA2::bf.add(GNA2::bf, 0, e);
GNA3::bf.mul(gt1[j], gt1[j], 0, e);
k & 1 ? GNA2::bf.add(GNA3::bf, s, e) : GNA2::bf.dif(GNA3::bf, s, e);
GNA2::bf.add(GNA3::bf, 0, s), GNA2::bf.idft(0, e), GNA2::bf.zeros(s, e), GNA2::bf.get(rr+= s, 0, s);
if (j= min(s, l - s * k); j > 0)
for (pp+= s; j--;) rr[j]-= pp[j];
GNA2::bf.set(rr, 0, s), GNA2::bf.dft(0, e), GNA2::bf.mul(GNA1::bf, 0, e), GNA2::bf.idft(0, e), GNA2::bf.get(rr, 0, min(s, n - s * k));
}
}
}
template <class mod_t, size_t LM= 1 << 22> vector<mod_t> sqrt(const vector<mod_t>& p) {
mod_t *r= GlobalArray<mod_t, LM, 1>::bf, *v= GlobalArray<mod_t, LM, 2>::bf, *iv= GlobalArray<mod_t, LM, 3>::bf;
static constexpr size_t LM2= LM >> 2;
int n= p.size(), cnt= 0;
while (cnt < n && p[cnt] == mod_t()) cnt++;
if (cnt == n) return p;
if (cnt & 1) return {}; // no solution
const int nn= n - (cnt >> 1), l= n - cnt;
const mod_t* pp= p.data() + cnt;
mod_t* rr= r + (cnt >> 1);
fill_n(r, n, mod_t());
if (rr[0]= mod_sqrt(pp[0].val(), mod_t::mod()); rr[0] * rr[0] != pp[0]) return {}; // no solution
sqrt_base<LM2, mod_t>(pp, nn, rr, l, v, iv);
return vector(r, r + n);
}
}
using math_internal::sqrt;