// competitive-verifier: STANDALONE
#include "../../../src/comb/seq/fact/lib.hpp"
#include "../../../src/comb/seq/ifact/lib.hpp"
#include "../../../src/comb/seq/inv/lib.hpp"
#include "../../../src/comb/seq/invseq/lib.hpp"
#include "../../../src/comb/seq/ipowi/lib.hpp"
#include "../../../src/comb/seq/pows/lib.hpp"
#include "../../../src/math/ds/mint/md/lib.hpp"
#include "../../../src/math/ds/mint/md64/lib.hpp"
#include "../../../src/math/ds/mint/ms/lib.hpp"
#include "../../../src/math/ds/mint/ms64/lib.hpp"
#include "../../../src/math/qpow/basic/lib.hpp"
#include "../../../src/util/rand/lib.hpp"
#include "../base.hpp"
using namespace tifa_libs;
rand_gen<u64> g(1);
template <class mint>
void test_all(u32 n) {
[](u32 n) { // fact & ifact
timer_(auto fact = gen_fact<mint>(n));
timer_(auto ifact = gen_ifact<mint>(n));
mint bf = 1;
flt_ (u32, i, 0, n) {
if (i) bf *= i;
check(fact[i], bf, check_param(i));
check(ifact[i], bf.inv(), check_param(i));
}
}(n);
[](u32 n) { // inv
timer_(auto inv = gen_inv<mint>(n));
flt_ (u32, i, 1, n) {
auto bf = mint{i}.inv();
check(inv[i], bf, check_param(i));
}
}(n);
[](u32 n) { // invseq
vec<mint> seq;
timer_(std::generate_n(std::back_inserter(seq), n, [&] { return mint(g()); }));
auto invseq = gen_invseq(seq);
flt_ (u32, i, 0, n) {
auto bf = seq[i].inv();
check(invseq[i], bf, check_param(i), check_param(seq[i]));
}
}(n);
[](u32 n) { // ipowi
timer_(auto ipowi = gen_ipowi<mint>(n));
flt_ (u32, i, 0, n) {
auto bf = qpow<mint>(i, i);
check(ipowi[i], bf, check_param(i));
}
}(n);
auto test_pows = [](u32 n, u32 b) { // pows
timer_(auto pows = gen_pows<mint>(n, b));
flt_ (u32, i, 0, n) {
auto bf = qpow<mint>(i, b);
check(pows[i], bf, check_param(i), check_param(b));
}
};
timer_(test_pows(n, 0));
timer_(test_pows(n, 1));
timer_(test_pows(n, 114514));
timer_(test_pows(n, n));
}
using mints30 = mint_ms<998244353>;
using mints63 = mint_ms64<998244353>;
using mintd31 = mint_md<__LINE__>;
using mintd63 = mint_md64<__LINE__>;
int main() {
mintd31::set_mod(1000000000 + 7);
mintd63::set_mod(1000000000 + 7);
timer_(test_all<mints30>(1000));
timer_(test_all<mints63>(1000));
timer_(test_all<mintd31>(1000));
timer_(test_all<mintd63>(1000));
timer_(test_all<mints30>(1'000'000));
timer_(test_all<mints63>(1'000'000));
timer_(test_all<mintd31>(1'000'000));
timer_(test_all<mintd63>(1'000'000));
}
#line 1 "test/cpv_local/comb/gen.cpp"
// competitive-verifier: STANDALONE
#line 2 "src/comb/seq/fact/lib.hpp"
#line 2 "src/math/mul_mod/lib.hpp"
#line 2 "src/math/safe_mod/lib.hpp"
#line 2 "src/util/traits/math/lib.hpp"
// clang-format off
#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/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 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/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 5 "src/comb/seq/fact/lib.hpp"
namespace tifa_libs {
// i! from i=0..n-1
CEXP vecuu gen_fact(u32 n, u64 mod) NE {
retif_((n <= 1) [[unlikely]], vecuu(n, 1));
vecuu ans(n);
ans[0] = ans[1] = 1;
flt_ (u32, i, 2, n) ans[i] = mul_mod_u(ans[i - 1], i, mod);
return ans;
}
// i! from i=0..n-1
template <class mint>
CEXP vec<mint> gen_fact(u32 n) NE {
vec<mint> ans(n);
auto _ = gen_fact(n, mint::mod());
flt_ (u32, i, 0, n) ans[i] = _[i];
return ans;
}
} // namespace tifa_libs
#line 2 "src/comb/seq/ifact/lib.hpp"
#line 2 "src/comb/seq/inv/lib.hpp"
#line 5 "src/comb/seq/inv/lib.hpp"
namespace tifa_libs {
// i^{-1} from i=0..n-1
CEXP vecuu gen_inv(u32 n, u64 mod) NE {
retif_((n <= 1) [[unlikely]], vecuu(n, 1));
vecuu ans(n);
ans[0] = ans[1] = 1;
flt_ (u32, i, 2, n) ans[i] = mul_mod_u(mod - mod / i, ans[mod % i], mod);
return ans;
}
// i^{-1} from i=0..n-1
template <class mint>
CEXP vec<mint> gen_inv(u32 n) NE {
vec<mint> ans(n);
auto _ = gen_inv(n, mint::mod());
flt_ (u32, i, 0, n) ans[i] = _[i];
return ans;
}
} // namespace tifa_libs
#line 5 "src/comb/seq/ifact/lib.hpp"
namespace tifa_libs {
// (i!)^{-1} from i=0..n-1
CEXP vecuu gen_ifact(u32 n, u64 mod, vecuu inv) NE {
flt_ (u32, i, 2, n) inv[i] = mul_mod_u(inv[i], inv[i - 1], mod);
return inv;
}
// (i!)^{-1} from i=0..n-1
CEXP vecuu gen_ifact(u32 n, u64 mod) NE { return gen_ifact(n, mod, gen_inv(n, mod)); }
// (i!)^{-1} from i=0..n-1
template <class mint>
CEXP vec<mint> gen_ifact(u32 n, vec<mint> inv) NE {
flt_ (u32, i, 2, n) inv[i] *= inv[i - 1];
return inv;
}
// (i!)^{-1} from i=0..n-1
template <class mint>
CEXP vec<mint> gen_ifact(u32 n) NE { return gen_ifact(n, gen_inv<mint>(n)); }
} // namespace tifa_libs
#line 2 "src/comb/seq/invseq/lib.hpp"
#line 2 "src/nt/inverse/lib.hpp"
#line 2 "src/nt/gl/inv_gcd/lib.hpp"
#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 5 "src/comb/seq/invseq/lib.hpp"
namespace tifa_libs {
// i^{-1} for i in v
CEXP vecuu gen_invseq(spnuu v, u64 mod) NE {
cu32 n = (u32)v.size();
retif_((n == 0) [[unlikely]], {});
if (n == 1) return {inverse(v[0], mod)};
vecuu ans(n);
ans[0] = v[0];
flt_ (u32, i, 1, n) ans[i] = mul_mod_u(ans[i - 1], v[i], mod);
u64 _ = inverse(ans.back(), mod);
for (u32 i = n - 1; i; --i) ans[i] = mul_mod_u(_, ans[i - 1], mod), _ = mul_mod_u(_, v[i], mod);
ans[0] = _;
return ans;
}
// i^{-1} for i in v
template <class mint>
CEXP vec<mint> gen_invseq(vec<mint> CR v) NE {
u32 n = (u32)v.size();
vec<mint> ans(n);
vecuu v2(n);
flt_ (u32, i, 0, n) v2[i] = v[i].val();
auto _ = gen_invseq(v2, mint::mod());
flt_ (u32, i, 0, n) ans[i] = _[i];
return ans;
}
} // namespace tifa_libs
#line 2 "src/comb/seq/ipowi/lib.hpp"
#line 2 "src/math/iroot/sqrt/lib.hpp"
#line 4 "src/math/iroot/sqrt/lib.hpp"
namespace tifa_libs {
CEXP u32 isqrt(u64 x) NE {
retif_((!x) [[unlikely]], 0);
#pragma GCC diagnostic ignored "-Wconversion"
csint sh = 31 - (std::bit_width(x) - 1) / 2;
#pragma GCC diagnostic warning "-Wconversion"
u32 u = [](u64 x) NE {
CEXP arr<u8, 192> TAB{128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 144, 145, 146, 147, 148, 149, 150, 151, 151, 152, 153, 154, 155, 156, 156, 157, 158, 159, 160, 160, 161, 162, 163, 164, 164, 165, 166, 167, 167, 168, 169, 170, 170, 171, 172, 173, 173, 174, 175, 176, 176, 177, 178, 179, 179, 180, 181, 181, 182, 183, 183, 184, 185, 186, 186, 187, 188, 188, 189, 190, 190, 191, 192, 192, 193, 194, 194, 195, 196, 196, 197, 198, 198, 199, 200, 200, 201, 201, 202, 203, 203, 204, 205, 205, 206, 206, 207, 208, 208, 209, 210, 210, 211, 211, 212, 213, 213, 214, 214, 215, 216, 216, 217, 217, 218, 219, 219, 220, 220, 221, 221, 222, 223, 223, 224, 224, 225, 225, 226, 227, 227, 228, 228, 229, 229, 230, 230, 231, 232, 232, 233, 233, 234, 234, 235, 235, 236, 237, 237, 238, 238, 239, 239, 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245, 246, 246, 247, 247, 248, 248, 249, 249, 250, 250, 251, 251, 252, 252, 253, 253, 254, 254, 255, 255, 255};
u32 u = TAB[(x >> 56) - 64];
u = (u << 7) + (u32)(x >> 41) / u;
return (u << 15) + (u32)((x >> 17) / u);
}(x << 2 * sh);
u >>= sh, u -= (u64)u * u > x;
return u;
}
} // namespace tifa_libs
#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 2 "src/nt/lsieve/impl1/lib.hpp"
#line 4 "src/nt/lsieve/impl1/lib.hpp"
namespace tifa_libs {
template <class... Ts>
struct lsieve : Ts... {
vecb not_prime;
vecu primes;
CEXPE lsieve(u32 n) NE : Ts(n)..., not_prime(n) {
if (n < 2) return;
// clang-format off
primes.reserve((usz)max(127, int(n * (n >= 2e5 ? 1.6 : 1.7) / std::bit_width(n) + 1)));
// clang-format on
flt_ (u32, i, 2, n) {
if (!not_prime[i]) primes.push_back(i), (Ts::prime(i), ...);
for (auto j : primes) {
if (i * j >= n) break;
not_prime[i * j] = true;
if (i % j) (Ts::coprime(i, j), ...);
else {
(Ts::not_coprime(i, j), ...);
break;
}
}
}
primes.shrink_to_fit();
}
};
} // namespace tifa_libs
#line 2 "src/nt/mod/barrett/lib.hpp"
#line 4 "src/nt/mod/barrett/lib.hpp"
namespace tifa_libs {
template <u64 MOD, u64 B_ = 1>
struct barrett {
static CEXP u64 B = B_ % MOD, R = ((u128)B << 64) / MOD;
static CEXP u64 reduce(u64 a) NE {
if (u64 q = u64((u128)a * R >> 64); (a = a * B - q * MOD) >= MOD) a -= MOD;
return a;
}
};
template <> // dynamic
struct barrett<0> {
u64 mod, b, r;
CEXP barrett() NE = default;
CEXPE barrett(u64 mod, u64 b = 1) NE { reset(mod, b); }
CEXP void reset(u64 mod_, u64 b_ = 1) NE { assert(mod_), mod = mod_, b = b_ % mod, r = (u64(((u128)b << 64) / mod)); }
ND CEXP u64 reduce(u64 a) CNE {
if (cu64 q = u64((u128)a * r >> 64); (a = a * b - q * mod) >= mod) a -= mod;
return a;
}
};
} // namespace tifa_libs
#line 7 "src/comb/seq/ipowi/lib.hpp"
namespace tifa_libs {
namespace gen_ipowi_impl_ {
struct ls_ipowi {
static inline u64 mod;
vecuu ipowi;
protected:
cu32 B;
vvecuu fp, fpb;
u32 pre_j{0}, gap{0};
u64 now_r{1};
arr<u64, 355> p{1}; // maximal prime gaps: g_n=354 => p_n=4,302,407,359
CEXPE ls_ipowi(u32 n) NE : ipowi(n), B{isqrt(n)}, fp(B + 1, vecuu(B + 1)), fpb(B + 1, vecuu(B + 1)) {
if (n) ipowi[0] = 1;
if (n > 1) ipowi[1] = 1;
}
void prime(u32 p) NE {
if (ipowi[p] = qpow_mod(p, p, mod); p <= B) {
fp[p][0] = 1;
barrett<0> brt(mod, ipowi[p]);
flt_ (u32, j, 1, B + 1)
fp[p][j] = brt.reduce(fp[p][j - 1]);
fpb[p][0] = 1;
brt.reset(mod, fp[p].back());
flt_ (u32, j, 1, B + 1)
fpb[p][j] = brt.reduce(fpb[p][j - 1]);
}
}
void coprime(u32 i, u32 j) NE {
if (j <= pre_j) now_r = 1, pre_j = gap = 0;
const barrett<0> brt(mod, ipowi[i]);
if (cu32 new_gap = j - pre_j; new_gap > gap) {
flt_ (u32, x, gap + 1, new_gap + 1) p[x] = brt.reduce(p[x - 1]);
gap = new_gap;
}
now_r = mul_mod_u(now_r, p[j - pre_j], mod);
ipowi[i * j] = mul_mod_u(mul_mod_u(fp[j][i % B], fpb[j][i / B], mod), now_r, mod);
pre_j = j;
}
void not_coprime(u32 i, u32 j) NE { coprime(i, j); }
};
} // namespace gen_ipowi_impl_
// i^i from i=0..n-1
CEXP vecuu gen_ipowi(u32 n, u64 mod) NE {
retif_((n < 2) [[unlikely]], vecuu(n, 1));
gen_ipowi_impl_::ls_ipowi::mod = mod;
return lsieve<gen_ipowi_impl_::ls_ipowi>(n).ipowi;
}
// i^i from i=0..n-1
template <class mint>
CEXP vec<mint> gen_ipowi(u32 n) NE {
vec<mint> ans(n);
auto _ = gen_ipowi(n, mint::mod());
flt_ (u32, i, 0, n) ans[i] = _[i];
return ans;
}
} // namespace tifa_libs
#line 2 "src/comb/seq/pows/lib.hpp"
#line 5 "src/comb/seq/pows/lib.hpp"
namespace tifa_libs {
namespace gen_pows_impl_ {
struct ls_pows {
static inline u64 b, mod;
vecuu pows;
protected:
CEXPE ls_pows(u32 n) NE : pows(n) {
if (n > 1) pows[1] = 1;
}
void prime(u32 p) NE { pows[p] = qpow_mod(p, b, mod); }
void coprime(u32 i, u32 j) NE { pows[i * j] = mul_mod_u(pows[i], pows[j], mod); }
void not_coprime(u32 i, u32 j) NE { coprime(i, j); }
};
} // namespace gen_pows_impl_
// i^{b} from i=0..n-1
CEXP vecuu gen_pows(u32 n, u64 b, u64 mod) NE {
retif_((!b) [[unlikely]], vecuu(n, mod > 1));
retif_((!n) [[unlikely]], {});
gen_pows_impl_::ls_pows::b = b;
gen_pows_impl_::ls_pows::mod = mod;
return lsieve<gen_pows_impl_::ls_pows>(n).pows;
}
// i^{b} from i=0..n-1
template <class mint>
CEXP vec<mint> gen_pows(u32 n, u64 b) NE {
vec<mint> ans(n);
auto _ = gen_pows(n, b, mint::mod());
flt_ (u32, i, 0, n) ans[i] = _[i];
return ans;
}
} // namespace tifa_libs
#line 2 "src/math/ds/mint/md/lib.hpp"
#line 2 "src/nt/mod/montgomery/lib.hpp"
#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/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/md/lib.hpp"
namespace tifa_libs {
template <i64 ID>
class mint_md_tag : public mint_impl_::mint_tag_base {
static inline montgomery<0> core;
public:
static CEXP bool FIXED_MOD = false;
static CEXP void set_mod(u32 m) NE { core.reset(m); }
protected:
using raw_t = u32;
raw_t v_{};
CEXP mint_md_tag() NE = default;
CEXP mint_md_tag(int_c auto v) NE : v_{mod(v)} {}
public:
static CEXP raw_t mod(sint_c auto v) NE { retif_((v >= 0) [[likely]], mod((to_uint_t<decltype(v)>)v), core.tsf(core.norm(i32(v % (i32)mod())))); }
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.tsf((raw_t)x), core.tsf((raw_t)(x % mod())));
} else retif_((v < mod()) [[likely]], core.tsf((raw_t)v), core.tsf((raw_t)(v % mod())));
}
static CEXP raw_t mod() NE { return core.MOD; }
ND CEXP raw_t val() CNE {
retif_((!core.OFFSET) [[likely]], core.reduce(v_));
const raw_t h = core.reduce(v_ >> core.OFFSET);
return ((h - v_) * core.R & core.MASK) * core.MOD_ODD + h;
}
CEXP raw_t& data() NE { return v_; }
protected:
template <class mint>
ND CEXP auto neg() CNE {
mint res;
if (!core.OFFSET) [[likely]] {
res.v_ = (core.MOD_ODD & -raw_t(v_ != 0)) - v_;
return res;
}
const raw_t h = v_ >> core.OFFSET;
res.v_ = (((core.MOD_ODD & -raw_t(h != 0)) - h) << core.OFFSET) | (-v_ & core.MASK);
return res;
}
CEXP void add(mint_md_tag CR r) NE {
if (!core.OFFSET) [[likely]] {
const raw_t h = v_ + r.v_ - core.MOD_ODD;
v_ = h + (core.MOD_ODD & -(h >> 31));
return;
}
const raw_t h = (v_ >> core.OFFSET) + (r.v_ >> core.OFFSET) - core.MOD_ODD;
v_ = ((h + (core.MOD_ODD & -(h >> 31))) << core.OFFSET) | ((v_ + r.v_) & core.MASK);
}
CEXP void sub(mint_md_tag CR r) NE {
if (!core.OFFSET) [[likely]] {
const raw_t h = v_ - r.v_;
v_ = h + (core.MOD_ODD & -(h >> 31));
return;
}
const raw_t h = (v_ >> core.OFFSET) - (r.v_ >> core.OFFSET);
v_ = ((h + (core.MOD_ODD & -(h >> 31))) << core.OFFSET) | ((v_ - r.v_) & core.MASK);
}
CEXP void mul(mint_md_tag CR r) NE {
if (!core.OFFSET) [[likely]] {
v_ = core.reduce((u64)v_ * r.v_);
return;
}
v_ = (core.reduce((u64)(v_ >> core.OFFSET) * (r.v_ >> core.OFFSET)) << core.OFFSET) | ((v_ * r.v_) & core.MASK);
}
};
template <i64 ID>
using mint_md = mint_impl_::mint<mint_md_tag<ID>>;
} // namespace tifa_libs
#line 2 "src/math/ds/mint/md64/lib.hpp"
#line 2 "src/nt/mod/montgomery64/lib.hpp"
#line 4 "src/nt/mod/montgomery64/lib.hpp"
namespace tifa_libs {
template <u64 MOD>
struct montgomery64 {
static CEXP u64 R = [] {
u64 iv = MOD * (2 - MOD * MOD);
iv *= 2 - MOD * iv, iv *= 2 - MOD * iv, iv *= 2 - MOD * iv;
return iv * (2 - MOD * iv);
}();
static CEXP u64 R2 = [] {
u64 iv = -MOD % MOD;
for (u32 i = 0; i != 64; ++i)
if ((iv *= 2) >= MOD) iv -= MOD;
return iv;
}();
static_assert(MOD & 1);
static_assert(R * MOD == 1);
static_assert((MOD >> 63) == 0);
static_assert(MOD != 1);
static CEXP u64 mulh(u64 x, u64 y) NE { return u64((u128)x * y >> 64); }
static CEXP u64 redc_mul(u64 x, u64 y) NE {
u64 res = mulh(x, y) - mulh(x * y * R, MOD);
return res + (MOD & -(res >> 63));
}
static CEXP u64 norm(i64 x) NE { return (u64)x + (MOD & u64(-(x < 0))); }
};
template <> // dynamic
struct montgomery64<0> {
u64 MOD, R, R2;
CEXP montgomery64() NE = default;
CEXPE montgomery64(u64 m) NE { reset(m); }
CEXP void reset(u64 m) NE {
assert(!((m & 1) == 0 || m == 1 || m >> 63)), MOD = m;
u64 iv = MOD * (2 - MOD * MOD);
iv *= 2 - MOD * iv, iv *= 2 - MOD * iv, iv *= 2 - MOD * iv, R = iv * (2 - MOD * iv), R2 = -MOD % MOD;
flt_ (u32, i, 0, 64)
if ((R2 *= 2) >= MOD) R2 -= MOD;
}
ND CEXP u64 mul_h(u64 x, u64 y) CNE { return u64((u128)x * y >> 64); }
ND CEXP u64 redc_mul(u64 x, u64 y) CNE {
cu64 res = mul_h(x, y) - mul_h(x * y * R, MOD);
return res + (MOD & -(res >> 63));
}
ND CEXP u64 norm(i64 x) CNE { return u64(x + i64(MOD & u64(-(x < 0)))); }
};
} // namespace tifa_libs
#line 5 "src/math/ds/mint/md64/lib.hpp"
namespace tifa_libs {
template <i64 ID>
class mint_md64_tag : public mint_impl_::mint_tag_base {
static inline montgomery64<0> core;
public:
static CEXP bool FIXED_MOD = false;
static CEXP void set_mod(u64 m) NE { core.reset(m); }
protected:
using raw_t = u64;
raw_t v_{};
CEXP mint_md64_tag() NE = default;
CEXP mint_md64_tag(int_c auto v) NE : v_{mod(v)} {}
public:
static CEXP raw_t mod(sint_c auto v) NE { retif_((v >= 0) [[likely]], mod((to_uint_t<decltype(v)>)v), core.redc_mul(core.norm(i64(v % (i64)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.redc_mul(x, core.R2), core.redc_mul(x % mod(), core.R2));
} else retif_((v < mod()) [[likely]], core.redc_mul((raw_t)v, core.R2), core.redc_mul((raw_t)(v % mod()), core.R2));
}
static CEXP raw_t mod() NE { return core.MOD; }
ND CEXP raw_t val() CNE {
const raw_t res = -core.mul_h(v_ * core.R, mod());
return res + (mod() & -(res >> 63));
}
CEXP raw_t& data() NE { return v_; }
protected:
template <class mint>
CEXP auto neg() CNE {
mint res;
res.v_ = (mod() & -raw_t(v_ != 0)) - v_;
return res;
}
CEXP void add(mint_md64_tag CR r) NE { v_ += r.v_ - mod(), v_ += mod() & -(v_ >> 63); }
CEXP void sub(mint_md64_tag CR r) NE { v_ -= r.v_, v_ += mod() & -(v_ >> 63); }
CEXP void mul(mint_md64_tag CR r) NE { v_ = core.redc_mul(v_, r.v_); }
};
template <i64 ID>
using mint_md64 = mint_impl_::mint<mint_md64_tag<ID>>;
} // namespace tifa_libs
#line 2 "src/math/ds/mint/ms/lib.hpp"
#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/math/ds/mint/ms64/lib.hpp"
#line 5 "src/math/ds/mint/ms64/lib.hpp"
namespace tifa_libs {
template <u64 MOD>
class mint_ms64_tag : public mint_impl_::mint_tag_base {
using core = montgomery64<MOD>;
public:
static CEXP bool FIXED_MOD = true;
protected:
using raw_t = u64;
raw_t v_{};
CEXP mint_ms64_tag() NE = default;
CEXP mint_ms64_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::redc_mul((u64)v, core::R2));
}
return core::redc_mul(core::norm(i64(v % (i64)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::redc_mul(x, core::R2), core::redc_mul(x % mod(), core::R2));
} else retif_((v < mod()) [[likely]], core::redc_mul((u64)v, core::R2), core::redc_mul((u64)(v % mod()), core::R2));
}
static CEXP raw_t mod() NE { return MOD; }
ND CEXP raw_t val() CNE {
const raw_t res = -core::mulh(this->v_ * core::R, mod());
return res + (mod() & -(res >> 63));
}
CEXP raw_t& data() NE { return v_; }
protected:
template <class mint>
CEXP auto neg() CNE {
mint res;
res.v_ = (mod() & -raw_t(this->v_ != 0)) - v_;
return res;
}
CEXP void add(mint_ms64_tag CR r) NE { v_ += r.v_ - mod(), v_ += mod() & -(this->v_ >> 63); }
CEXP void sub(mint_ms64_tag CR r) NE { v_ -= r.v_, v_ += mod() & -(this->v_ >> 63); }
CEXP void mul(mint_ms64_tag CR r) NE { v_ = core::redc_mul(this->v_, r.v_); }
};
template <u64 MOD>
using mint_ms64 = mint_impl_::mint<mint_ms64_tag<MOD>>;
} // namespace tifa_libs
#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/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 "test/cpv_local/base.hpp"
#line 2 "src/io/container/lib.hpp"
#line 4 "src/io/container/lib.hpp"
namespace tifa_libs {
auto& operator>>(tifa_libs::istream_c auto& is, tifa_libs::container_c auto& x) NE {
for (auto& i : x) is >> i;
return is;
}
auto& operator<<(tifa_libs::ostream_c auto& os, tifa_libs::container_c auto CR x) NE {
if (begin(x) == end(x)) [[unlikely]]
return os;
auto it = begin(x);
for (os << *it++; it != end(x); ++it) os << ' ' << *it;
return os;
}
} // namespace tifa_libs
#line 2 "src/io/i128/lib.hpp"
#line 5 "src/io/i128/lib.hpp"
namespace tifa_libs {
namespace ios128_impl_ {
auto& read(tifa_libs::istream_c auto& is, tifa_libs::u128_c auto& n) {
static strn int_buf;
int_buf.reserve(43), n = 0, is >> int_buf;
for (u32 i = 0; i + 1 < int_buf.size(); i += 2) (n *= 100) += STR2U16[*(u16*)(int_buf.data() + i)];
if (int_buf.size() & 1) (n *= 10) += int_buf.back() & 15;
return is;
}
} // namespace ios128_impl_
auto& operator>>(tifa_libs::istream_c auto& is, tifa_libs::s128_c auto& n) NE {
bool neg = false;
if CEXP (requires { is.skip_nnegdigit(); }) neg = (is.skip_nnegdigit().peek() == '-' && is.get_unchk());
else
while (!neg && !isdigit(is.peek())) {
if (is.peek() == '-') neg = true;
is.get();
}
u128 n_ = 0;
if (ios128_impl_::read(is, n_); neg) n_ = -n_;
n = (i128)n_;
return is;
}
auto& operator>>(tifa_libs::istream_c auto& is, tifa_libs::u128_c auto& n) NE {
if CEXP (requires { is.skip_ndigit(); }) is.skip_ndigit();
else
while (!isdigit(is.peek())) is.get();
return ios128_impl_::read(is, n);
}
auto& operator<<(tifa_libs::ostream_c auto& os, tifa_libs::u128_c auto n) NE {
static strn int_buf(40, '\0');
auto it = end(int_buf);
do *(--(it)) = chr(n % 10) | '0';
while (n /= 10);
return os << int_buf.substr(usz(it - begin(int_buf)));
}
auto& operator<<(tifa_libs::ostream_c auto& os, tifa_libs::s128_c auto n) NE {
if (n < 0) return os << '-' << -(u128)n;
return os << (u128)n;
}
} // namespace tifa_libs
#line 2 "src/io/pair/lib.hpp"
#line 4 "src/io/pair/lib.hpp"
namespace tifa_libs {
template <class T, class U>
auto& operator>>(tifa_libs::istream_c auto& is, std::pair<T, U>& p) NE { return is >> p.first >> p.second; }
template <class T, class U>
auto& operator<<(tifa_libs::ostream_c auto& os, std::pair<T, U> CR p) NE { return os << p.first << ' ' << p.second; }
} // namespace tifa_libs
#line 2 "src/io/tuple/lib.hpp"
#line 4 "src/io/tuple/lib.hpp"
namespace tifa_libs {
template <class... Ts>
auto& operator>>(tifa_libs::istream_c auto& is, std::tuple<Ts...>& p) NE {
std::apply([&](Ts&... ts) NE { (is >> ... >> ts); }, p);
return is;
}
template <class... Ts>
auto& operator<<(tifa_libs::ostream_c auto& os, std::tuple<Ts...> CR p) NE {
std::apply([&, n = 0](Ts const&... ts) mutable NE { ((os << ts << (++n != sizeof...(Ts) ? " " : "")), ...); }, p);
return os;
}
} // namespace tifa_libs
#line 7 "test/cpv_local/base.hpp"
namespace tifa_libs {
namespace detail__ {
template <class T>
strn to_str(T CR x) NE {
std::stringstream ss;
ss << std::fixed << std::setprecision(12) << x;
auto str = ss.str();
retif_((str.length() <= 1024), str, std::format("{}... (length = {})", str.substr(0, 1024), std::to_string(str.length())));
}
template <class T, class... Ts>
void check_(strnv pretty_func, strnv got_str, T CR got, strnv want_str, T CR want, Ts... param) {
if CEXP (sizeof...(param) == 0) {
if (got != want) throw std::runtime_error(std::format("{}: got \"{}\" = {}, want \"{}\" = {}", pretty_func, got_str, to_str(got), want_str, to_str(want)));
} else {
if (got != want) throw std::runtime_error(std::format("{}: got \"{}\" = {}, want \"{}\" = {} with", pretty_func, got_str, to_str(got), want_str, to_str(want)) + (std::format(" {} = {};", param.first, ::tifa_libs::detail__::to_str(param.second)) + ...));
}
}
template <class... Ts>
void check_bool_(strnv pretty_func, strnv expression, bool res, Ts... param) {
if CEXP (sizeof...(param) == 0) {
if (!res) throw std::runtime_error(std::format("{} :\"{}\" failed", pretty_func, expression));
} else {
if (!res) throw std::runtime_error(std::format("{} :\"{}\" failed with", pretty_func, expression) + (std::format(" {} = {};", param.first, ::tifa_libs::detail__::to_str(param.second)) + ...));
}
}
} // namespace detail__
template <class clock_t = std::chrono::high_resolution_clock, class duration_t = std::chrono::microseconds>
requires specialized_from_v<duration_t, std::chrono::duration>
struct timer {
void tic(int line_num) NE {
s_line_num = line_num;
const std::lock_guard<std::mutex> lock(s_clock_mutex);
s_clock_start = clock_t::now();
}
void tac() NE {
const std::lock_guard<std::mutex> lock(s_clock_mutex);
s_clock_end = clock_t::now();
}
auto passed() CNE { return std::chrono::duration_cast<duration_t>(s_clock_end - s_clock_start); }
operator strn() { return std::format("{} passed in line {}", passed(), s_line_num); }
private:
std::mutex s_clock_mutex;
std::chrono::time_point<clock_t> s_clock_start, s_clock_end;
int s_line_num;
};
inline timer default_timer;
#define timer_(...) \
::tifa_libs::default_timer.tic(__LINE__); \
__VA_ARGS__; \
::tifa_libs::default_timer.tac(); \
std::cerr << (strn)::tifa_libs::default_timer << '\n'
#define check(got, want, ...) ::tifa_libs::detail__::check_(__PRETTY_FUNCTION__, #got, got, #want, want __VA_OPT__(, ) __VA_ARGS__)
#define check_bool(expression, ...) ::tifa_libs::detail__::check_bool_(__PRETTY_FUNCTION__, #expression, expression __VA_OPT__(, ) __VA_ARGS__)
#define check_param(x) \
std::pair<std::string, decltype(x)> { #x, x }
} // namespace tifa_libs
#line 15 "test/cpv_local/comb/gen.cpp"
using namespace tifa_libs;
rand_gen<u64> g(1);
template <class mint>
void test_all(u32 n) {
[](u32 n) { // fact & ifact
timer_(auto fact = gen_fact<mint>(n));
timer_(auto ifact = gen_ifact<mint>(n));
mint bf = 1;
flt_ (u32, i, 0, n) {
if (i) bf *= i;
check(fact[i], bf, check_param(i));
check(ifact[i], bf.inv(), check_param(i));
}
}(n);
[](u32 n) { // inv
timer_(auto inv = gen_inv<mint>(n));
flt_ (u32, i, 1, n) {
auto bf = mint{i}.inv();
check(inv[i], bf, check_param(i));
}
}(n);
[](u32 n) { // invseq
vec<mint> seq;
timer_(std::generate_n(std::back_inserter(seq), n, [&] { return mint(g()); }));
auto invseq = gen_invseq(seq);
flt_ (u32, i, 0, n) {
auto bf = seq[i].inv();
check(invseq[i], bf, check_param(i), check_param(seq[i]));
}
}(n);
[](u32 n) { // ipowi
timer_(auto ipowi = gen_ipowi<mint>(n));
flt_ (u32, i, 0, n) {
auto bf = qpow<mint>(i, i);
check(ipowi[i], bf, check_param(i));
}
}(n);
auto test_pows = [](u32 n, u32 b) { // pows
timer_(auto pows = gen_pows<mint>(n, b));
flt_ (u32, i, 0, n) {
auto bf = qpow<mint>(i, b);
check(pows[i], bf, check_param(i), check_param(b));
}
};
timer_(test_pows(n, 0));
timer_(test_pows(n, 1));
timer_(test_pows(n, 114514));
timer_(test_pows(n, n));
}
using mints30 = mint_ms<998244353>;
using mints63 = mint_ms64<998244353>;
using mintd31 = mint_md<__LINE__>;
using mintd63 = mint_md64<__LINE__>;
int main() {
mintd31::set_mod(1000000000 + 7);
mintd63::set_mod(1000000000 + 7);
timer_(test_all<mints30>(1000));
timer_(test_all<mints63>(1000));
timer_(test_all<mintd31>(1000));
timer_(test_all<mintd63>(1000));
timer_(test_all<mints30>(1'000'000));
timer_(test_all<mints63>(1'000'000));
timer_(test_all<mintd31>(1'000'000));
timer_(test_all<mintd63>(1'000'000));
}