This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub suisen-cp/cp-library-cpp
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" #include <iostream> #include <random> #include <atcoder/modint> #include "library/polynomial/fps.hpp" #include "library/math/factorial_large.hpp" template <int MOD> void test() { using mint = atcoder::static_modint<MOD>; std::mt19937 rng{}; constexpr int MAX_N = 10000000; std::uniform_int_distribution<int> dist(0, MAX_N); const int q = 10000; std::vector<int> ns(q); for (int& e : ns) e = dist(rng); // power of 2 for (int i = 0; 1 << i < MAX_N; ++i) ns[i] = 1 << i; std::vector<mint> res1, res2; { suisen::factorial_large<mint> fac{}; fac.threshold = -1; res1.resize(q); for (int i = 0; i < q; ++i) res1[i] = fac.fac(ns[i]); } { suisen::factorial<mint> fac{}; res2.resize(q); for (int i = 0; i < q; ++i) res2[i] = fac.fac(ns[i]); } assert(res1 == res2); } int main() { test<998244353>(); test<1000000007>(); test<1000000009>(); std::cout << "Hello World" << std::endl; return 0; }
#line 1 "test/src/math/factorial_large/dummy.test.cpp" #define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" #include <iostream> #include <random> #include <atcoder/modint> #line 1 "library/polynomial/fps.hpp" #include <algorithm> #include <cassert> #line 7 "library/polynomial/fps.hpp" #include <queue> #line 1 "library/polynomial/fps_naive.hpp" #line 5 "library/polynomial/fps_naive.hpp" #include <cmath> #include <limits> #include <type_traits> #include <vector> #line 1 "library/type_traits/type_traits.hpp" #line 7 "library/type_traits/type_traits.hpp" namespace suisen { template <typename ...Constraints> using constraints_t = std::enable_if_t<std::conjunction_v<Constraints...>, std::nullptr_t>; template <typename T, typename = std::nullptr_t> struct bitnum { static constexpr int value = 0; }; template <typename T> struct bitnum<T, constraints_t<std::is_integral<T>>> { static constexpr int value = std::numeric_limits<std::make_unsigned_t<T>>::digits; }; template <typename T> static constexpr int bitnum_v = bitnum<T>::value; template <typename T, size_t n> struct is_nbit { static constexpr bool value = bitnum_v<T> == n; }; template <typename T, size_t n> static constexpr bool is_nbit_v = is_nbit<T, n>::value; template <typename T, typename = std::nullptr_t> struct safely_multipliable { using type = T; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 32>>> { using type = long long; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 64>>> { using type = __int128_t; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 32>>> { using type = unsigned long long; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 64>>> { using type = __uint128_t; }; template <typename T> using safely_multipliable_t = typename safely_multipliable<T>::type; template <typename T, typename = void> struct rec_value_type { using type = T; }; template <typename T> struct rec_value_type<T, std::void_t<typename T::value_type>> { using type = typename rec_value_type<typename T::value_type>::type; }; template <typename T> using rec_value_type_t = typename rec_value_type<T>::type; template <typename T> class is_iterable { template <typename T_> static auto test(T_ e) -> decltype(e.begin(), e.end(), std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval<T>()))::value; }; template <typename T> static constexpr bool is_iterable_v = is_iterable<T>::value; template <typename T> class is_writable { template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::ostream&>() << e, std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval<T>()))::value; }; template <typename T> static constexpr bool is_writable_v = is_writable<T>::value; template <typename T> class is_readable { template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::istream&>() >> e, std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval<T>()))::value; }; template <typename T> static constexpr bool is_readable_v = is_readable<T>::value; } // namespace suisen #line 11 "library/polynomial/fps_naive.hpp" #line 1 "library/math/modint_extension.hpp" #line 5 "library/math/modint_extension.hpp" #include <optional> /** * refernce: https://37zigen.com/tonelli-shanks-algorithm/ * calculates x s.t. x^2 = a mod p in O((log p)^2). */ template <typename mint> std::optional<mint> safe_sqrt(mint a) { static int p = mint::mod(); if (a == 0) return std::make_optional(0); if (p == 2) return std::make_optional(a); if (a.pow((p - 1) / 2) != 1) return std::nullopt; mint b = 1; while (b.pow((p - 1) / 2) == 1) ++b; static int tlz = __builtin_ctz(p - 1), q = (p - 1) >> tlz; mint x = a.pow((q + 1) / 2); b = b.pow(q); for (int shift = 2; x * x != a; ++shift) { mint e = a.inv() * x * x; if (e.pow(1 << (tlz - shift)) != 1) x *= b; b *= b; } return std::make_optional(x); } /** * calculates x s.t. x^2 = a mod p in O((log p)^2). * if not exists, raises runtime error. */ template <typename mint> auto sqrt(mint a) -> decltype(mint::mod(), mint()) { return *safe_sqrt(a); } template <typename mint> auto log(mint a) -> decltype(mint::mod(), mint()) { assert(a == 1); return 0; } template <typename mint> auto exp(mint a) -> decltype(mint::mod(), mint()) { assert(a == 0); return 1; } template <typename mint, typename T> auto pow(mint a, T b) -> decltype(mint::mod(), mint()) { return a.pow(b); } template <typename mint> auto inv(mint a) -> decltype(mint::mod(), mint()) { return a.inv(); } #line 1 "library/math/inv_mods.hpp" #line 5 "library/math/inv_mods.hpp" namespace suisen { template <typename mint> class inv_mods { public: inv_mods() = default; inv_mods(int n) { ensure(n); } const mint& operator[](int i) const { ensure(i); return invs[i]; } static void ensure(int n) { int sz = invs.size(); if (sz < 2) invs = { 0, 1 }, sz = 2; if (sz < n + 1) { invs.resize(n + 1); for (int i = sz; i <= n; ++i) invs[i] = mint(mod - mod / i) * invs[mod % i]; } } private: static std::vector<mint> invs; static constexpr int mod = mint::mod(); }; template <typename mint> std::vector<mint> inv_mods<mint>::invs{}; template <typename mint> std::vector<mint> get_invs(const std::vector<mint>& vs) { const int n = vs.size(); mint p = 1; for (auto& e : vs) { p *= e; assert(e != 0); } mint ip = p.inv(); std::vector<mint> rp(n + 1); rp[n] = 1; for (int i = n - 1; i >= 0; --i) { rp[i] = rp[i + 1] * vs[i]; } std::vector<mint> res(n); for (int i = 0; i < n; ++i) { res[i] = ip * rp[i + 1]; ip *= vs[i]; } return res; } } #line 14 "library/polynomial/fps_naive.hpp" namespace suisen { template <typename T> struct FPSNaive : std::vector<T> { static inline int MAX_SIZE = std::numeric_limits<int>::max() / 2; using value_type = T; using element_type = rec_value_type_t<T>; using std::vector<value_type>::vector; FPSNaive(const std::initializer_list<value_type> l) : std::vector<value_type>::vector(l) {} FPSNaive(const std::vector<value_type>& v) : std::vector<value_type>::vector(v) {} static void set_max_size(int n) { FPSNaive<T>::MAX_SIZE = n; } const value_type operator[](int n) const { return n <= deg() ? unsafe_get(n) : value_type{ 0 }; } value_type& operator[](int n) { return ensure_deg(n), unsafe_get(n); } int size() const { return std::vector<value_type>::size(); } int deg() const { return size() - 1; } int normalize() { while (size() and this->back() == value_type{ 0 }) this->pop_back(); return deg(); } FPSNaive& cut_inplace(int n) { if (size() > n) this->resize(std::max(0, n)); return *this; } FPSNaive cut(int n) const { FPSNaive f = FPSNaive(*this).cut_inplace(n); return f; } FPSNaive operator+() const { return FPSNaive(*this); } FPSNaive operator-() const { FPSNaive f(*this); for (auto& e : f) e = -e; return f; } FPSNaive& operator++() { return ++(*this)[0], * this; } FPSNaive& operator--() { return --(*this)[0], * this; } FPSNaive& operator+=(const value_type x) { return (*this)[0] += x, *this; } FPSNaive& operator-=(const value_type x) { return (*this)[0] -= x, *this; } FPSNaive& operator+=(const FPSNaive& g) { ensure_deg(g.deg()); for (int i = 0; i <= g.deg(); ++i) unsafe_get(i) += g.unsafe_get(i); return *this; } FPSNaive& operator-=(const FPSNaive& g) { ensure_deg(g.deg()); for (int i = 0; i <= g.deg(); ++i) unsafe_get(i) -= g.unsafe_get(i); return *this; } FPSNaive& operator*=(const FPSNaive& g) { return *this = *this * g; } FPSNaive& operator*=(const value_type x) { for (auto& e : *this) e *= x; return *this; } FPSNaive& operator/=(const FPSNaive& g) { return *this = *this / g; } FPSNaive& operator%=(const FPSNaive& g) { return *this = *this % g; } FPSNaive& operator<<=(const int shamt) { this->insert(this->begin(), shamt, value_type{ 0 }); return *this; } FPSNaive& operator>>=(const int shamt) { if (shamt > size()) this->clear(); else this->erase(this->begin(), this->begin() + shamt); return *this; } friend FPSNaive operator+(FPSNaive f, const FPSNaive& g) { f += g; return f; } friend FPSNaive operator+(FPSNaive f, const value_type& x) { f += x; return f; } friend FPSNaive operator-(FPSNaive f, const FPSNaive& g) { f -= g; return f; } friend FPSNaive operator-(FPSNaive f, const value_type& x) { f -= x; return f; } friend FPSNaive operator*(const FPSNaive& f, const FPSNaive& g) { if (f.empty() or g.empty()) return FPSNaive{}; const int n = f.size(), m = g.size(); FPSNaive h(std::min(MAX_SIZE, n + m - 1)); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { if (i + j >= MAX_SIZE) break; h.unsafe_get(i + j) += f.unsafe_get(i) * g.unsafe_get(j); } return h; } friend FPSNaive operator*(FPSNaive f, const value_type& x) { f *= x; return f; } friend FPSNaive operator/(FPSNaive f, const FPSNaive& g) { return std::move(f.div_mod(g).first); } friend FPSNaive operator%(FPSNaive f, const FPSNaive& g) { return std::move(f.div_mod(g).second); } friend FPSNaive operator*(const value_type x, FPSNaive f) { f *= x; return f; } friend FPSNaive operator<<(FPSNaive f, const int shamt) { f <<= shamt; return f; } friend FPSNaive operator>>(FPSNaive f, const int shamt) { f >>= shamt; return f; } std::pair<FPSNaive, FPSNaive> div_mod(FPSNaive g) const { FPSNaive f = *this; const int fd = f.normalize(), gd = g.normalize(); assert(gd >= 0); if (fd < gd) return { FPSNaive{}, f }; if (gd == 0) return { f *= g.unsafe_get(0).inv(), FPSNaive{} }; const int k = f.deg() - gd; value_type head_inv = g.unsafe_get(gd).inv(); FPSNaive q(k + 1); for (int i = k; i >= 0; --i) { value_type div = f.unsafe_get(i + gd) * head_inv; q.unsafe_get(i) = div; for (int j = 0; j <= gd; ++j) f.unsafe_get(i + j) -= div * g.unsafe_get(j); } f.cut_inplace(gd); f.normalize(); return { q, f }; } friend bool operator==(const FPSNaive& f, const FPSNaive& g) { const int n = f.size(), m = g.size(); if (n < m) return g == f; for (int i = 0; i < m; ++i) if (f.unsafe_get(i) != g.unsafe_get(i)) return false; for (int i = m; i < n; ++i) if (f.unsafe_get(i) != 0) return false; return true; } friend bool operator!=(const FPSNaive& f, const FPSNaive& g) { return not (f == g); } FPSNaive mul(const FPSNaive& g, int n = -1) const { if (n < 0) n = size(); if (this->empty() or g.empty()) return FPSNaive{}; const int m = size(), k = g.size(); FPSNaive h(std::min(n, m + k - 1)); for (int i = 0; i < m; ++i) { for (int j = 0, jr = std::min(k, n - i); j < jr; ++j) { h.unsafe_get(i + j) += unsafe_get(i) * g.unsafe_get(j); } } return h; } FPSNaive diff() const { if (this->empty()) return {}; FPSNaive g(size() - 1); for (int i = 1; i <= deg(); ++i) g.unsafe_get(i - 1) = unsafe_get(i) * i; return g; } FPSNaive intg() const { const int n = size(); FPSNaive g(n + 1); for (int i = 0; i < n; ++i) g.unsafe_get(i + 1) = unsafe_get(i) * invs[i + 1]; if (g.deg() > MAX_SIZE) g.cut_inplace(MAX_SIZE); return g; } FPSNaive inv(int n = -1) const { if (n < 0) n = size(); FPSNaive g(n); const value_type inv_f0 = ::inv(unsafe_get(0)); g.unsafe_get(0) = inv_f0; for (int i = 1; i < n; ++i) { for (int j = 1; j <= i; ++j) g.unsafe_get(i) -= g.unsafe_get(i - j) * (*this)[j]; g.unsafe_get(i) *= inv_f0; } return g; } FPSNaive exp(int n = -1) const { if (n < 0) n = size(); assert(unsafe_get(0) == value_type{ 0 }); FPSNaive g(n); g.unsafe_get(0) = value_type{ 1 }; for (int i = 1; i < n; ++i) { for (int j = 1; j <= i; ++j) g.unsafe_get(i) += j * g.unsafe_get(i - j) * (*this)[j]; g.unsafe_get(i) *= invs[i]; } return g; } FPSNaive log(int n = -1) const { if (n < 0) n = size(); assert(unsafe_get(0) == value_type{ 1 }); FPSNaive g(n); g.unsafe_get(0) = value_type{ 0 }; for (int i = 1; i < n; ++i) { g.unsafe_get(i) = i * (*this)[i]; for (int j = 1; j < i; ++j) g.unsafe_get(i) -= (i - j) * g.unsafe_get(i - j) * (*this)[j]; g.unsafe_get(i) *= invs[i]; } return g; } FPSNaive pow(const long long k, int n = -1) const { if (n < 0) n = size(); if (k == 0) { FPSNaive res(n); res[0] = 1; return res; } int z = 0; while (z < size() and unsafe_get(z) == value_type{ 0 }) ++z; if (z == size() or z > (n - 1) / k) return FPSNaive(n, 0); const int m = n - z * k; FPSNaive g(m); const value_type inv_f0 = ::inv(unsafe_get(z)); g.unsafe_get(0) = unsafe_get(z).pow(k); for (int i = 1; i < m; ++i) { for (int j = 1; j <= i; ++j) g.unsafe_get(i) += (element_type{ k } *j - (i - j)) * g.unsafe_get(i - j) * (*this)[z + j]; g.unsafe_get(i) *= inv_f0 * invs[i]; } g <<= z * k; return g; } std::optional<FPSNaive> safe_sqrt(int n = -1) const { if (n < 0) n = size(); int dl = 0; while (dl < size() and unsafe_get(dl) == value_type{ 0 }) ++dl; if (dl == size()) return FPSNaive(n, 0); if (dl & 1) return std::nullopt; const int m = n - dl / 2; FPSNaive g(m); auto opt_g0 = ::safe_sqrt((*this)[dl]); if (not opt_g0.has_value()) return std::nullopt; g.unsafe_get(0) = *opt_g0; value_type inv_2g0 = ::inv(2 * g.unsafe_get(0)); for (int i = 1; i < m; ++i) { g.unsafe_get(i) = (*this)[dl + i]; for (int j = 1; j < i; ++j) g.unsafe_get(i) -= g.unsafe_get(j) * g.unsafe_get(i - j); g.unsafe_get(i) *= inv_2g0; } g <<= dl / 2; return g; } FPSNaive sqrt(int n = -1) const { if (n < 0) n = size(); return *safe_sqrt(n); } value_type eval(value_type x) const { value_type y = 0; for (int i = size() - 1; i >= 0; --i) y = y * x + unsafe_get(i); return y; } private: static inline inv_mods<element_type> invs; void ensure_deg(int d) { if (deg() < d) this->resize(d + 1, value_type{ 0 }); } const value_type& unsafe_get(int i) const { return std::vector<value_type>::operator[](i); } value_type& unsafe_get(int i) { return std::vector<value_type>::operator[](i); } }; } // namespace suisen template <typename mint> suisen::FPSNaive<mint> sqrt(suisen::FPSNaive<mint> a) { return a.sqrt(); } template <typename mint> suisen::FPSNaive<mint> log(suisen::FPSNaive<mint> a) { return a.log(); } template <typename mint> suisen::FPSNaive<mint> exp(suisen::FPSNaive<mint> a) { return a.exp(); } template <typename mint, typename T> suisen::FPSNaive<mint> pow(suisen::FPSNaive<mint> a, T b) { return a.pow(b); } template <typename mint> suisen::FPSNaive<mint> inv(suisen::FPSNaive<mint> a) { return a.inv(); } #line 12 "library/polynomial/fps.hpp" namespace suisen { template <typename mint> using convolution_t = std::vector<mint>(*)(const std::vector<mint>&, const std::vector<mint>&); template <typename mint> struct FPS : public std::vector<mint> { using base_type = std::vector<mint>; using value_type = typename base_type::value_type; using base_type::vector; FPS(const std::initializer_list<mint> l) : std::vector<mint>::vector(l) {} FPS(const std::vector<mint>& v) : std::vector<mint>::vector(v) {} FPS(std::vector<mint>&& v) : std::vector<mint>::vector(std::move(v)) {} static void set_multiplication(convolution_t<mint> multiplication) { FPS<mint>::mult = multiplication; } int size() const noexcept { return base_type::size(); } int deg() const noexcept { return size() - 1; } void ensure(int n) { if (size() < n) this->resize(n); } value_type safe_get(int d) const { return d <= deg() ? (*this)[d] : 0; } value_type& safe_get(int d) { ensure(d + 1); return (*this)[d]; } FPS& cut_trailing_zeros() { while (this->size() and this->back() == 0) this->pop_back(); return *this; } FPS& cut(int n) { if (size() > n) this->resize(std::max(0, n)); return *this; } FPS cut_copy(int n) const { FPS res(this->begin(), this->begin() + std::min(size(), n)); res.ensure(n); return res; } FPS cut_copy(int l, int r) const { if (l >= size()) return FPS(r - l, 0); FPS res(this->begin() + l, this->begin() + std::min(size(), r)); res.ensure(r - l); return res; } /* Unary Operations */ FPS operator+() const { return *this; } FPS operator-() const { FPS res = *this; for (auto& e : res) e = -e; return res; } FPS& operator++() { return ++safe_get(0), * this; } FPS& operator--() { return --safe_get(0), * this; } FPS operator++(int) { FPS res = *this; ++(*this); return res; } FPS operator--(int) { FPS res = *this; --(*this); return res; } /* Binary Operations With Constant */ FPS& operator+=(const value_type& x) { return safe_get(0) += x, *this; } FPS& operator-=(const value_type& x) { return safe_get(0) -= x, *this; } FPS& operator*=(const value_type& x) { for (auto& e : *this) e *= x; return *this; } FPS& operator/=(const value_type& x) { return *this *= x.inv(); } friend FPS operator+(FPS f, const value_type& x) { f += x; return f; } friend FPS operator+(const value_type& x, FPS f) { f += x; return f; } friend FPS operator-(FPS f, const value_type& x) { f -= x; return f; } friend FPS operator-(const value_type& x, FPS f) { f -= x; return -f; } friend FPS operator*(FPS f, const value_type& x) { f *= x; return f; } friend FPS operator*(const value_type& x, FPS f) { f *= x; return f; } friend FPS operator/(FPS f, const value_type& x) { f /= x; return f; } /* Binary Operations With Formal Power Series */ FPS& operator+=(const FPS& g) { const int n = g.size(); ensure(n); for (int i = 0; i < n; ++i) (*this)[i] += g[i]; return *this; } FPS& operator-=(const FPS& g) { const int n = g.size(); ensure(n); for (int i = 0; i < n; ++i) (*this)[i] -= g[i]; return *this; } FPS& operator*=(const FPS& g) { return *this = *this * g; } FPS& operator/=(const FPS& g) { return *this = *this / g; } FPS& operator%=(const FPS& g) { return *this = *this % g; } friend FPS operator+(FPS f, const FPS& g) { f += g; return f; } friend FPS operator-(FPS f, const FPS& g) { f -= g; return f; } friend FPS operator*(const FPS& f, const FPS& g) { return mult(f, g); } friend FPS operator/(FPS f, FPS g) { if (f.size() < 60) return FPSNaive<mint>(f).div_mod(g).first; f.cut_trailing_zeros(), g.cut_trailing_zeros(); const int fd = f.deg(), gd = g.deg(); assert(gd >= 0); if (fd < gd) return {}; if (gd == 0) { f /= g[0]; return f; } std::reverse(f.begin(), f.end()), std::reverse(g.begin(), g.end()); const int qd = fd - gd; FPS q = f * g.inv(qd + 1); q.cut(qd + 1); std::reverse(q.begin(), q.end()); return q; } friend FPS operator%(const FPS& f, const FPS& g) { return f.div_mod(g).second; } std::pair<FPS, FPS> div_mod(const FPS& g) const { if (size() < 60) { auto [q, r] = FPSNaive<mint>(*this).div_mod(g); return { q, r }; } FPS q = *this / g, r = *this - g * q; r.cut_trailing_zeros(); return { q, r }; } /* Shift Operations */ FPS& operator<<=(const int shamt) { return this->insert(this->begin(), shamt, 0), * this; } FPS& operator>>=(const int shamt) { return this->erase(this->begin(), this->begin() + std::min(shamt, size())), * this; } friend FPS operator<<(FPS f, const int shamt) { f <<= shamt; return f; } friend FPS operator>>(FPS f, const int shamt) { f >>= shamt; return f; } /* Compare */ friend bool operator==(const FPS& f, const FPS& g) { const int n = f.size(), m = g.size(); if (n < m) return g == f; for (int i = 0; i < m; ++i) if (f[i] != g[i]) return false; for (int i = m; i < n; ++i) if (f[i] != 0) return false; return true; } friend bool operator!=(const FPS& f, const FPS& g) { return not (f == g); } /* Other Operations */ FPS& diff_inplace() { const int n = size(); for (int i = 1; i < n; ++i) (*this)[i - 1] = (*this)[i] * i; return (*this)[n - 1] = 0, *this; } FPS diff() const { FPS res = *this; res.diff_inplace(); return res; } FPS& intg_inplace() { const int n = size(); inv_mods<value_type> invs(n); this->resize(n + 1); for (int i = n; i > 0; --i) (*this)[i] = (*this)[i - 1] * invs[i]; return (*this)[0] = 0, *this; } FPS intg() const { FPS res = *this; res.intg_inplace(); return res; } FPS& inv_inplace(const int n = -1) { return *this = inv(n); } FPS inv(int n = -1) const { if (n < 0) n = size(); if (n < 60) return FPSNaive<mint>(*this).inv(n); if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return inv_sparse(std::move(*sp_f), n); FPS g{ (*this)[0].inv() }; for (int k = 1; k < n; k *= 2) { FPS f_lo = cut_copy(k), f_hi = cut_copy(k, 2 * k); FPS h = (f_hi * g).cut(k) + ((f_lo * g) >>= k); FPS g_hi = g * h; g.resize(2 * k); for (int i = 0; i < k; ++i) g[k + i] = -g_hi[i]; } g.resize(n); return g; } FPS& log_inplace(int n = -1) { return *this = log(n); } FPS log(int n = -1) const { assert(safe_get(0) == 1); if (n < 0) n = size(); if (n < 60) return FPSNaive<mint>(cut_copy(n)).log(n); if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return log_sparse(std::move(*sp_f), n); FPS res = inv(n) * diff(); res.resize(n - 1); return res.intg(); } FPS& exp_inplace(int n = -1) { return *this = exp(n); } FPS exp(int n = -1) { assert(safe_get(0) == 0); if (n < 0) n = size(); if (n < 60) return FPSNaive<mint>(cut_copy(n)).exp(n); if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return exp_sparse(std::move(*sp_f), n); FPS res{ 1 }; for (int k = 1; k < n; k *= 2) res *= ++(cut_copy(k * 2) - res.log(k * 2)), res.cut(k * 2); res.resize(n); return res; } FPS& pow_inplace(long long k, int n = -1) { return *this = pow(k, n); } FPS pow(const long long k, int n = -1) const { if (n < 0) n = size(); if (n < 60) return FPSNaive<mint>(cut_copy(n)).pow(k, n); if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return pow_sparse(std::move(*sp_f), k, n); if (k == 0) { FPS f{ 1 }; f.resize(n); return f; } int tlz = 0; while (tlz < size() and (*this)[tlz] == 0) ++tlz; if (tlz == size() or tlz > (n - 1) / k) return FPS(n, 0); const int m = n - tlz * k; FPS f = *this >> tlz; value_type base = f[0]; return ((((f /= base).log(m) *= k).exp(m) *= base.pow(k)) <<= (tlz * k)); } std::optional<FPS> safe_sqrt(int n = -1) const { if (n < 0) n = size(); if (n < 60) return FPSNaive<mint>(cut_copy(n)).safe_sqrt(n); if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return safe_sqrt_sparse(std::move(*sp_f), n); int tlz = 0; while (tlz < size() and (*this)[tlz] == 0) ++tlz; if (tlz == size()) return FPS(n, 0); if (tlz & 1) return std::nullopt; const int m = n - tlz / 2; FPS h(this->begin() + tlz, this->end()); auto q0 = ::safe_sqrt(h[0]); if (not q0.has_value()) return std::nullopt; FPS f{ *q0 }, g{ q0->inv() }; mint inv_2 = mint(2).inv(); for (int k = 1; k < m; k *= 2) { FPS tmp = h.cut_copy(2 * k) * f.inv(2 * k); tmp.cut(2 * k); f += tmp, f *= inv_2; } f.resize(m); f <<= tlz / 2; return f; } FPS& sqrt_inplace(int n = -1) { return *this = sqrt(n); } FPS sqrt(int n = -1) const { return *safe_sqrt(n); } mint eval(mint x) const { mint y = 0; for (int i = size() - 1; i >= 0; --i) y = y * x + (*this)[i]; return y; } static FPS prod(const std::vector<FPS>& fs) { auto comp = [](const FPS& f, const FPS& g) { return f.size() > g.size(); }; std::priority_queue<FPS, std::vector<FPS>, decltype(comp)> pq{ comp }; for (const auto& f : fs) pq.push(f); while (pq.size() > 1) { auto f = pq.top(); pq.pop(); auto g = pq.top(); pq.pop(); pq.push(f * g); } return pq.top(); } std::optional<std::vector<std::pair<int, value_type>>> sparse_fps_format(int max_size) const { std::vector<std::pair<int, value_type>> res; for (int i = 0; i <= deg() and int(res.size()) <= max_size; ++i) if (value_type v = (*this)[i]; v != 0) res.emplace_back(i, v); if (int(res.size()) > max_size) return std::nullopt; return res; } protected: static convolution_t<mint> mult; static FPS div_fps_sparse(const FPS& f, const std::vector<std::pair<int, value_type>>& g, int n) { const int siz = g.size(); assert(siz and g[0].first == 0); const value_type inv_g0 = g[0].second.inv(); FPS h(n); for (int i = 0; i < n; ++i) { value_type v = f.safe_get(i); for (int idx = 1; idx < siz; ++idx) { const auto& [j, gj] = g[idx]; if (j > i) break; v -= gj * h[i - j]; } h[i] = v * inv_g0; } return h; } static FPS inv_sparse(const std::vector<std::pair<int, value_type>>& g, const int n) { return div_fps_sparse(FPS{ 1 }, g, n); } static FPS exp_sparse(const std::vector<std::pair<int, value_type>>& f, const int n) { const int siz = f.size(); assert(not siz or f[0].first != 0); FPS g(n); g[0] = 1; inv_mods<value_type> invs(n); for (int i = 1; i < n; ++i) { value_type v = 0; for (const auto& [j, fj] : f) { if (j > i) break; v += j * fj * g[i - j]; } v *= invs[i]; g[i] = v; } return g; } static FPS log_sparse(const std::vector<std::pair<int, value_type>>& f, const int n) { const int siz = f.size(); assert(siz and f[0].first == 0 and f[0].second == 1); FPS g(n); for (int idx = 1; idx < siz; ++idx) { const auto& [j, fj] = f[idx]; if (j >= n) break; g[j] = j * fj; } inv_mods<value_type> invs(n); for (int i = 1; i < n; ++i) { value_type v = g[i]; for (int idx = 1; idx < siz; ++idx) { const auto& [j, fj] = f[idx]; if (j > i) break; v -= fj * g[i - j] * (i - j); } v *= invs[i]; g[i] = v; } return g; } static FPS pow_sparse(const std::vector<std::pair<int, value_type>>& f, const long long k, const int n) { if (k == 0) { FPS res(n, 0); res[0] = 1; return res; } const int siz = f.size(); if (not siz) return FPS(n, 0); const int p = f[0].first; if (p > (n - 1) / k) return FPS(n, 0); const value_type inv_f0 = f[0].second.inv(); const int lz = p * k; FPS g(n); g[lz] = f[0].second.pow(k); inv_mods<value_type> invs(n); for (int i = 1; lz + i < n; ++i) { value_type v = 0; for (int idx = 1; idx < siz; ++idx) { auto [j, fj] = f[idx]; j -= p; if (j > i) break; v += fj * g[lz + i - j] * (value_type(k) * j - (i - j)); } v *= invs[i] * inv_f0; g[lz + i] = v; } return g; } static std::optional<FPS> safe_sqrt_sparse(const std::vector<std::pair<int, value_type>>& f, const int n) { const int siz = f.size(); if (not siz) return FPS(n, 0); const int p = f[0].first; if (p % 2 == 1) return std::nullopt; if (p / 2 >= n) return FPS(n, 0); const value_type inv_f0 = f[0].second.inv(); const int lz = p / 2; FPS g(n); auto opt_g0 = ::safe_sqrt(f[0].second); if (not opt_g0.has_value()) return std::nullopt; g[lz] = *opt_g0; value_type k = mint(2).inv(); inv_mods<value_type> invs(n); for (int i = 1; lz + i < n; ++i) { value_type v = 0; for (int idx = 1; idx < siz; ++idx) { auto [j, fj] = f[idx]; j -= p; if (j > i) break; v += fj * g[lz + i - j] * (k * j - (i - j)); } v *= invs[i] * inv_f0; g[lz + i] = v; } return g; } static FPS sqrt_sparse(const std::vector<std::pair<int, value_type>>& f, const int n) { return *safe_sqrt(f, n); } }; template <typename mint> convolution_t<mint> FPS<mint>::mult = [](const auto&, const auto&) { std::cerr << "convolution function is not available." << std::endl; assert(false); return std::vector<mint>{}; }; } // namespace suisen template <typename mint> suisen::FPS<mint> sqrt(suisen::FPS<mint> a) { return a.sqrt(); } template <typename mint> suisen::FPS<mint> log(suisen::FPS<mint> a) { return a.log(); } template <typename mint> suisen::FPS<mint> exp(suisen::FPS<mint> a) { return a.exp(); } template <typename mint, typename T> suisen::FPS<mint> pow(suisen::FPS<mint> a, T b) { return a.pow(b); } template <typename mint> suisen::FPS<mint> inv(suisen::FPS<mint> a) { return a.inv(); } #line 1 "library/math/factorial_large.hpp" #include <utility> #line 1 "library/polynomial/shift_of_sampling_points.hpp" #line 5 "library/polynomial/shift_of_sampling_points.hpp" #include <atcoder/convolution> #line 1 "library/math/factorial.hpp" #line 6 "library/math/factorial.hpp" namespace suisen { template <typename T, typename U = T> struct factorial { factorial() = default; factorial(int n) { ensure(n); } static void ensure(const int n) { int sz = _fac.size(); if (n + 1 <= sz) return; int new_size = std::max(n + 1, sz * 2); _fac.resize(new_size), _fac_inv.resize(new_size); for (int i = sz; i < new_size; ++i) _fac[i] = _fac[i - 1] * i; _fac_inv[new_size - 1] = U(1) / _fac[new_size - 1]; for (int i = new_size - 1; i > sz; --i) _fac_inv[i - 1] = _fac_inv[i] * i; } T fac(const int i) { ensure(i); return _fac[i]; } T operator()(int i) { return fac(i); } U fac_inv(const int i) { ensure(i); return _fac_inv[i]; } U binom(const int n, const int r) { if (n < 0 or r < 0 or n < r) return 0; ensure(n); return _fac[n] * _fac_inv[r] * _fac_inv[n - r]; } template <typename ...Ds, std::enable_if_t<std::conjunction_v<std::is_integral<Ds>...>, std::nullptr_t> = nullptr> U polynom(const int n, const Ds& ...ds) { if (n < 0) return 0; ensure(n); int sumd = 0; U res = _fac[n]; for (int d : { ds... }) { if (d < 0 or d > n) return 0; sumd += d; res *= _fac_inv[d]; } if (sumd > n) return 0; res *= _fac_inv[n - sumd]; return res; } U perm(const int n, const int r) { if (n < 0 or r < 0 or n < r) return 0; ensure(n); return _fac[n] * _fac_inv[n - r]; } private: static std::vector<T> _fac; static std::vector<U> _fac_inv; }; template <typename T, typename U> std::vector<T> factorial<T, U>::_fac{ 1 }; template <typename T, typename U> std::vector<U> factorial<T, U>::_fac_inv{ 1 }; } // namespace suisen #line 8 "library/polynomial/shift_of_sampling_points.hpp" namespace suisen { template <typename mint, typename Convolve, std::enable_if_t<std::is_invocable_r_v<std::vector<mint>, Convolve, std::vector<mint>, std::vector<mint>>, std::nullptr_t> = nullptr> std::vector<mint> shift_of_sampling_points(const std::vector<mint>& ys, mint t, int m, const Convolve &convolve) { const int n = ys.size(); factorial<mint> fac(std::max(n, m)); std::vector<mint> b = [&] { std::vector<mint> f(n), g(n); for (int i = 0; i < n; ++i) { f[i] = ys[i] * fac.fac_inv(i); g[i] = (i & 1 ? -1 : 1) * fac.fac_inv(i); } std::vector<mint> b = convolve(f, g); b.resize(n); return b; }(); std::vector<mint> e = [&] { std::vector<mint> c(n); mint prd = 1; std::reverse(b.begin(), b.end()); for (int i = 0; i < n; ++i) { b[i] *= fac.fac(n - i - 1); c[i] = prd * fac.fac_inv(i); prd *= t - i; } std::vector<mint> e = convolve(b, c); e.resize(n); return e; }(); std::reverse(e.begin(), e.end()); for (int i = 0; i < n; ++i) { e[i] *= fac.fac_inv(i); } std::vector<mint> f(m); for (int i = 0; i < m; ++i) f[i] = fac.fac_inv(i); std::vector<mint> res = convolve(e, f); res.resize(m); for (int i = 0; i < m; ++i) res[i] *= fac.fac(i); return res; } template <typename mint> std::vector<mint> shift_of_sampling_points(const std::vector<mint>& ys, mint t, int m) { auto convolve = [&](const std::vector<mint> &f, const std::vector<mint> &g) { return atcoder::convolution(f, g); }; return shift_of_sampling_points(ys, t, m, convolve); } } // namespace suisen #line 1 "library/convolution/arbitrary_mod_convolution.hpp" #line 6 "library/convolution/arbitrary_mod_convolution.hpp" #line 1 "library/convolution/convolution_naive.hpp" #line 5 "library/convolution/convolution_naive.hpp" namespace suisen::internal { template <typename T, typename R = T> std::vector<R> convolution_naive(const std::vector<T>& a, const std::vector<T>& b) { const int n = a.size(), m = b.size(); std::vector<R> c(n + m - 1); if (n < m) { for (int j = 0; j < m; j++) for (int i = 0; i < n; i++) c[i + j] += R(a[i]) * b[j]; } else { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) c[i + j] += R(a[i]) * b[j]; } return c; } } // namespace suisen #line 8 "library/convolution/arbitrary_mod_convolution.hpp" namespace suisen { template <typename mint, atcoder::internal::is_modint_t<mint>* = nullptr> std::vector<mint> arbitrary_mod_convolution(const std::vector<mint>& a, const std::vector<mint>& b) { int n = int(a.size()), m = int(b.size()); if constexpr (atcoder::internal::is_static_modint<mint>::value) { if constexpr (not (mint::mod() & 63)) { int maxz = 1; while (not ((mint::mod() - 1) & maxz)) maxz <<= 1; int z = 1; while (z < n + m - 1) z <<= 1; if (z <= maxz) return atcoder::convolution<mint>(a, b); } } if (n == 0 or m == 0) return {}; if (std::min(n, m) <= 120) return internal::convolution_naive(a, b); static constexpr long long MOD1 = 754974721; // 2^24 static constexpr long long MOD2 = 167772161; // 2^25 static constexpr long long MOD3 = 469762049; // 2^26 static constexpr long long M1M2 = MOD1 * MOD2; static constexpr long long INV_M1_MOD2 = atcoder::internal::inv_gcd(MOD1, MOD2).second; static constexpr long long INV_M1M2_MOD3 = atcoder::internal::inv_gcd(M1M2, MOD3).second; std::vector<int> a2(n), b2(m); for (int i = 0; i < n; ++i) a2[i] = a[i].val(); for (int i = 0; i < m; ++i) b2[i] = b[i].val(); auto c1 = atcoder::convolution<MOD1>(a2, b2); auto c2 = atcoder::convolution<MOD2>(a2, b2); auto c3 = atcoder::convolution<MOD3>(a2, b2); const long long m1m2 = mint(M1M2).val(); std::vector<mint> c(n + m - 1); for (int i = 0; i < n + m - 1; ++i) { // Garner's Algorithm // X = x1 + x2 * m1 + x3 * m1 * m2 // x1 = c1[i], x2 = (c2[i] - x1) / m1 (mod m2), x3 = (c3[i] - x1 - x2 * m1) / m2 (mod m3) long long x1 = c1[i]; long long x2 = (atcoder::static_modint<MOD2>(c2[i] - x1) * INV_M1_MOD2).val(); long long x3 = (atcoder::static_modint<MOD3>(c3[i] - x1 - x2 * MOD1) * INV_M1M2_MOD3).val(); c[i] = x1 + x2 * MOD1 + x3 * m1m2; } return c; } std::vector<__uint128_t> convolution_int(const std::vector<int> &a, const std::vector<int> &b) { int n = int(a.size()), m = int(b.size()); auto check_nonnegative = [](int e) { return e >= 0; }; assert(std::all_of(a.begin(), a.end(), check_nonnegative)); assert(std::all_of(b.begin(), b.end(), check_nonnegative)); if (n == 0 or m == 0) return {}; if (std::min(n, m) <= 120) return internal::convolution_naive<int, __uint128_t>(a, b); static constexpr long long MOD1 = 754974721; // 2^24 static constexpr long long MOD2 = 167772161; // 2^25 static constexpr long long MOD3 = 469762049; // 2^26 static constexpr long long M1M2 = MOD1 * MOD2; static constexpr long long INV_M1_MOD2 = atcoder::internal::inv_gcd(MOD1, MOD2).second; static constexpr long long INV_M1M2_MOD3 = atcoder::internal::inv_gcd(M1M2, MOD3).second; auto c1 = atcoder::convolution<MOD1>(a, b); auto c2 = atcoder::convolution<MOD2>(a, b); auto c3 = atcoder::convolution<MOD3>(a, b); std::vector<__uint128_t> c(n + m - 1); for (int i = 0; i < n + m - 1; ++i) { // Garner's Algorithm // X = x1 + x2 * m1 + x3 * m1 * m2 // x1 = c1[i], x2 = (c2[i] - x1) / m1 (mod m2), x3 = (c3[i] - x1 - x2 * m1) / m2 (mod m3) int x1 = c1[i]; int x2 = (atcoder::static_modint<MOD2>(c2[i] - x1) * INV_M1_MOD2).val(); int x3 = (atcoder::static_modint<MOD3>(c3[i] - x1 - x2 * MOD1) * INV_M1M2_MOD3).val(); c[i] = x1 + x2 * MOD1 + __uint128_t(x3) * M1M2; } return c; } } // namespace suisen #line 9 "library/math/factorial_large.hpp" namespace suisen { // mod must be a prime number template <typename mint, std::enable_if_t<atcoder::internal::is_static_modint<mint>::value, std::nullptr_t> = nullptr> struct factorial_large { using value_type = mint; static constexpr int LOG_BLOCK_SIZE = 9; static constexpr int BLOCK_SIZE = 1 << LOG_BLOCK_SIZE; static constexpr int BLOCK_NUM = value_type::mod() >> LOG_BLOCK_SIZE; static inline int threshold = 2000000; static_assert(atcoder::internal::is_prime_constexpr(mint::mod())); static value_type fac(long long n) { if (n >= mint::mod()) return 0; return n <= threshold ? factorial<mint>{}.fac(n) : _large_fac(n); } static value_type fac_inv(long long n) { assert(n < (long long) mint::mod()); return n <= threshold ? factorial<mint>{}.fac_inv(n) : _large_fac(n).inv(); } static value_type binom(int n, int r) { if (r < 0 or r > n) return 0; return fac(n) * fac_inv(r) * fac_inv(n - r); } template <typename ...Ds, std::enable_if_t<std::conjunction_v<std::is_integral<Ds>...>, std::nullptr_t> = nullptr> static value_type polynom(const int n, const Ds& ...ds) { if (n < 0) return 0; long long sumd = 0; value_type res = fac(n); for (int d : { ds... }) { if (d < 0 or d > n) return 0; sumd += d; res *= fac_inv(d); } if (sumd > n) return 0; res *= fac_inv(n - sumd); return res; } static value_type perm(int n, int r) { if (r < 0 or r > n) return 0; return fac(n) * fac_inv(n - r); } private: static inline std::vector<value_type> _block_fac{}; static void _build() { if (_block_fac.size()) { return; } std::vector<value_type> f{ 1 }; f.reserve(BLOCK_SIZE); for (int i = 0; i < LOG_BLOCK_SIZE; ++i) { std::vector<value_type> g = shift_of_sampling_points<value_type>(f, 1 << i, 3 << i, arbitrary_mod_convolution<value_type>); const auto get = [&](int j) { return j < (1 << i) ? f[j] : g[j - (1 << i)]; }; f.resize(2 << i); for (int j = 0; j < 2 << i; ++j) { f[j] = get(2 * j) * get(2 * j + 1) * ((2 * j + 1) << i); } } // f_B(x) = (x+1) * ... * (x+B-1) if (BLOCK_NUM > BLOCK_SIZE) { std::vector<value_type> g = shift_of_sampling_points<value_type>(f, BLOCK_SIZE, BLOCK_NUM - BLOCK_SIZE, arbitrary_mod_convolution<value_type>); std::move(g.begin(), g.end(), std::back_inserter(f)); } else { f.resize(BLOCK_NUM); } for (int i = 0; i < BLOCK_NUM; ++i) { f[i] *= value_type(i + 1) * BLOCK_SIZE; } // f[i] = (i*B + 1) * ... * (i*B + B) f.insert(f.begin(), 1); for (int i = 1; i <= BLOCK_NUM; ++i) { f[i] *= f[i - 1]; } _block_fac = std::move(f); } static value_type _large_fac(int n) { _build(); value_type res; int q = n / BLOCK_SIZE, r = n % BLOCK_SIZE; if (2 * r <= BLOCK_SIZE) { res = _block_fac[q]; for (int i = 0; i < r; ++i) { res *= value_type::raw(n - i); } } else if (q != factorial_large::BLOCK_NUM) { res = _block_fac[q + 1]; value_type den = 1; for (int i = 1; i <= BLOCK_SIZE - r; ++i) { den *= value_type::raw(n + i); } res /= den; } else { // Wilson's theorem res = value_type::mod() - 1; value_type den = 1; for (int i = value_type::mod() - 1; i > n; --i) { den *= value_type::raw(i); } res /= den; } return res; } }; } // namespace suisen #line 10 "test/src/math/factorial_large/dummy.test.cpp" template <int MOD> void test() { using mint = atcoder::static_modint<MOD>; std::mt19937 rng{}; constexpr int MAX_N = 10000000; std::uniform_int_distribution<int> dist(0, MAX_N); const int q = 10000; std::vector<int> ns(q); for (int& e : ns) e = dist(rng); // power of 2 for (int i = 0; 1 << i < MAX_N; ++i) ns[i] = 1 << i; std::vector<mint> res1, res2; { suisen::factorial_large<mint> fac{}; fac.threshold = -1; res1.resize(q); for (int i = 0; i < q; ++i) res1[i] = fac.fac(ns[i]); } { suisen::factorial<mint> fac{}; res2.resize(q); for (int i = 0; i < q; ++i) res2[i] = fac.fac(ns[i]); } assert(res1 == res2); } int main() { test<998244353>(); test<1000000007>(); test<1000000009>(); std::cout << "Hello World" << std::endl; return 0; }