// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/6/NTL/all/NTL_2_A
#include "../../../src/math/ds/mpi/lib.hpp"
using namespace tifa_libs;
int main() {
std::cin.tie(nullptr)->std::ios::sync_with_stdio(false);
mpi a, b;
std::cin >> a >> b;
std::cout << a + b << '\n';
return 0;
}
#line 1 "test/cpv/aizu-ntl/ntl_2_a.cpp"
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/6/NTL/all/NTL_2_A
#line 2 "src/math/ds/mpi/lib.hpp"
#line 2 "src/conv/add/u128/lib.hpp"
#line 2 "src/math/ds/mint/ms/lib.hpp"
#line 2 "src/nt/mod/montgomery/lib.hpp"
#line 2 "src/util/alias/num/lib.hpp"
#line 2 "src/util/util/lib.hpp"
// https://github.com/Tiphereth-A/CP-lib
#include <bits/extc++.h>
// clang-format off
namespace tifa_libs {
#define CEXP constexpr
#define CEXPE constexpr explicit
#define CR const&
#define CP const*
#define PC *const
#define CPC const*const
#define TPN typename
#define NE noexcept
#define CNE const noexcept
#define ND [[nodiscard]]
#define cT_(...) std::conditional_t<sizeof(__VA_ARGS__) <= sizeof(size_t) * 2, __VA_ARGS__, __VA_ARGS__ CR>
// NOLINTNEXTLINE(misc-const-correctness)
#define flt_(T, i, l, r, ...) for (T i = (l), i##e = (r)__VA_OPT__(, ) __VA_ARGS__; i < i##e; ++i)
#define retif_(cond, if_true, ...) if cond return if_true __VA_OPT__(; else return __VA_ARGS__)
#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif
using namespace std::ranges;
using namespace std::literals;
template <class T>
CEXP T abs(T x) NE { retif_((x < 0), -x, x); }
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/alias/num/lib.hpp"
// clang-format off
namespace tifa_libs {
#define mk0_(w, t) using w = t; using c##w = const t
#define mk_(w, t) mk0_(w, t); CEXP w operator""_##w(unsigned long long x) NE { return (w)x; }
mk_(i8, int8_t) mk_(u8, uint8_t) mk_(i16, int16_t) mk_(u16, uint16_t) mk_(i32, int32_t) mk_(u32, uint32_t) mk_(i64, int64_t) mk_(u64, uint64_t) mk_(isz, ssize_t) mk_(usz, size_t) mk_(chr, char) mk_(schr, signed char) mk_(uchr, unsigned char) mk_(sint, signed) mk_(uint, unsigned);
mk0_(i128, __int128_t); mk0_(u128, __uint128_t); mk0_(f32, float); mk0_(f64, double); mk0_(f128, long double);
#undef mk0_
#undef mk_
} // namespace tifa_libs
// clang-format on
#line 4 "src/nt/mod/montgomery/lib.hpp"
namespace tifa_libs {
template <u32 MOD>
struct montgomery {
static CEXP u32 MOD2 = MOD << 1, R2 = -(u64)(MOD) % MOD, R = [] {
u32 iv = MOD * (2 - MOD * MOD);
iv *= 2 - MOD * iv, iv *= 2 - MOD * iv;
return iv * (MOD * iv - 2);
}();
static_assert(MOD & 1);
static_assert(-R * MOD == 1);
static_assert((MOD >> 30) == 0);
static_assert(MOD != 1);
static CEXP u32 reduce(u64 x) NE { return u32((x + u64((u32)x * R) * MOD) >> 32); }
static CEXP u32 norm(u32 x) NE { return x - (MOD & -((MOD - 1 - x) >> 31)); }
};
template <> // dynamic
struct montgomery<0> {
u32 R, R2, MOD, MOD_ODD, OFFSET, MASK;
CEXP montgomery() NE = default;
CEXPE montgomery(u32 m) NE { reset(m); }
CEXP void reset(u32 m) NE {
for (assert(!(m == 1 || m >> 31)), MOD = MOD_ODD = m, OFFSET = 0; (MOD_ODD & 1) == 0; ++OFFSET, MOD_ODD /= 2);
MASK = (1_u32 << OFFSET) - 1_u32;
u32 iv = MOD_ODD * (2 - MOD_ODD * MOD_ODD);
iv *= 2 - MOD_ODD * iv, iv *= 2 - MOD_ODD * iv, R = iv * (MOD_ODD * iv - 2), R2 = u32(-u64(MOD_ODD) % MOD_ODD);
}
ND CEXP u32 norm(i32 x) CNE { return u32(x + (-(x < 0) & (i32)MOD)); }
ND CEXP u32 reduce(u64 x) CNE {
cu32 t = u32((x + u64((u32)x * R) * MOD_ODD) >> 32);
return t - (MOD_ODD & -((MOD_ODD - 1 - t) >> 31));
}
ND CEXP u32 tsf(u32 x) CNE { retif_((!OFFSET) [[likely]], reduce(u64(x) * R2), reduce(u64(x % MOD_ODD) * R2) << OFFSET | (x & MASK)); }
};
} // namespace tifa_libs
#line 2 "src/math/ds/mint/_base/lib.hpp"
#line 2 "src/nt/inverse/lib.hpp"
#line 2 "src/nt/gl/inv_gcd/lib.hpp"
#line 2 "src/math/safe_mod/lib.hpp"
#line 2 "src/util/traits/math/lib.hpp"
// clang-format off
#line 4 "src/util/traits/math/lib.hpp"
namespace tifa_libs {
template <class T> concept char_c = std::same_as<T, char> || std::same_as<T, signed char> || std::same_as<T, unsigned char>;
#pragma GCC diagnostic ignored "-Wpedantic"
template <class T> concept s128_c = std::same_as<T, __int128_t> || std::same_as<T, __int128>;
template <class T> concept u128_c = std::same_as<T, __uint128_t> || std::same_as<T, unsigned __int128>;
template <class T> concept i128_c = s128_c<T> || u128_c<T>;
#pragma GCC diagnostic warning "-Wpedantic"
template <class T> concept imost64_c = std::integral<T> && sizeof(T) * __CHAR_BIT__ <= 64;
template <class T> concept smost64_c = imost64_c<T> && std::signed_integral<T>;
template <class T> concept umost64_c = imost64_c<T> && std::unsigned_integral<T>;
template <class T> concept int_c = i128_c<T> || imost64_c<T>;
template <class T> concept sint_c = s128_c<T> || smost64_c<T>;
template <class T> concept uint_c = u128_c<T> || umost64_c<T>;
template <class T> concept arithm_c = std::is_arithmetic_v<T> || int_c<T>;
template <class T> concept mint_c = requires(T x) { {x.mod()} -> uint_c; {x.val()} -> uint_c; };
template <class T> concept dft_c = requires(T x, std::vector<TPN T::data_t> v, u32 n) { {x.size()} -> std::same_as<u32>; x.bzr(n); x.dif(v, n); x.dit(v, n); };
template <class T> concept ntt_c = dft_c<T> && requires(T x) { T::max_size; T::G; };
template <class T> struct to_sint : std::make_signed<T> {};
template <> struct to_sint<u128> { using type = i128; };
template <> struct to_sint<i128> { using type = i128; };
template <class T> using to_sint_t = TPN to_sint<T>::type;
template <class T> struct to_uint : std::make_unsigned<T> {};
template <> struct to_uint<u128> { using type = u128; };
template <> struct to_uint<i128> { using type = u128; };
template <class T> using to_uint_t = TPN to_uint<T>::type;
template <arithm_c T> struct to_bigger : std::make_unsigned<T> {};
#define _(w,ww) template <> struct to_bigger<w> { using type = ww; }
#define _2(w,ww) _(i##w,i##ww); _(u##w,u##ww);
_2(8, 16); _2(16, 32); _2(32, 64); _2(64, 128); _(f32, f64); _(f64, f128);
#undef _2
#undef _
template <class T> using to_bigger_t = TPN to_bigger<T>::type;
template <arithm_c T> CEXP T inf_v = [] {
if CEXP(sint_c<T>) return T(to_uint_t<T>(-1) / 4 - 1);
else if CEXP(uint_c<T>) return T(-1) / 2 - 1;
else return std::numeric_limits<T>::max() / 2 - 1;
}();
} // namespace tifa_libs
// clang-format on
#line 4 "src/math/safe_mod/lib.hpp"
namespace tifa_libs {
template <int_c T>
CEXP T safe_mod(T x, to_uint_t<T> mod) NE {
if CEXP (sint_c<T>) {
if (x <= -(T)mod || x >= (T)mod) x %= (T)mod;
retif_((x < 0), x + (T)mod, x);
} else {
retif_((x >= mod), x % mod, x);
}
}
} // namespace tifa_libs
#line 2 "src/util/alias/others/lib.hpp"
#line 2 "src/util/consts/lib.hpp"
#line 4 "src/util/consts/lib.hpp"
// clang-format off
namespace tifa_libs {
using std::numbers::pi_v;
template <std::floating_point FP>
inline FP eps_v = std::sqrt(std::numeric_limits<FP>::epsilon());
template <std::floating_point FP>
CEXP void set_eps(FP v) NE { eps_v<FP> = v; }
CEXP u32 TIME = ((__TIME__[0] & 15) << 20) | ((__TIME__[1] & 15) << 16) | ((__TIME__[3] & 15) << 12) | ((__TIME__[4] & 15) << 8) | ((__TIME__[6] & 15) << 4) | (__TIME__[7] & 15);
CEXP auto STR2U16 = [] { std::array<u32, 65536> table{}; table.fill(-1_u32); flt_ (u32, i, 48, 58) flt_ (u32, j, 48, 58) table[i << 8 | j] = (j & 15) * 10 + (i & 15); return table; }();
inline const auto fn_0 = [](auto&&...) NE {};
inline const auto fn_is0 = [](auto x) NE { return x == 0; };
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/alias/others/lib.hpp"
namespace tifa_libs {
template <class T>
struct chash {
CEXP static u64 C = u64(pi_v<f128> * 2e18) | 71;
CEXP u64 operator()(T x) CNE { return __builtin_bswap64(((u64)x ^ TIME) * C); }
};
// clang-format off
#define mk_(w, t) using w = t; using c##w = const t;
mk_(strn, std::string) mk_(strnv, std::string_view)
#undef mk_
template <class T> struct edge_t { T w; u32 u, v; CEXP auto operator<=>(edge_t CR) const = default; }; template <class T> using cedge_t = const edge_t<T>;
template <class T> struct pt3 { T _0, _1, _2; CEXP auto operator<=>(pt3 CR) const = default; }; template <class T> using cpt3 = const pt3<T>;
template <class T> struct pt4 { T _0, _1, _2, _3; CEXP auto operator<=>(pt4 CR) const = default; }; template <class T> using cpt4 = const pt4<T>;
#define mkT_(w, t, ...) template <class T> using w = t __VA_OPT__(, ) __VA_ARGS__; template <class T> using c##w = const t __VA_OPT__(, ) __VA_ARGS__;
mkT_(ptt, std::pair<T, T>) mkT_(alc, std::pmr::polymorphic_allocator<T>) mkT_(vec, std::vector<T>) mkT_(vvec, vec<vec<T>>) mkT_(v3ec, vvec<vec<T>>) mkT_(vecpt, vec<ptt<T>>) mkT_(vvecpt, vvec<ptt<T>>) mkT_(ptvec, ptt<vec<T>>) mkT_(ptvvec, ptt<vvec<T>>)
#undef mkT_
template <class T> using itl = std ::initializer_list<T>;
template <class T, usz ext = std::dynamic_extent> using spn = std::span<T const, ext>;
template <class T, usz N> using arr = std::array<T, N>; template <class T, usz N> using carr = std::array<const T, N>;
template <class U, class T> using vecp = vec<std::pair<U, T>>; template <class U, class T> using vvecp = vvec<std::pair<U, T>>;
template <class U, class T> using vvecp = vvec<std::pair<U, T>>; template <class U, class T> using vvvecp = vvec<vvec<std::pair<U, T>>>;
#ifdef PB_DS_ASSOC_CNTNR_HPP
template <class T, class C = std::less<T>> using set = __gnu_pbds::tree<T, __gnu_pbds::null_type, C>;
template <class K, class V, class C = std::less<K>> using map = __gnu_pbds::tree<K, V, C>;
// hset<u64> s({}, {}, {}, {}, {1<<16});
template <class T, class HF = chash<T>> using hset = __gnu_pbds::gp_hash_table<T, __gnu_pbds::null_type, HF>;
// hmap<u64, int> s({}, {}, {}, {}, {1<<16});
template <class K, class V, class HF = chash<K>> using hmap = __gnu_pbds::gp_hash_table<K, V, HF>;
#else
using std::set, std::map;
template <class T, class HF = chash<T>> using hset = std::unordered_set<T, HF>;
template <class K, class V, class HF = chash<K>> using hmap = std::unordered_map<K, V, HF>;
#endif
#ifdef PB_DS_PRIORITY_QUEUE_HPP
template <class T, class C = std::less<T>> using pq = __gnu_pbds::priority_queue<T, C>;
#else
template <class T, class C = std::less<T>> using pq = std::priority_queue<T, vec<T>, C>;
#endif
template <class T> using pqg = pq<T, std::greater<T>>;
// clang-format on
#define mk1_(V, A, T) using V##A = V<T>;
#define mk_(V, A, T) mk1_(V, A, T) mk1_(c##V, A, T)
#define mk(A, T) mk_(edge_t, A, T) mk_(ptt, A, T) mk_(pt3, A, T) mk_(pt4, A, T) mk_(vec, A, T) mk_(vvec, A, T) mk_(v3ec, A, T) mk_(vecpt, A, T) mk_(vvecpt, A, T) mk_(ptvec, A, T) mk_(ptvvec, A, T) mk1_(spn, A, T) mk1_(itl, A, T)
mk(b, bool) mk(c, chr) mk(i, i32) mk(u, u32) mk(ii, i64) mk(uu, u64) mk(t, isz) mk(z, usz) mk(f, f32) mk(d, f64) mk(s, strn);
#undef mk
#undef mk_
#undef mk1_
} // namespace tifa_libs
#line 2 "src/nt/gl/exgcd/lib.hpp"
#line 4 "src/nt/gl/exgcd/lib.hpp"
namespace tifa_libs {
// Binary exgcd
template <uint_c U, bool only_x = false>
CEXP auto exgcd_b(U a, U b) NE {
using T = to_sint_t<U>;
if CEXP (only_x) {
if (!a) return std::make_tuple(b, (T)0);
if (!b) return std::make_tuple(a, (T)1);
} else {
if (!a) return std::make_tuple(b, (T)0, (T) !!b);
if (!b) return std::make_tuple(a, (T)1, (T)0);
}
auto r = std::__countr_zero(a | b);
a >>= r, b >>= r;
T x = (T)a, y = (T)b, s = 1, t = 0, u = 0, v = 1;
while (x) {
while (!(x & 1))
if (x /= 2; !((s | t) & 1)) s /= 2, t /= 2;
else s = (s + (T)b) / 2, t = (t - (T)a) / 2;
while (!(y & 1))
if (y /= 2; !((u | v) & 1)) u /= 2, v /= 2;
else u = (u + (T)b) / 2, v = (v - (T)a) / 2;
if (x >= y) x -= y, s -= u, t -= v;
else y -= x, u -= s, v -= t;
}
if (y > 1) a /= (U)y, b /= (U)y;
if (a && (U)abs(v) >= a) {
const T _ = v / (T)a;
v -= _ * (T)a, u += _ * (T)b;
}
if (b && (U)abs(u) >= b) {
const T _ = u / (T)b;
u -= _ * (T)b, v += _ * (T)a;
}
if (const T u_ = u + (T)b, v_ = v - (T)a; abs(u_) + abs(v_) <= abs(u) + abs(v)) u = u_, v = v_;
if (const T u_ = u - (T)b, v_ = v + (T)a; abs(u_) + abs(v_) <= abs(u) + abs(v)) u = u_, v = v_;
if CEXP (only_x) return std::make_tuple(U(y << r), u);
else return std::make_tuple(U(y << r), u, v);
}
// @return then return tuple(g, x[, y]) s.t. g = gcd(a, b), xa + yb = g, |x| + |y| is the minimal (primary) and x <= y (secondarily)
template <sint_c T, bool only_x = false>
CEXP auto exgcd(T a, T b) NE {
using U = to_uint_t<T>;
if (auto [x, y] = minmax(a, b); x >= 0 && y <= T(U(-1) >> sizeof(U))) return exgcd_b<U, only_x>((U)a, (U)b);
if CEXP (only_x) {
T s = 1, u = 0;
while (b) {
T c = a / b;
std::tie(s, u, a, b) = std::make_tuple(u, s - u * c, b, a - b * c);
}
return std::make_tuple((U)a, s);
} else {
T s = 1, t = 0, u = 0, v = 1;
while (b) {
T c = a / b;
std::tie(s, t, u, v, a, b) = std::make_tuple(u, v, s - u * c, t - v * c, b, a - b * c);
}
return std::make_tuple((U)a, s, t);
}
}
} // namespace tifa_libs
#line 6 "src/nt/gl/inv_gcd/lib.hpp"
namespace tifa_libs {
template <uint_c T>
CEXP ptt<T> inv_gcd(T n, T mod) NE {
using U = to_sint_t<T>;
auto [g, x] = exgcd<U, true>(U(n % mod), (U)mod);
return {g, safe_mod(x, mod)};
}
} // namespace tifa_libs
#line 4 "src/nt/inverse/lib.hpp"
namespace tifa_libs {
// simple but slower: inv(n, mod) -> 1 < n ? mod - inv(mod % n, n) * mod / n : 1;
template <uint_c T, uint_c U>
CEXP U inverse(T n, U mod) NE {
auto [g, x] = inv_gcd(U(n % mod), mod);
assert(g == 1);
return x;
}
} // namespace tifa_libs
#line 2 "src/util/traits/others/lib.hpp"
// clang-format off
#line 4 "src/util/traits/others/lib.hpp"
namespace tifa_libs {
//! only for template without non-type argument
template <class, template <class...> class> CEXP bool specialized_from_v = false;
template <template <class...> class T, class... Args> CEXP bool specialized_from_v<T<Args...>, T> = true;
static_assert(specialized_from_v<vecu, std::vector>);
template <class T> concept container_c = common_range<T> && !std::is_array_v<std::remove_cvref_t<T>> && !std::same_as<std::remove_cvref_t<T>, strn> && !std::same_as<std::remove_cvref_t<T>, strnv>;
template <class T> concept istream_c = std::derived_from<T, std::istream> || std::derived_from<T, std::wistream> || requires(T is) { is.peek(); };
template <class T> concept ostream_c = std::derived_from<T, std::ostream> || std::derived_from<T, std::wostream> || requires(T os) { os.flush(); };
} // namespace tifa_libs
// clang-format on
#line 5 "src/math/ds/mint/_base/lib.hpp"
namespace tifa_libs::mint_impl_ {
struct mint_tag_base {};
template <std::derived_from<mint_tag_base> tag_t>
struct mint : tag_t {
CEXP mint() = default;
CEXP mint(int_c auto v) NE : tag_t(v) {}
using raw_t = tag_t::raw_t;
using sraw_t = to_sint_t<raw_t>;
static CEXP sraw_t smod() NE { return (sraw_t)tag_t::mod(); }
ND CEXP sraw_t sval() CNE { return (sraw_t)tag_t::val(); }
template <int_c T>
CEXPE operator T() CNE { return (T)tag_t::val(); }
CEXP mint& operator+=(mint CR r) NE {
mint::add(r);
return *this;
}
CEXP mint& operator-=(mint CR r) NE {
mint::sub(r);
return *this;
}
CEXP mint& operator*=(mint CR r) NE {
mint::mul(r);
return *this;
}
CEXP mint& operator/=(mint CR r) NE { return *this = *this * r.inv(); }
CEXP mint CR operator+() CNE { return *this; }
CEXP mint operator-() CNE { return tag_t::template neg<mint>(); }
ND CEXP mint inv() CNE { return inverse(tag_t::val(), tag_t::mod()); }
friend CEXP mint operator+(mint l, mint CR r) NE { return l += r; }
friend CEXP mint operator-(mint l, mint CR r) NE { return l -= r; }
friend CEXP mint operator*(mint l, mint CR r) NE { return l *= r; }
friend CEXP mint operator/(mint l, mint CR r) NE { return l /= r; }
friend CEXP bool operator==(mint CR l, mint CR r) NE { return l.val() == r.val(); }
friend CEXP auto operator<=>(mint CR l, mint CR r) NE { return l.sval() <=> r.sval(); }
friend auto& operator>>(istream_c auto& is, mint& x) NE {
i64 _;
is >> _, x = mint(_);
return is;
}
friend auto& operator<<(ostream_c auto& os, mint CR x) NE { return os << x.val(); }
friend CEXP auto abs(mint CR x) NE { return x.val(); }
};
} // namespace tifa_libs::mint_impl_
#line 5 "src/math/ds/mint/ms/lib.hpp"
namespace tifa_libs {
template <u64 MOD_>
class mint_ms_tag : public mint_impl_::mint_tag_base {
static_assert(MOD_ <= UINT32_MAX);
using core = montgomery<MOD_>;
public:
static CEXP bool FIXED_MOD = true;
protected:
using raw_t = u32;
raw_t v_{};
CEXP mint_ms_tag() NE = default;
CEXP mint_ms_tag(int_c auto v) NE : v_{mod(v)} {}
public:
static CEXP raw_t mod(sint_c auto v) NE {
if CEXP (smost64_c<decltype(v)>) {
retif_((v >= 0 && (u64)v < mod()) [[likely]], core::reduce(u64((raw_t)v) * core::R2));
}
return core::reduce(u64(i32(v % (i32)mod()) + (i32)mod()) * core::R2);
}
static CEXP raw_t mod(uint_c auto v) NE {
if CEXP (umost64_c<decltype(v)>) {
retif_((cu64 x = (u64)v; x < mod()) [[likely]], core::reduce(x * core::R2), core::reduce(u64(x % mod()) * core::R2));
} else retif_((v < mod()) [[likely]], core::reduce(u64((raw_t)v) * core::R2), core::reduce(u64((raw_t)(v % mod())) * core::R2));
}
static CEXP raw_t mod() NE { return MOD_; }
ND CEXP raw_t val() CNE { return core::norm(core::reduce(v_)); }
CEXP raw_t& data() NE { return v_; }
protected:
template <class mint>
ND CEXP auto neg() CNE {
mint res;
res.v_ = (core::MOD2 & -raw_t(v_ != 0)) - v_;
return res;
}
CEXP void add(mint_ms_tag CR r) NE { v_ += r.v_ - core::MOD2, v_ += core::MOD2 & -(v_ >> 31); }
CEXP void sub(mint_ms_tag CR r) NE { v_ -= r.v_, v_ += core::MOD2 & -(v_ >> 31); }
CEXP void mul(mint_ms_tag CR r) NE { v_ = core::reduce(u64(v_) * r.v_); }
};
template <u64 MOD>
using mint_ms = mint_impl_::mint<mint_ms_tag<MOD>>;
} // namespace tifa_libs
#line 2 "src/conv/trans/ntt/lib.hpp"
#line 2 "src/math/qpow/basic/lib.hpp"
#line 4 "src/math/qpow/basic/lib.hpp"
namespace tifa_libs {
template <class T>
CEXP T qpow(T a, u64 b, cT_(T) init_v = T{1}) NE {
T res = init_v;
for (; b; b >>= 1, a = a * a) {
while (!(b & 1)) b >>= 1, a = a * a;
res = res * a;
}
return res;
}
} // namespace tifa_libs
#line 2 "src/nt/proot/uint/lib.hpp"
#line 2 "src/nt/pfactors/lib.hpp"
#line 2 "src/edh/discretization/lib.hpp"
#line 2 "src/fast/rsort32/lib.hpp"
#line 4 "src/fast/rsort32/lib.hpp"
namespace tifa_libs {
template <class C>
requires(std::is_array_v<C> && std::integral<decltype(std::declval<C>()[0])> && sizeof(std::declval<C>()[0]) == 4) || (std::contiguous_iterator<TPN C::iterator> && std::integral<TPN C::value_type> && sizeof(TPN C::value_type) == 4)
void rsort32(C& a) NE {
if (a.size() <= 1) return;
if (a.size() <= 200'000) {
std::ranges::sort(a);
return;
}
arr<u32, 256> _0{}, _1{}, _2{}, _3{};
cu32 n = (u32)a.size();
vecu b(n);
u32 *a_ = (u32*)a.data(), *b_ = (u32*)b.data();
for (cu32 *_ = a_ + n, *i = a_; i < _; ++i) ++_0[*i & 255], ++_1[*i >> 8 & 255], ++_2[*i >> 16 & 255], ++_3[*i >> 24 & 255];
flt_ (u32, i, 1, 256) _0[i] += _0[i - 1], _1[i] += _1[i - 1], _2[i] += _2[i - 1], _3[i] += _3[i - 1];
for (u32 CP i = a_ + n; --i >= a_;) b_[--_0[*i & 255]] = *i;
for (u32 CP i = b_ + n; --i >= b_;) a_[--_1[*i >> 8 & 255]] = *i;
for (u32 CP i = a_ + n; --i >= a_;) b_[--_2[*i >> 16 & 255]] = *i;
for (u32 CP i = b_ + n; --i >= b_;) a_[--_3[*i >> 24 & 255]] = *i;
if CEXP (std::is_signed_v<TPN C::value_type>) {
u32 i = n;
while (i && a[i - 1] < 0) --i;
rotate(a_, a_ + n, a_ + i);
}
}
template <class C>
requires(std::is_array_v<C> && std::integral<decltype(std::declval<C>()[0])> && sizeof(std::declval<C>()[0]) == 4) || range<C>
void sort(C& a) NE {
if CEXP (std::is_array_v<C> || (std::contiguous_iterator<TPN C::iterator> && std::integral<TPN C::value_type> && sizeof(TPN C::value_type) == 4)) rsort32(a);
else std::ranges::sort(a);
}
} // namespace tifa_libs
#line 4 "src/edh/discretization/lib.hpp"
namespace tifa_libs {
template <common_range T>
CEXP T uniq(T v) NE {
tifa_libs::sort(v);
auto r = unique(begin(v), end(v));
return {begin(v), begin(r)};
}
template <common_range T>
CEXP std::pair<T, vecu> gen_id(T CR v) NE {
const T _ = uniq(v);
vecu _1;
_1.reserve(v.size());
flt_ (u32, i, 0, (u32)v.size()) _1.push_back(u32(lower_bound(_, v[i]) - begin(_)));
return {_, _1};
}
} // namespace tifa_libs
#line 2 "src/math/mul_mod/lib.hpp"
#line 4 "src/math/mul_mod/lib.hpp"
namespace tifa_libs {
CEXP i64 mul_mod_s(i64 a, i64 b, u64 mod) NE {
if (std::bit_width((u64)abs(a)) + std::bit_width((u64)abs(b)) < 64) return safe_mod(a * b % (i64)mod, mod);
return safe_mod((i64)((i128)a * b % mod), mod);
}
CEXP u64 mul_mod_u(u64 a, u64 b, u64 mod) NE {
if (std::bit_width(a) + std::bit_width(b) <= 64) return a * b % mod;
return (u64)((u128)a * b % mod);
}
} // namespace tifa_libs
#line 2 "src/util/rand/lib.hpp"
#line 5 "src/util/rand/lib.hpp"
namespace tifa_libs {
template <class T>
requires std::is_arithmetic_v<T>
class rand_gen {
using res_t = std::conditional_t<sizeof(T) <= 4, u32, u64>;
using res_wt = std::conditional_t<sizeof(T) <= 4, u64, u128>;
// clang-format off
struct mt19937_param { static CEXP u32 w = 32, n = 624, m = 397, r = 31, a = 0x9908b0df, u = 11, d = 0xffffffff, s = 7, b = 0x9d2c5680, t = 15, c = 0xefc60000, l = 18, f = 1812433253; };
struct mt19937_64_param { static CEXP u64 w = 64, n = 312, m = 156, r = 31, a = 0xb5026f5aa96619e9, u = 29, d = 0x5555555555555555, s = 17, b = 0x71d67fffeda60000, t = 37, c = 0xfff7eee000000000, l = 43, f = 6364136223846793005; };
using pm = std::conditional_t<std::is_same_v<res_t, u32>, mt19937_param, mt19937_64_param>;
// clang-format on
T a_, b_;
arr<res_t, pm::n> x_;
u32 p_;
CEXP void gen_() NE {
CEXP res_t um = (~res_t()) << pm::r, lm = ~um;
res_t _;
flt_ (res_t, i, p_ = 0, pm::n - pm::m) _ = ((x_[i] & um) | (x_[i + 1] & lm)), x_[i] = (x_[i + pm::m] ^ (_ >> 1) ^ ((_ & 1) ? pm::a : 0));
flt_ (res_t, i, pm::n - pm::m, pm::n - 1) _ = ((x_[i] & um) | (x_[i + 1] & lm)), x_[i] = (x_[i + (pm::m - pm::n)] ^ (_ >> 1) ^ ((_ & 1) ? pm::a : 0));
_ = ((x_[pm::n - 1] & um) | (x_[0] & lm)), x_[pm::n - 1] = (x_[pm::m - 1] ^ (_ >> 1) ^ ((_ & 1) ? pm::a : 0));
}
public:
CEXPE rand_gen(T a = std::numeric_limits<T>::min(), T b = std::numeric_limits<T>::max(), res_t sd = (res_t)TIME) NE : a_(a), b_(b) { assert(a < b || (std::is_integral_v<T> && a == b)), seed(sd); }
CEXP void range(T min, T max) NE { assert(min < max || (std::is_integral_v<T> && min == max)), a_ = min, b_ = max; }
void seed() NE { seed((res_t)std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); }
CEXP void seed(res_t sd) NE {
x_[0] = sd & gen_max();
flt_ (res_t, i, 1, p_ = pm::n) x_[i] = ((x_[i - 1] ^ (x_[i - 1] >> (pm::w - 2))) * pm::f + i % pm::n) & gen_max();
}
ND CEXP res_t gen_min() CNE { return 0; }
ND CEXP res_t gen_max() CNE {
if CEXP (sizeof(res_t) * 8 == pm::w) return ~res_t();
else return ((res_t)1 << pm::w) - 1;
}
CEXP res_t next() NE {
if (p_ >= pm::n) gen_();
res_t _ = x_[p_++];
_ ^= (_ >> pm::u) & pm::d, _ ^= (_ << pm::s) & pm::b, _ ^= (_ << pm::t) & pm::c, _ ^= (_ >> pm::l);
return _;
}
CEXP T operator()() NE {
if CEXP (std::integral<T>) {
const res_wt r = (res_wt)b_ - (res_wt)a_ + 1;
res_wt p = r * next();
if (auto l = (res_t)p, _ = res_t(res_wt(-(res_t)r) % r); l < r)
while (l < _) l = res_t(p = r * next());
return T((res_t)(p >> pm::w) + (res_t)a_);
} else return T(next() / (f128)((u128)gen_max() + 1) * (b_ - a_) + a_);
}
};
} // namespace tifa_libs
#line 2 "src/nt/gl/gcd/lib.hpp"
#line 4 "src/nt/gl/gcd/lib.hpp"
namespace tifa_libs {
namespace gcd_impl_ {
template <uint_c T, uint_c U>
CEXP std::common_type_t<T, U> gcd__(T u, U v) NE {
using W = std::common_type_t<T, U>;
retif_((!u || !v) [[unlikely]], u ^ v);
const auto k = std::__countr_zero(u | v);
u >>= k, v >>= k;
do {
if (W const _ = v >> std::__countr_zero(v); u > _) v = u - _, u = _;
else v = _ - u;
} while (v);
return u << k;
}
} // namespace gcd_impl_
template <int_c T, int_c U>
CEXP auto gcd(T a, U b) NE { return gcd_impl_::gcd__((to_uint_t<T>)abs(a), (to_uint_t<U>)abs(b)); }
} // namespace tifa_libs
#line 2 "src/nt/is_prime/lib.hpp"
#line 2 "src/math/qpow/mod/lib.hpp"
#line 4 "src/math/qpow/mod/lib.hpp"
namespace tifa_libs {
CEXP u64 qpow_mod(u64 a, u64 b, u64 mod) NE {
u64 res(1);
for (a %= mod; b; b >>= 1, a = mul_mod_u(a, a, mod)) {
while (!(b & 1)) b >>= 1, a = mul_mod_u(a, a, mod);
res = mul_mod_u(res, a, mod);
}
return res;
}
} // namespace tifa_libs
#line 5 "src/nt/is_prime/lib.hpp"
namespace tifa_libs {
CEXP bool is_prime(u64 n) NE {
retif_((n <= 2) [[unlikely]], n == 2);
if (~n & 1) return false;
if (n < 8 || n == 61) return true;
if (!(n % 3) || !(n % 5) || !(n % 7)) return false;
if (n < 121) return true;
auto f = [n, d = (n - 1) >> std::countr_zero(n - 1)](auto&& bases) NE -> bool {
for (cu64 i : bases) {
if (!(i % n)) continue;
u64 t = d, y = qpow_mod(i, t, n);
while (t != n - 1 && y != 1 && y != n - 1) y = mul_mod_u(y, y, n), t *= 2;
if (y != n - 1 && (~t & 1)) return false;
}
return true;
};
// NOLINTBEGIN(modernize-avoid-c-arrays)
CEXP u64 THRES1 = 341531, BASE1[]{9345883071009581737u};
CEXP u64 THRES2 = 1050535501, BASE2[]{336781006125, 9639812373923155};
CEXP u64 THRES3 = 350269456337, BASE3[]{4230279247111683200, 14694767155120705706u, 16641139526367750375u};
CEXP u64 THRES4 = 55245642489451, BASE4[]{2, 141889084524735, 1199124725622454117, 11096072698276303650u};
CEXP u64 THRES5 = 7999252175582851, BASE5[]{2, 4130806001517, 149795463772692060, 186635894390467037, 3967304179347715805};
CEXP u64 THRES6 = 585226005592931977, BASE6[]{2, 123635709730000, 9233062284813009, 43835965440333360, 761179012939631437, 1263739024124850375};
CEXP u64 BASE7[]{2, 325, 9375, 28178, 450775, 9780504, 1795265022};
// NOLINTEND(modernize-avoid-c-arrays)
if (n < THRES1) return f(BASE1);
if (n < THRES2) return f(BASE2);
if (n < THRES3) return f(BASE3);
if (n < THRES4) return f(BASE4);
if (n < THRES5) return f(BASE5);
if (n < THRES6) return f(BASE6);
return f(BASE7);
}
} // namespace tifa_libs
#line 8 "src/nt/pfactors/lib.hpp"
namespace tifa_libs {
namespace pfactors_impl_ {
static rand_gen<u64> e;
static auto __ = [] { e.seed(); return 0; }();
CEXP u64 rho(u64 n) NE {
e.range(1, n - 1);
auto f = [n, r = e()](u64 x) NE { return (mul_mod_u(x, x, n) + r) % n; };
u64 g = 1, x = 0, y = e(), yy = 0;
cu32 LIM = 128;
for (u64 r = 1, q = 1; g == 1; r *= 2) {
x = y;
flt_ (u64, i, 0, r) y = f(y);
for (u64 k = 0; g == 1 && k < r; k += LIM) {
yy = y;
for (u64 i = 0; i < LIM && i < r - k; ++i) q = mul_mod_u(q, (n - (y = f(y)) + x) % n, n);
g = gcd(q, n);
}
}
if (g == n) do {
g = gcd((x + (n - (yy = f(yy)))) % n, n);
} while (g == 1);
retif_((g == n), rho(n), g);
}
CEXP void run(u64 n, vecuu& p) NE {
if (n < 2) return;
if (is_prime(n)) return p.push_back(n);
cu64 g = rho(n);
run(n / g, p), run(g, p);
}
} // namespace pfactors_impl_
template <bool unique = true>
CEXP vecuu pfactors(u64 n) NE {
vecuu p;
if (cu32 _ = (u32)std::countr_zero(n) & 63; _) {
n >>= _;
if CEXP (unique) p.push_back(2);
else p.assign(_, 2);
}
if (n < 1000'000)
for (u32 i = 3; i <= n; ++i) {
if (n % i) continue;
if CEXP (unique) p.push_back(i);
do {
if CEXP (n /= i; !unique) p.push_back(i);
} while (!(n % i));
}
if (n < 2) return p;
pfactors_impl_::run(n, p);
if CEXP (unique) return uniq(p);
tifa_libs::sort(p);
return p;
}
CEXP vecp<u64, u32> pf_exp(u64 n) NE {
auto p = pfactors<false>(n);
vecp<u64, u32> ans;
for (u64 lst = 0; cu64 i : p)
if (i != lst) ans.emplace_back(lst = i, 1);
else ++ans.back().second;
return ans;
}
} // namespace tifa_libs
#line 2 "src/nt/proot/is/lib.hpp"
#line 4 "src/nt/proot/is/lib.hpp"
namespace tifa_libs {
template <std::unsigned_integral T, class It>
CEXP bool is_proot(T g, T m, It pf_begin, It pf_end) NE {
retif_((!g) [[unlikely]], false);
for (; pf_begin != pf_end; ++pf_begin)
if (qpow_mod(g, (m - 1) / *pf_begin, m) == 1) return false;
return true;
}
} // namespace tifa_libs
#line 5 "src/nt/proot/uint/lib.hpp"
namespace tifa_libs {
CEXP u64 proot(u64 m) NE {
retif_((m == 2) [[unlikely]], 1);
retif_((m == 3 || m == 5) [[unlikely]], 2);
if (m == 104857601 || m == 167772161 || m == 469762049 || m == 998244353 || m == 1004535809) return 3;
if (m == 1012924417) return 5;
if (m == 754974721) return 11;
const auto pf = pfactors(m - 1);
for (u64 g = 2;; ++g)
if (is_proot(g, m, begin(pf), end(pf))) return g;
}
} // namespace tifa_libs
#line 5 "src/conv/trans/ntt/lib.hpp"
namespace tifa_libs {
template <class mint>
struct ntt {
using data_t = mint;
static_assert(is_prime(mint::mod()) && (mint::mod() & 3) == 1, "MOD must be prime with 4k+1");
static CEXP u64 R = std::countr_zero(mint::mod() - 1), max_size = 1_u64 << R;
static CEXP mint G = proot(mint::mod());
private:
static inline arr<mint, R + 1> root, iroot, inv2;
static inline arr<mint, R - 1> rate, irate;
u32 sz{};
public:
CEXPE ntt() NE {
if (inv2[0].val()) return;
root[R] = qpow(G, mint::mod() >> R), iroot[R] = root[R].inv();
for (u32 i = R - 1; ~i; --i) {
root[i] = root[i + 1] * root[i + 1];
iroot[i] = iroot[i + 1] * iroot[i + 1];
}
mint prod(1), iprod(1);
flt_ (u32, i, 0, R - 1) {
rate[i] = prod * root[i + 2];
irate[i] = iprod * iroot[i + 2];
prod *= iroot[i + 2], iprod *= root[i + 2];
}
mint i2 = mint::mod() / 2 + 1;
inv2[0] = 1;
flt_ (u32, i, 0, R) inv2[i + 1] = inv2[i] * i2;
}
ND CEXP u32 size() CNE { return sz; }
CEXP void bzr(u32 len = max_size) NE {
cu32 n = std::bit_ceil(len);
assert(n <= max_size), sz = n;
}
CEXP void dif(vec<mint>& f, u32 n = 0) CNE {
if (assert(size()); !n) n = size();
if (f.size() < n) f.resize(n);
assert(std::has_single_bit(n) && n <= size());
cu32 l = (u32)std::countr_zero(n);
flt_ (u32, i, 0, l) {
cu32 w = 1 << (l - 1 - i), b = 1 << i;
mint z = 1;
flt_ (u32, j, 0, b) {
cu32 o = j << (l - i);
flt_ (u32, k, 0, w) {
mint x = f[o + k], y = f[o + k + w] * z;
f[o + k] = x + y, f[o + k + w] = x - y;
}
z *= rate[(u32)std::countr_zero(~j)];
}
}
}
CEXP void dit(vec<mint>& f, u32 n = 0) CNE {
assert(size());
if (!n) n = size();
if (f.size() < n) f.resize(n);
assert(std::has_single_bit(n) && n <= size());
cu32 l = (u32)std::countr_zero(n);
for (u32 i = l - 1; ~i; --i) {
cu32 w = 1 << (l - 1 - i), b = 1 << i;
mint z = 1;
flt_ (u32, j, 0, b) {
cu32 o = j << (l - i);
flt_ (u32, k, 0, w) {
mint x = f[o + k], y = f[o + k + w];
f[o + k] = x + y, f[o + k + w] = (x - y) * z;
}
z *= irate[(u32)std::countr_zero(~j)];
}
}
flt_ (u32, i, 0, n) f[i] *= inv2[l];
}
};
} // namespace tifa_libs
#line 2 "src/conv/add/dft/lib.hpp"
#line 2 "src/conv/add/naive/lib.hpp"
#line 4 "src/conv/add/naive/lib.hpp"
namespace tifa_libs {
CEXP inline u32 CONV_NAIVE_THRESHOLD = 16;
template <class U, class T = U>
requires(sizeof(U) <= sizeof(T))
CEXP vec<T> conv_naive(vec<U> CR l, vec<U> CR r, u32 ans_size = 0) NE {
retif_((l.empty() || r.empty()) [[unlikely]], {});
if (!ans_size) ans_size = u32(l.size() + r.size() - 1);
vec<T> ans(ans_size);
u32 n = (u32)l.size(), m = (u32)r.size();
auto &&l_ = n < m ? r : l, &&r_ = n < m ? l : r;
if (n < m) swap(n, m);
flt_ (u32, i, 0, n)
flt_ (u32, j, 0, min(m, ans_size - i)) ans[i + j] += (T)l_[i] * (T)r_[j];
return ans;
}
} // namespace tifa_libs
#line 5 "src/conv/add/dft/lib.hpp"
namespace tifa_libs {
template <dft_c DFT_t, std::same_as<TPN DFT_t::data_t> DFT_data_t>
CEXP vec<DFT_data_t> conv_dft(DFT_t& dft, vec<DFT_data_t> l, vec<DFT_data_t> r, u32 ans_size = 0) NE {
if (!ans_size) ans_size = u32(l.size() + r.size() - 1);
if (min(l.size(), r.size()) < CONV_NAIVE_THRESHOLD) return conv_naive(l, r, ans_size);
dft.bzr(max({(u32)l.size(), (u32)r.size(), min(u32(l.size() + r.size() - 1), ans_size)}));
dft.dif(l), dft.dif(r);
flt_ (u32, i, 0, dft.size()) l[i] *= r[i];
dft.dit(l), l.resize(ans_size);
return l;
}
template <class DFT_t, class mint, class T = u64>
CEXP vec<mint> conv_dft_um(DFT_t& dft, vec<T> CR l, vec<T> CR r, u32 ans_size = 0) NE {
if (!ans_size) ans_size = u32(l.size() + r.size() - 1);
vec<mint> l_, r_;
for (l_.reserve(l.size()); auto CR i : l) l_.push_back(i);
for (r_.reserve(r.size()); auto CR i : r) r_.push_back(i);
return conv_dft(dft, l_, r_, ans_size);
}
} // namespace tifa_libs
#line 7 "src/conv/add/u128/lib.hpp"
namespace tifa_libs {
// max = 167772161 * 469762049 * 754974721 \approx 5.95e25
template <class T>
vec<u128> conv_u128(vec<T> CR l, vec<T> CR r, u32 ans_size = 0) NE {
if (!ans_size) ans_size = u32(l.size() + r.size() - 1);
if (min(l.size(), r.size()) < CONV_NAIVE_THRESHOLD) return conv_naive<T, u128>(l, r, ans_size);
static CEXP u32 m0 = 167772161, m1 = 469762049, m2 = 754974721;
using mint0 = mint_ms<m0>;
using mint1 = mint_ms<m1>;
using mint2 = mint_ms<m2>;
static CEXP u32 r01 = inverse(m0, mint1::mod()),
r02 = inverse(m0, mint2::mod()),
r12 = inverse(m1, mint2::mod()),
r02r12 = (u64)r02 * r12 % m2;
static CEXP u64 w1 = m0, w2 = (u64)m0 * m1;
static ntt<mint0> ntt0;
static ntt<mint1> ntt1;
static ntt<mint2> ntt2;
cvec<mint0> d0 = conv_dft_um<ntt<mint0>, mint0>(ntt0, l, r, ans_size);
cvec<mint1> d1 = conv_dft_um<ntt<mint1>, mint1>(ntt1, l, r, ans_size);
cvec<mint2> d2 = conv_dft_um<ntt<mint2>, mint2>(ntt2, l, r, ans_size);
vec<u128> ret(ans_size);
flt_ (u32, i, 0, ans_size) {
cu64 n1 = d1[i].val(), n2 = d2[i].val(),
a = d0[i].val(), b = (n1 + m1 - a) * r01 % m1;
cu128 c = ((n2 + m2 - a) * r02r12 + (m2 - b) * r12) % m2;
ret[i] = a + b * w1 + c * w2;
}
return ret;
}
} // namespace tifa_libs
#line 2 "src/fast/s2u/u32/lib.hpp"
#line 2 "src/bit/bswap/lib.hpp"
#line 4 "src/bit/bswap/lib.hpp"
namespace tifa_libs {
// From GCC lib
template <class T>
CEXP T bswap(T x) NE {
if CEXP (sizeof(T) == 2) return __builtin_bswap16(x);
if CEXP (sizeof(T) == 4) return __builtin_bswap32(x);
if CEXP (sizeof(T) == 8) return __builtin_bswap64(x);
if CEXP (sizeof(T) == 16) return __builtin_bswap128(x);
// if CEXP (sizeof(T) == 16) return (__builtin_bswap64(x >> 64) | (static_cast<T>(__builtin_bswap64(x)) << 64));
}
} // namespace tifa_libs
#line 5 "src/fast/s2u/u32/lib.hpp"
namespace tifa_libs {
CEXP u32 str2uint_si64(chr CPC s) NE {
u64 _ = *((u64*)(s));
if CEXP (std::endian::native == std::endian::big) _ = bswap(_);
_ = (_ & 0x0F0F0F0F0F0F0F0F) * 2561 >> 8;
_ = (_ & 0x00FF00FF00FF00FF) * 6553601 >> 16;
_ = (_ & 0x0000FFFF0000FFFF) * 42949672960001 >> 32;
return (u32)_;
}
} // namespace tifa_libs
#line 2 "src/util/strip/lib.hpp"
#line 4 "src/util/strip/lib.hpp"
namespace tifa_libs {
// pred(x) == true ==> drop
template <common_range R, class F>
CEXP auto lstrip_view(R CR range, F&& pred) NE {
auto v = range | views::drop_while(std::forward<F>(pred));
return subrange{begin(v), end(v)};
}
// pred(x) == true ==> drop
template <common_range R, class F>
CEXP auto rstrip_view(R CR range, F&& pred) NE {
auto v = range | views::reverse | views::drop_while(std::forward<F>(pred));
return subrange{end(v).base(), begin(v).base()};
}
// pred(x) == true ==> drop
template <common_range R, class F>
CEXP auto strip_view(R CR range, F&& pred) NE {
auto v = range | views::drop_while(std::forward<F>(pred)) | views::reverse | views::drop_while(std::forward<F>(pred));
return subrange{end(v).base(), begin(v).base()};
}
} // namespace tifa_libs
#line 7 "src/math/ds/mpi/lib.hpp"
namespace tifa_libs {
struct mpi : vecu {
static CEXP u32 D = 1e8, lgD = 8, sqrtD = 1e4;
static_assert(sqrtD * sqrtD == D);
static CEXP u32 hex_lgD = 6, hex_D = 1u << 24;
private:
static CEXP arr<u32, hex_lgD + 1> hex_pow = {1u, 16u, 256u, 4096u, 65536u, 1048576u, hex_D};
struct ict4 {
CEXP static auto num = [] {
arr<u32, sqrtD> num;
for (u32 e0 = (48 << 0), j = 0; e0 < (58 << 0); e0 += (1 << 0))
for (u32 e1 = (48 << 8); e1 < (58 << 8); e1 += (1 << 8))
for (u32 e2 = (48 << 16); e2 < (58 << 16); e2 += (1 << 16))
for (u32 e3 = (48 << 24); e3 < (58 << 24); e3 += (1 << 24)) num[j++] = e0 ^ e1 ^ e2 ^ e3;
return num;
}();
static auto get(u32 x) NE { return (chr CP)(num.data() + x); }
};
bool neg = false, hmd_ = false;
vecu hbits_;
#define vec_like std::derived_from<vecu> auto
// name starts with u ==> ignore sign
static CEXP auto ucmp(vec_like CR a, vec_like CR b) NE {
if (a.size() != b.size()) return a.size() <=> b.size();
return std::lexicographical_compare_three_way(a.rbegin(), a.rend(), b.rbegin(), b.rend());
}
static CEXP auto cmp(mpi CR a, mpi CR b) NE {
if (a.neg) {
retif_((b.neg), ucmp(b, a), std::strong_ordering::less);
} else retif_((b.neg), std::strong_ordering::greater, ucmp(a, b));
}
static CEXP bool is_0(vec_like CR a) NE { return a.empty(); }
static CEXP bool is_pm1(vec_like CR a) NE { return a.size() == 1 && a[0] == 1; }
static CEXP bool is_1(mpi CR a) NE { return !a.neg && is_pm1(a); }
static CEXP void shrink_(vec_like& a) NE {
auto [_, r] = rstrip_view(a, [](u32 x) { return !x; });
a.erase(r, a.end());
}
template <int_c T>
static CEXP vecu int2vecu(T x) NE {
if CEXP (sint_c<T>) assert(x >= 0);
vecu res;
while (x) res.push_back(u32(x % D)), x /= D;
return res;
}
static CEXP i64 uvec2i64(vec_like CR a) NE {
i64 res = 0;
for (u32 i = (u32)a.size() - 1; ~i; --i) res = res * D + a[i];
return res;
}
public:
CEXP mpi() NE = default;
CEXP mpi(bool n, itlu x) NE : vecu(x), neg(n) {}
CEXP mpi(bool n, spnu d) NE : vecu(d.begin(), d.end()), neg(n) {}
template <int_c T>
CEXP mpi(T x) NE : mpi() {
if CEXP (sint_c<T>)
if (x < 0) neg = true, x = -x;
while (x) push_back(u32(to_uint_t<T>(x) % D)), x /= (T)D;
}
CEXP mpi(strn s) NE : mpi() {
if (assert(!s.empty()); s.size() == 1u) {
if (s[0] == '0') return;
assert(isdigit(s[0])), push_back(s[0] & 15);
return;
}
u32 l = 0;
if (s[0] == '-') ++l, neg = true;
u32 _ = 0;
if (cu32 ofs = u32(s.size() - l) & 7; ofs) {
flt_ (u32, i, l, l + ofs) _ = _ * 10 + (s[i] & 15);
l += ofs;
}
if (l) s = s.substr(l);
for (u32 ie = (u32)s.size(); ie >= lgD; ie -= lgD)
push_back(str2uint_si64(s.data() + ie - lgD));
if (_) push_back(_);
}
CEXP static mpi from_hex_str(strn s) NE {
mpi res;
res.hmd_ = true;
if (assert(!s.empty()); s.size() == 1u && s[0] == '0') return res;
u32 l = 0;
if (s[0] == '-') ++l, res.neg = true;
if (l + 1 < s.size() && s[l] == '0' && (s[l + 1] == 'x' || s[l + 1] == 'X')) l += 2;
assert(l < s.size());
for (u32 r = (u32)s.size(); l < r;) {
cu32 is = r > lgD ? r - lgD : l;
u32 x = 0;
flt_ (u32, i, is, r) x = (x << 4) | hex_to_u32_(s[i]);
res.hbits_.push_back(x), r = is;
}
if (h_shrink_(res.hbits_); res.hbits_.empty()) res.neg = false;
return res;
}
CEXP void set_neg(bool s) NE { neg = is_zero() ? false : s; }
ND CEXP bool is_neg() CNE { return neg; }
ND CEXP bool is_zero() CNE { return hmd_ ? hbits_.empty() : is_0(*this); }
CEXP void shrink() NE {
if (hmd_) h_shrink_(hbits_);
else shrink_(*this);
if (is_zero()) neg = false;
}
friend CEXP mpi abs(mpi m) NE {
m.neg = false;
return m;
}
friend CEXP mpi operator+(mpi CR l, mpi CR r) NE {
if (l.hmd_ && r.hmd_) {
if (l.neg == r.neg) return make_hex_(l.neg, h_add_(l.hbits_, r.hbits_));
ci32 ccmp = h_cmp_mag_(l.hbits_, r.hbits_);
if (!ccmp) return make_hex_(false, {});
if (ccmp < 0) {
auto c = h_sub_(r.hbits_, l.hbits_);
bool const n = c.empty() ? false : r.neg;
return make_hex_(n, std::move(c));
}
auto c = h_sub_(l.hbits_, r.hbits_);
bool const n = c.empty() ? false : l.neg;
return make_hex_(n, std::move(c));
}
auto ld = l.as_dec_(), rd = r.as_dec_();
if (ld.neg == rd.neg) return {ld.neg, uadd(ld, rd)};
if (std::is_lteq(ucmp(ld, rd))) {
auto c = usub(rd, ld);
return {is_0(c) ? false : rd.neg, c};
}
auto c = usub(ld, rd);
return {is_0(c) ? false : ld.neg, c};
}
friend CEXP mpi operator-(mpi CR l, mpi CR r) NE { return l + (-r); }
friend CEXP mpi operator*(mpi CR l, mpi CR r) NE {
if (l.hmd_ && r.hmd_) {
auto c = h_mul_(l.hbits_, r.hbits_);
bool const n = c.empty() ? false : bool(l.neg ^ r.neg);
return make_hex_(n, std::move(c));
}
auto ld = l.as_dec_(), rd = r.as_dec_();
auto c = umul(ld, rd);
bool const n = is_0(c) ? false : (ld.neg ^ rd.neg);
return {n, c};
}
friend CEXP ptt<mpi> divmod(mpi CR l, mpi CR r) NE {
if (l.hmd_ && r.hmd_) {
const auto dm = h_divmod_(l.hbits_, r.hbits_);
const bool qn = dm.first.empty() ? false : (l.neg != r.neg), rn = dm.second.empty() ? false : l.neg;
return {make_hex_(qn, dm.first), make_hex_(rn, dm.second)};
}
auto ld = l.as_dec_(), rd = r.as_dec_();
auto dm = udivmod(ld, rd);
return {mpi{is_0(dm.first) ? false : ld.neg != rd.neg, dm.first}, mpi{is_0(dm.second) ? false : ld.neg, dm.second}};
}
friend CEXP mpi operator/(mpi CR l, mpi CR r) NE { return divmod(l, r).first; }
friend CEXP mpi operator%(mpi CR l, mpi CR r) NE { return divmod(l, r).second; }
CEXP mpi& operator+=(mpi CR r) NE { return (*this) = (*this) + r; }
CEXP mpi& operator-=(mpi CR r) NE { return (*this) = (*this) - r; }
CEXP mpi& operator*=(mpi CR r) NE { return (*this) = (*this) * r; }
CEXP mpi& operator/=(mpi CR r) NE { return (*this) = (*this) / r; }
CEXP mpi& operator%=(mpi CR r) NE { return (*this) = (*this) % r; }
CEXP mpi operator-() CNE {
if (is_zero()) return *this;
auto ret = *this;
ret.neg = !ret.neg;
return ret;
}
CEXP mpi operator+() CNE { return *this; }
friend CEXP auto operator<=>(mpi CR l, mpi CR r) NE {
if (l.hmd_ && r.hmd_) {
if (l.neg != r.neg) return l.neg ? std::strong_ordering::less : std::strong_ordering::greater;
if (h_eq_(l.hbits_, r.hbits_)) return std::strong_ordering::equal;
return l.neg ^ h_lt_(l.hbits_, r.hbits_) ? std::strong_ordering::less : std::strong_ordering::greater;
}
return cmp(l.as_dec_(), r.as_dec_());
}
friend CEXP bool operator==(mpi CR l, mpi CR r) NE { return std::is_eq(l <=> r); }
ND CEXP strn to_str() CNE {
if (is_zero()) return "0";
strn r;
if (r.reserve(size() * 8 + 1); neg) r.push_back('-');
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
chr int_buf[11];
auto res = std::to_chars(int_buf, int_buf + 11, back());
r.append(int_buf, u32(res.ptr - int_buf));
for (u32 i = (u32)size() - 2; ~i; --i) r.append(ict4::get((*this)[i] / 10000u), 4), r.append(ict4::get((*this)[i] % 10000u), 4);
return r;
}
ND CEXP strn to_hex_str() CNE {
if (hmd_) {
if (hbits_.empty()) return "0";
strn res;
if (neg) res.push_back('-');
for (u32 i = (u32)hbits_.size() - 1; ~i; --i) res += h_u32_to_hex_(hbits_[i], i + 1 != hbits_.size());
return res;
}
if (is_zero()) return "0";
mpi x = abs(*this);
vecu rems;
rems.reserve((x.size() * 27 + 23) / 24);
while (!x.is_zero()) {
auto [q, r] = udivmod_1e8(x, vecu{hex_D});
x = mpi{false, q}, rems.push_back(r.empty() ? 0 : r[0]);
}
strn res;
if (res.reserve(neg + rems.size() * hex_lgD); neg) res.push_back('-');
res += u32_to_hex_(rems.back(), false);
for (u32 i = (u32)rems.size() - 1; i; --i) res += u32_to_hex_(rems[i - 1], true);
return res;
}
ND CEXP mpi as_mpi() CNE { return *this; }
ND CEXP i64 to_i64() CNE { retif_((ci64 res = uvec2i64(*this); neg), -res, res); }
ND CEXP i128 to_i128() CNE {
i128 res = 0;
for (u32 i = (u32)size() - 1; ~i; --i) res = res * D + (*this)[i];
retif_((neg), -res, res);
}
friend auto& operator>>(istream_c auto& is, mpi& m) NE {
strn s;
is >> s, m = mpi{s};
return is;
}
friend auto& operator<<(ostream_c auto& os, mpi CR m) NE { return os << m.to_str(); }
private:
static CEXP mpi make_hex_(bool n, vecu bits) NE {
mpi x;
x.hmd_ = true, x.hbits_ = std::move(bits), h_shrink_(x.hbits_), x.neg = x.hbits_.empty() ? false : n;
return x;
}
ND CEXP mpi as_dec_() CNE {
if (!hmd_) return *this;
mpi r;
r.neg = neg;
for (u32 i = (u32)hbits_.size() - 1; ~i; --i) umuladd_(r, 65536u, 0), umuladd_(r, 65536u, hbits_[i] >> 16), umuladd_(r, 65536u, hbits_[i] & 65535u);
if (r.empty()) r.neg = false;
return r;
}
static CEXP u32 hex_to_u32_(chr c) NE { return (c & 15) + ((c & 64) >> 6) * 9; }
static CEXP strn u32_to_hex_(u32 x, bool pad0) NE {
arr<chr, hex_lgD> buf;
flt_ (u32, i, 0, hex_lgD) {
cu32 d = x & 15;
buf[hex_lgD - 1 - i] = chr(d + (d < 10 ? 48 : 55)), x >>= 4;
}
if (pad0) return {buf.begin(), buf.end()};
u32 l = 0;
while (l + 1 < hex_lgD && buf[l] == '0') ++l;
return {buf.begin() + l, buf.end()};
}
static CEXP strn h_u32_to_hex_(u32 x, bool pad0) NE {
strn res;
res.reserve(8);
flt_ (u32, _, 0, 8) {
cu32 d = x & 15;
res.push_back(chr(d + (d < 10 ? 48 : 55))), x >>= 4;
}
if (!pad0) {
while (!res.empty() && res.back() == '0') res.pop_back();
if (res.empty()) res.push_back('0');
}
reverse(res);
return res;
}
static CEXP void umuladd_(vecu& a, u32 mul, u32 add) NE {
if (a.empty() && !add) return;
u64 carry = add;
flt_ (u32, i, 0, (u32)a.size()) a[i] = u32((carry += (u64)a[i] * mul) % D), carry /= D;
while (carry) a.push_back(u32(carry % D)), carry /= D;
}
using iter = u32*;
using citer = u32 CP;
static CEXP usz h_nz_size_(vecu CR a) NE {
usz n = a.size();
while (n && !a[n - 1]) --n;
return n;
}
static CEXP bool h_eq_(vecu CR a, vecu CR b) NE {
usz const asz = h_nz_size_(a);
if (asz != h_nz_size_(b)) return false;
flt_ (usz, i, 0, asz)
if (a[i] != b[i]) return false;
return true;
}
static CEXP bool h_is0_(vecu CR a) NE { return a.empty(); }
static CEXP bool h_is_one_(vecu CR a) NE { return a.size() == 1 && a[0] == 1; }
static CEXP void h_shrink_(vecu& a) NE {
while (!a.empty() && !a.back()) a.pop_back();
}
static CEXP bool h_lt_(citer a, citer a_end, citer b, citer b_end) NE {
usz asz = usz(a_end - a), bsz = usz(b_end - b);
if (asz != bsz) {
if (asz < bsz) {
if (find_if(b + asz, b_end, [](u32 x) { return x; }) != b_end) return true;
} else {
if (find_if(a + bsz, a_end, [](u32 x) { return x; }) != a_end) return false;
asz = bsz;
}
}
for (usz i = asz - 1; ~i; --i)
if (a[i] != b[i]) return a[i] < b[i];
return false;
}
static CEXP bool h_lt_(vecu CR a, vecu CR b) NE {
cusz asz = h_nz_size_(a), bsz = h_nz_size_(b);
if (asz != bsz) return asz < bsz;
for (usz i = asz - 1; ~i; --i)
if (a[i] != b[i]) return a[i] < b[i];
return false;
}
static CEXP bool h_leq_(vecu CR a, vecu CR b) NE { return h_eq_(a, b) || h_lt_(a, b); }
static CEXP i32 h_cmp_mag_(vecu CR a, vecu CR b) NE {
cusz asz = h_nz_size_(a), bsz = h_nz_size_(b);
if (asz != bsz) {
retif_((asz < bsz), -1, 1);
}
for (usz i = asz - 1; ~i; --i)
if (a[i] != b[i]) {
retif_((a[i] < b[i]), -1, 1);
}
return 0;
}
static CEXP void h_add_(citer a, citer a_end, citer b, citer b_end, iter c, iter c_end) NE {
if (a_end - a < b_end - b) std::swap(a, b), std::swap(a_end, b_end);
cusz asz = usz(a_end - a), bsz = usz(b_end - b);
assert(std::cmp_less_equal(asz, c_end - c));
u32 carry = 0;
u64 v;
flt_ (usz, i, 0, bsz) c[i] = u32(v = (u64)a[i] + b[i] + carry), carry = u32(v >> 32);
flt_ (usz, i, bsz, asz) c[i] = u32(v = (u64)a[i] + carry), carry = u32(v >> 32);
if (carry) c[asz] = carry;
}
static CEXP void h_add_(iter a, iter a_end, citer b, citer b_end) NE {
cusz asz = usz(a_end - a), bsz = usz(b_end - b);
u32 carry = 0;
u64 v;
flt_ (usz, i, 0, bsz) a[i] = u32(v = (u64)a[i] + b[i] + carry), carry = u32(v >> 32);
for (usz i = bsz; carry && i < asz; ++i) a[i] = u32(v = (u64)a[i] + carry), carry = u32(v >> 32);
}
static CEXP vecu h_add_(vecu CR a, vecu CR b) NE {
vecu c(max(a.size(), b.size()) + 1);
h_add_(a.data(), a.data() + a.size(), b.data(), b.data() + b.size(), c.data(), c.data() + c.size()), h_shrink_(c);
return c;
}
static CEXP void h_sub_(iter a, iter a_end, citer b, citer b_end) NE {
cusz asz = usz(a_end - a), bsz = usz(b_end - b);
i32 carry = 0;
i64 v;
flt_ (usz, i, 0, bsz) a[i] = u32(v = (i64)a[i] - b[i] + carry), carry = i32(v >> 32);
for (usz i = bsz; carry && i < asz; ++i) a[i] = u32(v = (i64)a[i] + carry), carry = i32(v >> 32);
assert(!carry);
}
static CEXP vecu h_sub_(vecu CR a, vecu CR b) NE {
assert(h_leq_(b, a));
vecu c = a;
h_sub_(c.data(), c.data() + c.size(), b.data(), b.data() + b.size()), h_shrink_(c);
return c;
}
static CEXP void h_mul_naive_(citer a, citer a_end, citer b, citer b_end, iter c, iter c_end) NE {
cusz asz = usz(a_end - a), bsz = usz(b_end - b);
if (!asz || !bsz) return;
assert(usz(c_end - c) == asz + bsz);
flt_ (usz, i, 0, asz) {
u32 carry = 0;
u64 p;
flt_ (usz, j, 0, bsz) c[i + j] = u32(p = (u64)a[i] * b[j] + carry + c[i + j]), carry = u32(p >> 32);
c[i + bsz] = carry;
}
}
static CEXP void h_mul_(citer a, citer a_end, citer b, citer b_end, iter c, iter c_end) NE {
if (a_end - a < b_end - b) std::swap(a, b), std::swap(a_end, b_end);
cusz asz = usz(a_end - a), bsz = usz(b_end - b);
assert(usz(c_end - c) == asz + bsz);
if (bsz <= 128) return h_mul_naive_(a, a_end, b, b_end, c, c_end);
usz const n = (asz + 1) >> 1;
if (bsz <= n) {
h_mul_(a, a + n, b, b_end, c, c + (n + bsz));
vecu carry(c + n, c + (n + bsz));
fill(c + n, c + (n + bsz), 0), h_mul_(a + n, a_end, b, b_end, c + n, c_end), h_add_(c + n, c_end, carry.data(), carry.data() + carry.size());
return;
}
vecu a1(n + 1), b1(n + 1), z1(2 * n + 2);
h_mul_(a, a + n, b, b + n, c, c + (n + n)), h_mul_(a + n, a_end, b + n, b_end, c + (n + n), c_end), h_add_(a, a + n, a + n, a_end, a1.data(), a1.data() + a1.size()), h_add_(b, b + n, b + n, b_end, b1.data(), b1.data() + b1.size()), h_mul_(a1.data(), a1.data() + a1.size(), b1.data(), b1.data() + b1.size(), z1.data(), z1.data() + z1.size()), h_sub_(z1.data(), z1.data() + z1.size(), c, c + (n + n)), h_sub_(z1.data(), z1.data() + z1.size(), c + (n + n), c_end), h_shrink_(z1), h_add_(c + n, c_end, z1.data(), z1.data() + z1.size());
}
static CEXP vecu h_mul_(vecu CR a, vecu CR b) NE {
if (h_is0_(a) || h_is0_(b)) return {};
if (h_is_one_(a)) return b;
if (h_is_one_(b)) return a;
vecu c(a.size() + b.size());
h_mul_(a.data(), a.data() + a.size(), b.data(), b.data() + b.size(), c.data(), c.data() + c.size()), h_shrink_(c);
return c;
}
static CEXP u64 h_it2u64_(citer it, citer end) NE {
u64 res = 0;
u32 sh = 0;
while (it != end) res |= ((u64)*it++) << sh, sh += 32;
return res;
}
static CEXP void h_u642it_(u64 x, iter it, iter end) NE {
while (x) {
assert(it < end);
*it++ = (u32)x, x >>= 32;
}
}
static CEXP void h_lsh_(iter a, iter a_end, int shift) NE {
if (!shift) return;
csint back = 32 - shift;
u32 carry = 0, v;
flt_ (iter, it, a, a_end) v = (*it << shift) | carry, carry = (*it >> back), *it = v;
assert(!carry);
}
static CEXP void h_rsh_(iter a, iter a_end, int shift) NE {
if (!shift) return;
csint back = 32 - shift;
u32 carry = 0, v;
for (iter it = a_end - 1; a <= it; --it) v = (*it >> shift) | carry, carry = (*it << back), *it = v;
assert(!carry);
}
static CEXP u32 h_div_naive_th_ = 64;
static CEXP void h_divmod_naive_(citer a, citer a_end, citer b, citer b_end, iter quo, iter quo_end, iter rem, iter rem_end) NE {
cusz asz = usz(a_end - a), bsz = usz(b_end - b);
assert(bsz > 0);
if (bsz == 1) {
u64 carry = 0, v;
for (usz i = asz - 1; ~i; --i) quo[i] = u32((v = (carry << 32) | a[i]) / (*b)), carry = v - quo[i] * (*b);
*rem = (u32)carry;
return;
}
if (max(asz, bsz) <= 2) {
cu64 a64 = h_it2u64_(a, a_end), b64 = h_it2u64_(b, b_end);
h_u642it_(a64 / b64, quo, quo_end), h_u642it_(a64 % b64, rem, rem_end);
return;
}
if (h_lt_(a, a_end, b, b_end)) {
if (asz > bsz) a_end = a + bsz;
copy(a, a_end, rem);
return;
}
auto sh = std::countl_zero(*(b_end - 1));
vecu x(asz + (std::countl_zero(*(a_end - 1)) < sh)), y(usz(b_end - b));
copy(a, a_end, x.begin()), copy(b, b_end, y.data()), h_lsh_(x.data(), x.data() + x.size(), sh), h_lsh_(y.data(), y.data() + y.size(), sh);
cu32 yb = y.back();
vecu qv(x.size() - y.size() + 1), rv(x.end() - (int)y.size(), x.end());
for (usz i = (u32)qv.size() - 1; ~i; --i) {
if (rv.size() == y.size()) {
if (h_leq_(y, rv)) qv[i] = 1, rv = h_sub_(rv, y);
} else if (rv.size() > y.size()) {
u64 q = (((u64)rv[rv.size() - 1] << 32) | rv[rv.size() - 2]) / yb;
if (q > (u64)-1_u32) q = (u64)-1_u32;
vecu yq = h_mul_(y, vecu{(u32)q});
while (h_lt_(rv, yq)) --q, yq = h_sub_(yq, y);
rv = h_sub_(rv, yq);
while (h_leq_(y, rv)) ++q, rv = h_sub_(rv, y);
qv[i] = (u32)q;
}
if (i) rv.insert(rv.begin(), x[i - 1]);
h_shrink_(rv);
}
h_shrink_(qv), h_rsh_(rv.data(), rv.data() + rv.size(), sh), h_shrink_(rv), copy(qv.data(), qv.data() + qv.size(), quo), copy(rv.data(), rv.data() + rv.size(), rem);
}
static CEXP void h_divmod_d2n1n_(citer a, citer a_end, citer b, citer b_end, iter quo, iter quo_end, iter rem, iter rem_end) {
usz const n = usz(b_end - b);
if ((n & 1) || n <= h_div_naive_th_) {
h_divmod_naive_(a, a_end, b, b_end, quo, quo_end, rem, rem_end);
return;
}
usz const half = n >> 1;
vecu r1(n + half + 1);
copy(a, a + half, r1.data()), h_divmod_d3n2n_impl_(a + half, a_end, b, b_end, quo + half, quo_end, r1.data() + half, r1.data() + r1.size()), h_divmod_d3n2n_impl_(r1.data(), r1.data() + r1.size() - 1, b, b_end, quo, quo + half, rem, rem_end);
}
static CEXP void h_divmod_d3n2n_impl_(citer a, citer a_end, citer b, citer b_end, iter quo, iter quo_end, iter rem, iter rem_end) {
usz const n = usz(b_end - b) >> 1;
vecu d(2 * n);
if (h_lt_(a + (n + n), a_end, b + n, b_end)) h_divmod_d2n1n_(a + n, a_end, b + n, b_end, quo, quo_end, rem + n, rem_end), h_mul_(quo, quo_end, b, b + n, d.data(), d.data() + d.size());
else fill(quo, quo_end, -1_u32), h_add_(a + n, a + (n + n), b + n, b_end, rem + n, rem_end), copy(b, b + n, d.data() + n), h_sub_(d.data(), d.data() + d.size(), b, b + n);
copy(a, a + n, rem);
while (h_lt_(rem, rem_end, d.data(), d.data() + d.size())) {
vecu one{1};
h_add_(rem, rem_end, b, b_end), h_sub_(quo, quo_end, one.data(), one.data() + one.size());
}
h_sub_(rem, rem_end, d.data(), d.data() + d.size());
}
static CEXP void h_divmod_(citer a, citer a_end, citer b, citer b_end, iter q, iter q_end, iter r, iter r_end) NE {
cusz asz = usz(a_end - a), bsz = usz(b_end - b), rsz = usz(r_end - r);
if (std::min(asz - bsz, bsz) <= h_div_naive_th_) return h_divmod_naive_(a, a_end, b, b_end, q, q_end, r, r_end);
usz n;
{
usz m = (bsz + h_div_naive_th_ - 1) / h_div_naive_th_;
if (m > 1) m = 1_usz << (32 - std::countl_zero(u32(m - 1)));
n = ((bsz + m - 1) / m) * m;
}
csint sd = (int)n - (int)bsz, ssh = std::countl_zero(*(b_end - 1));
vecu x(asz + (usz)sd + (std::countl_zero(*(a_end - 1)) <= ssh)), y(n), rr(n + 1), z(2 * n);
copy(a, a_end, x.begin() + sd), copy(b, b_end, y.begin() + sd), h_lsh_(x.data() + sd, x.data() + x.size(), ssh), h_lsh_(y.data() + sd, y.data() + y.size(), ssh);
usz const t = max(2_usz, (x.size() + n - 1) / n);
copy(x.data() + ((t - 2) * n), x.data() + x.size(), z.data());
if (usz const q_ulen = usz(q_end - (q + ((t - 2) * n))); q_ulen < n) {
vecu qq(n);
h_divmod_d2n1n_(z.data(), z.data() + z.size(), y.data(), y.data() + y.size(), qq.data(), qq.data() + qq.size(), rr.data(), rr.data() + rr.size()), copy(qq.data(), qq.data() + q_ulen, q + ((t - 2) * n));
} else h_divmod_d2n1n_(z.data(), z.data() + z.size(), y.data(), y.data() + y.size(), q + ((t - 2) * n), q + ((t - 1) * n), rr.data(), rr.data() + rr.size());
for (u32 i = (u32)t - 3; ~i; --i) copy(x.data() + i * n, x.data() + (i + 1) * n, z.data()), copy(rr.data(), rr.data() + n, z.data() + n), fill(rr.data(), rr.data() + rr.size(), 0), h_divmod_d2n1n_(z.data(), z.data() + z.size(), y.data(), y.data() + y.size(), q + i * n, q + (i + 1) * n, rr.data(), rr.data() + rr.size());
h_shrink_(rr), copy(rr.data() + sd, rr.data() + sd + rsz, r), h_rsh_(r, r_end, ssh);
}
static CEXP ptt<vecu> h_divmod_(vecu CR a, vecu CR b) NE {
assert(!h_is0_(b));
if (a.size() < b.size()) return {{}, a};
vecu q(a.size() - b.size() + 1), r(b.size());
h_divmod_(a.data(), a.data() + a.size(), b.data(), b.data() + b.size(), q.data(), q.data() + q.size(), r.data(), r.data() + r.size()), h_shrink_(q), h_shrink_(r);
return {q, r};
}
static CEXP vecu uadd(vec_like CR a, vec_like CR b) NE {
vecu c(max(a.size(), b.size()) + 1);
flt_ (u32, i, 0, (u32)a.size()) c[i] += a[i];
flt_ (u32, i, 0, (u32)b.size()) c[i] += b[i];
flt_ (u32, i, 0, (u32)c.size() - 1)
if (c[i] >= D) c[i] -= D, ++c[i + 1];
shrink_(c);
return c;
}
static CEXP vecu usub(vec_like CR a, vec_like CR b) NE {
assert(std::is_lteq(ucmp(b, a)));
vecu c = a;
u32 borrow = 0;
flt_ (u32, i, 0, (u32)a.size()) {
if (i < b.size()) borrow += b[i];
if (c[i] -= borrow, borrow = 0; (i32)c[i] < 0) c[i] += D, borrow = 1;
}
assert(!borrow);
shrink_(c);
return c;
}
static CEXP vecu umul_3ntt(vec_like CR a, vec_like CR b) NE {
if (a.empty() || b.empty()) return {};
auto m = conv_u128(a, b);
vecu c;
c.reserve(m.size() + 3);
u128 x = 0;
for (u32 i = 0;; ++i) {
if (i >= m.size() && !x) break;
if (i < m.size()) x += m[i];
c.push_back(u32(x % D)), x /= D;
}
shrink_(c);
return c;
}
static CEXP vecu umul_bf(vec_like CR a, vec_like CR b) NE {
if (a.empty() || b.empty()) return {};
vecuu prod(a.size() + b.size() - 1 + 1);
flt_ (u32, i, 0, (u32)a.size())
flt_ (u32, j, 0, (u32)b.size())
if ((prod[i + j] += (u64)a[i] * b[j]) >= 4_u64 * D * D) prod[i + j] -= 4_u64 * D * D, prod[i + j + 1] += 4_u64 * D;
vecu c(prod.size() + 1);
u64 x = 0;
u32 i = 0;
for (; i < prod.size(); ++i) x += prod[i], c[i] = u32(x % D), x /= D;
while (x) c[i] = u32(x % D), x /= D, ++i;
shrink_(c);
return c;
}
static CEXP vecu umul(vec_like CR a, vec_like CR b) NE {
if (is_0(a) || is_0(b)) return {};
if (is_pm1(a)) return b;
if (is_pm1(b)) return a;
if (min(a.size(), b.size()) <= CONV_NAIVE_THRESHOLD) {
retif_((a.size() < b.size()), umul_bf(b, a), umul_bf(a, b));
} else return umul_3ntt(a, b);
}
// 0 <= A < 1e16, 1 <= B < 1e8
static CEXP ptt<vecu> udivmod_li(vec_like CR a, vec_like CR b) NE {
assert(a.size() <= 2 && b.size() == 1);
ci64 va = uvec2i64(a);
cu32 vb = b[0];
return {int2vecu(va / vb), int2vecu(va % vb)};
}
// 0 <= A < 1e16, 1 <= B < 1e16
static CEXP ptt<vecu> udivmod_ll(vec_like CR a, vec_like CR b) NE {
assert(a.size() <= 2 && b.size() && b.size() <= 2);
ci64 va = uvec2i64(a), vb = uvec2i64(b);
return {int2vecu(va / vb), int2vecu(va % vb)};
}
// 1 <= B < 1e8
static CEXP ptt<vecu> udivmod_1e8(vec_like CR a, vec_like CR b) NE {
if (assert(b.size() == 1); b[0] == 1) return {a, {}};
if (a.size() <= 2) return udivmod_li(a, b);
vecu quo(a.size());
u64 d = 0;
cu32 b0 = b[0];
for (u32 i = (u32)a.size() - 1; ~i; --i) d = d * D + a[i], assert(d < (u64)D * b0), quo[i] = u32(d / b0), d = d % b0;
shrink_(quo);
return {quo, d ? vecu{u32(d)} : vecu{}};
}
// 0 <= A, 1 <= B
static CEXP ptt<vecu> udivmod_bf(vec_like CR a, vec_like CR b) NE {
if (assert(!is_0(b) && b.size()); b.size() == 1) return udivmod_1e8(a, b);
if (max(a.size(), b.size()) <= 2) return udivmod_ll(a, b);
if (std::is_lt(ucmp(a, b))) return {{}, a};
// B >= 1e8, A >= B
cu32 norm = D / (b.back() + 1);
vecu x = umul(a, vecu{norm}), y = umul(b, vecu{norm});
cu32 yb = y.back();
vecu quo(x.size() - y.size() + 1), rem(x.end() - (int)y.size(), x.end());
for (u32 i = (u32)quo.size() - 1; ~i; --i) {
if (rem.size() == y.size()) {
if (std::is_lteq(ucmp(y, rem))) quo[i] = 1, rem = usub(rem, y);
} else if (rem.size() > y.size()) {
assert(y.size() + 1 == rem.size());
u32 q = u32(((u64)rem[rem.size() - 1] * D + rem[rem.size() - 2]) / yb);
vecu yq = umul(y, vecu{q});
while (std::is_lt(ucmp(rem, yq))) --q, yq = usub(yq, y);
rem = usub(rem, yq);
while (std::is_lteq(ucmp(y, rem))) ++q, rem = usub(rem, y);
quo[i] = q;
}
if (i) rem.insert(rem.begin(), x[i - 1]);
}
shrink_(quo), shrink_(rem);
auto [q2, r2] = udivmod_1e8(rem, vecu{norm});
assert(is_0(r2));
return {quo, q2};
}
// 1 / a, abserr = B^{-deg}
static CEXP vecu inv_(vec_like CR a, u32 deg) {
assert(!a.empty() && D / 2 <= a.back() && a.back() < D);
u32 k = deg, c = (u32)a.size();
while (k > 64) k = (k + 1) / 2;
vecu z(c + k + 1);
z.back() = 1, z = udivmod_bf(z, a).first;
while (k < deg) {
vecu s = umul(z, z);
s.insert(s.begin(), 0);
cu32 d = min(c, 2 * k + 1);
cvecu t{a.end() - d, a.end()}, w2 = uadd(z, z);
vecu u = umul(s, t), w(k + 1);
u.erase(u.begin(), u.begin() + d), copy(w2, std::back_inserter(w));
(z = usub(w, u)).erase(z.begin()), k *= 2;
}
z.erase(z.begin(), z.begin() + k - deg);
return z;
}
static CEXP ptt<vecu> udivmod(vec_like CR a, vec_like CR b) NE {
if (assert(!is_0(b)); b.size() <= 64) return udivmod_bf(a, b);
if ((int)(a.size() - b.size()) <= 64) return udivmod_bf(a, b);
cu32 norm = D / (b.back() + 1);
cvecu x = umul(a, vecu{norm}), y = umul(b, vecu{norm});
cu32 s = (u32)x.size(), t = (u32)y.size(), deg = s + 2 - t;
cvecu z = inv_(y, deg);
vecu q = umul(x, z);
q.erase(q.begin(), q.begin() + t + deg);
vecu yq = umul(y, vecu{q});
while (std::is_lt(ucmp(x, yq))) q = usub(q, vecu{1}), yq = usub(yq, y);
vecu r = usub(x, yq);
while (std::is_lteq(ucmp(y, r))) q = uadd(q, vecu{1}), r = usub(r, y);
shrink_(q), shrink_(r);
auto [q2, r2] = udivmod_1e8(r, vecu{norm});
assert(is_0(r2));
return {q, q2};
}
#undef vec_like
};
} // namespace tifa_libs
#line 3 "test/cpv/aizu-ntl/ntl_2_a.cpp"
using namespace tifa_libs;
int main() {
std::cin.tie(nullptr)->std::ios::sync_with_stdio(false);
mpi a, b;
std::cin >> a >> b;
std::cout << a + b << '\n';
return 0;
}