競技プログラミング用 C++ ライブラリ
math/comb.hpp · GitHub で見る
全体サイト(256 KiB向け) · 高速実装
#define COMB_MAX 500000
#include "lib/math/comb.hpp"
int main() {
assert(comb(10, 3).val() == 120);
assert(fact[5].val() == 120);
assert(fact[5] * finv[5] == mint(1));
}
| 操作 | 内容・返り値 | 計算量 |
|---|---|---|
| 自動前計算 | include により、起動時に fact・finv を準備 |
\(O(N)\) |
comb(n,k) |
二項係数。n < 0、k < 0、n < k なら 0 |
\(O(1)\) |
fact[i] / finv[i]
|
階乗/逆階乗。i < 0 なら 0 |
\(O(1)\) |
comb<Mint>(n,k) / Comb<Mint>::comb(n,k)
|
指定した型の二項係数 | 初回前計算後 \(O(1)\) |
Comb<Mint>::fact[i] / Comb<Mint>::finv[i]
|
指定した型の階乗/逆階乗。i < 0 なら 0 |
初回前計算後 \(O(1)\) |
include すると起動時に上限まで自動で前計算します。fact[n] は \(n!\)、finv[n] はその逆数です。mod は 998244353 です。
上限までの comb・fact・finv を何度でも使えます。返り値は mint なので、通常の整数として取り出す場合は comb(n,k).val() とします。fact・finv は operator[] を持つ読み取り用オブジェクトで、値をコピーして返します。負の添字は 0 を返します。C 配列やポインタとしての受け渡し・テーブルへの代入には対応しません。
modint 型を一度指定して comb(n,k)・fact[i]・finv[i] の書き方を使いたい場合は、modint を指定する二項係数 を使ってください。
他の modint は型を指定します。atcoder::modint1000000007・atcoder::modint998244353・atcoder::dynamic_modint などを使えます。ACL はこのヘッダー自体の依存には含まれません。
#define COMB_MAX 500000
#include <atcoder/modint>
#include "lib/math/comb.hpp"
int main() {
using M = atcoder::modint1000000007;
using C = Comb<M>;
assert(comb<M>(10, 3).val() == 120);
assert(C::comb(10, 3).val() == 120);
assert(C::fact[5].val() == 120);
assert(C::fact[5] * C::finv[5] == M(1));
assert(C::fact[-1] == M(0) && C::finv[-1] == M(0));
const auto &fact = C::fact;
const auto &finv = C::finv;
assert(fact[5] * finv[5] == M(1));
}
既定以外の型では、最初の有効な comb・fact・finv の参照時にだけ前計算します。同じ型では 3 つの操作がテーブルを共有し、複数の cpp から使ってもプログラム全体で 1 つです。使わない型や、負の添字・範囲外の k に対して 0 を返すだけの問い合わせでは、テーブルを生成しません。Comb<mint> は起動時に生成する既定のテーブルを再利用します。
modint は素数法で、整数からの構築・乗算・mod()・inv() に対応するものを指定します。別型の前計算上限は min(COMB_INF,Mint::mod()-1) です。有効な問い合わせの非負の添字 i・n はこの上限以下にしてください。合成数法や n >= mod の二項係数には 任意 mod の二項係数 を使ってください。
val() を持つ型では、その値の整数表現を返すものとします。負の代表元にも対応します。val() を持たない型も利用できます。
dynamic_modint のように法を変更できる型では、法が変わった後の最初の有効なアクセスでテーブルを作り直します。異なる型は、同じ法でも別のテーブルを使います。
COMB_MAX は include 前に指定します。省略時は 10000000 です。\(n\) は前計算範囲内とし、COMB_MAX は 0 以上 998244353 未満が必要です。複数の cpp から include する場合は、全て同じ COMB_MAX を指定してください。初回の自動前計算はプログラム全体で 1 回です。既定の階乗・逆階乗の領域は合計 80000008 バイト(約 80 MB)です。
COMB_MAX を \(N\) として、起動時に \(O(N)\) 時間・空間を使います。comb(n,k) と配列の参照は \(O(1)\) です。include した場合は、二項係数を使わなくても起動時の前計算が発生します。
別型の実際の上限を \(L=\min(N,p-1)\)、法を \(p\) とすると、その型の初回前計算は \(O(L+\log p)\) 時間・\(O(L)\) 空間です。乗算を \(O(1)\)、逆元計算を \(O(\log p)\) とします。その後の問い合わせは \(O(1)\) です。法を変更した場合は同じ前計算コストが再びかかります。
#ifndef COMB_HPP
#define COMB_HPP
#include <array>
#include <bit>
#include <cassert>
#include <cstddef>
#include <type_traits>
#include "mint.hpp"
#include "../components/factorial_table.hpp"
#ifndef COMB_MAX
#define COMB_MAX 10000000
#endif
constexpr int COMB_INF = static_cast<int>(COMB_MAX);
static_assert(COMB_INF >= 0 && COMB_INF < mint::mod());
namespace comb_detail {
inline constinit auto storage = std::bit_cast<std::array<mint, std::size_t(COMB_INF + 1) * 2>>(
std::array<unsigned, std::size_t(COMB_INF + 1) * 2>{});
inline constexpr mint *factorial = storage.data(), *inverse_factorial = factorial + COMB_INF + 1;
struct Initializer {
Initializer() {
[[maybe_unused]] static const bool initialized = [] {
mint_detail::factorials(factorial, inverse_factorial, COMB_INF + 1);
return true;
}();
}
};
[[maybe_unused]] static const Initializer initializer;
template<class Mint>
const factorial_detail::Table<Mint> &table() {
static factorial_detail::Table<Mint> value(COMB_INF);
if (value.modulus != Mint::mod()) value = factorial_detail::Table<Mint>(COMB_INF);
return value;
}
template<class Mint, bool Inverse> struct Factorial {
Mint operator[](int i) const {
if (i < 0) return Mint(0);
assert(i <= COMB_INF);
if constexpr (std::is_same_v<Mint, mint>) {
return Inverse ? inverse_factorial[i] : factorial[i];
} else {
const auto &value = table<Mint>();
assert(std::size_t(i) < value.factorial.size());
if constexpr (Inverse) return value.inverse_factorial[i];
else return value.factorial[i];
}
}
};
}
inline mint comb(int n, int k) {
if (n < 0 || k < 0 || k > n) return 0;
assert(n <= COMB_INF);
return comb_detail::factorial[n] * comb_detail::inverse_factorial[k] * comb_detail::inverse_factorial[n - k];
}
template<class Mint>
Mint comb(int n, int k) {
if constexpr (std::is_same_v<Mint, mint>) return comb(n, k);
else {
if (n < 0 || k < 0 || k > n) return Mint(0);
assert(n <= COMB_INF);
const auto &table = comb_detail::table<Mint>();
assert(std::size_t(n) < table.factorial.size());
return table.factorial[n] * table.inverse_factorial[k] * table.inverse_factorial[n - k];
}
}
template<class Mint = mint> struct Comb {
inline static constexpr comb_detail::Factorial<Mint, false> fact{};
inline static constexpr comb_detail::Factorial<Mint, true> finv{};
static Mint comb(int n, int k) { return ::comb<Mint>(n, k); }
};
inline constexpr auto &fact = Comb<>::fact;
inline constexpr auto &finv = Comb<>::finv;
#endif
#line 1 "math/comb.hpp"
#include <array>
#include <bit>
#include <cassert>
#include <cstddef>
#include <type_traits>
#line 1 "math/mint.hpp"
#line 4 "math/mint.hpp"
#include <istream>
#include <ostream>
#line 7 "math/mint.hpp"
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace mint_detail {
template <class I>
constexpr bool mint_integral = std::is_integral_v<I>
#if defined(__SIZEOF_INT128__)
|| std::is_same_v<I, __int128> || std::is_same_v<I, unsigned __int128>
#endif
;
struct [[gnu::warn_unused]] Mint {
using mint = Mint;
unsigned v = 0;
[[nodiscard]] static constexpr unsigned mod() { return 998244353; }
[[nodiscard, gnu::always_inline]] static unsigned reduce(unsigned long long x) {
return (x + (unsigned long long)(unsigned(x) * 998244351u) * mod()) >> 32;
}
[[gnu::always_inline]] Mint() = default;
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
Mint(I x) {
long long r = x % (long long)mod();
if (r < 0) r += mod();
v = reduce((unsigned long long)r * 932051910);
}
[[nodiscard, gnu::always_inline]] static Mint raw(unsigned x) {
Mint a;
a.v = reduce((unsigned long long)x * 932051910);
return a;
}
[[nodiscard, gnu::always_inline]] static Mint word(unsigned x) {
Mint a;
a.v = x;
return a;
}
[[nodiscard, gnu::always_inline]] unsigned val() const {
unsigned r = reduce(v);
return r < mod() ? r : r - mod();
}
friend std::istream &operator>>(std::istream &is, Mint &x) {
long long value;
if (is >> value) x = Mint(value);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Mint &x) {
return os << x.val();
}
[[gnu::always_inline]] Mint &operator+=(Mint b) {
v += b.v;
if (v >= 2 * mod()) v -= 2 * mod();
return *this;
}
[[gnu::always_inline]] Mint &operator-=(Mint b) {
v -= b.v;
if (v >> 31) v += 2 * mod();
return *this;
}
[[gnu::always_inline]] Mint &operator*=(Mint b) {
v = reduce((unsigned long long)v * b.v);
return *this;
}
Mint &operator/=(Mint b) { return *this *= b.inv(); }
Mint &operator++() { return *this += Mint(1); }
Mint &operator--() { return *this -= Mint(1); }
Mint operator++(int) {
Mint a = *this;
++*this;
return a;
}
Mint operator--(int) {
Mint a = *this;
--*this;
return a;
}
[[nodiscard, gnu::always_inline]] Mint operator+() const { return *this; }
[[nodiscard, gnu::always_inline]] Mint operator-() const { return word(v ? 2 * mod() - v : 0); }
[[nodiscard, gnu::always_inline]] Mint operator+(Mint b) const { return Mint(*this) += b; }
[[nodiscard, gnu::always_inline]] Mint operator-(Mint b) const { return Mint(*this) -= b; }
[[nodiscard, gnu::always_inline]] Mint operator*(Mint b) const { return Mint(*this) *= b; }
[[nodiscard]] Mint operator/(Mint b) const { return Mint(*this) /= b; }
[[nodiscard, gnu::always_inline]] bool operator==(Mint b) const { return (v < mod() ? v : v - mod()) == (b.v < mod() ? b.v : b.v - mod()); }
[[nodiscard, gnu::always_inline]] bool operator!=(Mint b) const { return !(*this == b); }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator+(I a, Mint b) { return Mint(a) + b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator-(I a, Mint b) { return Mint(a) - b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator*(I a, Mint b) { return Mint(a) * b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator/(I a, Mint b) { return Mint(a) / b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend bool operator==(I a, Mint b) { return Mint(a) == b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend bool operator!=(I a, Mint b) { return Mint(a) != b; }
[[nodiscard]] Mint pow(long long n) const {
assert(n >= 0);
if (!n) return Mint(1);
Mint r(1), a = *this;
for (; n > 1; n >>= 1, a *= a)
if (n & 1) r *= a;
return r * a;
}
[[nodiscard, gnu::noinline]] Mint inv() const {
assert(*this != Mint(0));
Mint a2 = *this * *this, a3 = a2 * *this;
Mint a7 = a3 * a3 * *this;
Mint a31 = a7;
for (int i = 0; i < 2; ++i) a31 *= a31;
a31 *= a3;
Mint r = a7;
for (int i = 0; i < 3; ++i) r *= r;
r *= a3;
r *= r;
for (int j = 0; j < 4; ++j) {
for (int i = 0; i < 5; ++i) r *= r;
r *= a31;
}
for (int i = 0; i < 3; ++i) r *= r;
return r * a7;
}
};
} // namespace mint_detail
using mint = mint_detail::Mint;
using fps_mint = mint;
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 1 "components/factorial_table.hpp"
#line 1 "components/mint_factorials.hpp"
#line 1 "components/mint_arithmetic.hpp"
#line 5 "components/mint_arithmetic.hpp"
#if defined(__x86_64__) || defined(__i386__)
#include <immintrin.h>
#endif
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace mint_detail {
struct Ops {
using T = mint;
typedef unsigned int U __attribute__((may_alias));
static constexpr U mod = 998244353, twice = 2 * mod, one = 301989884;
static U reduce(unsigned long long x) { return T::reduce(x); }
static U mul(U a, U b) { return reduce((unsigned long long)a * b); }
static U add(U a, U b) {
U c = a + b;
return c < twice ? c : c - twice;
}
static U sub(U a, U b) {
U c = a - b;
return c < twice ? c : c + twice;
}
static U in(U a) { return mul(a, 932051910); }
static U out(U a) {
a = reduce(a);
return a < mod ? a : a - mod;
}
static U quotient(U r) { return ((unsigned long long)r << 32) / mod; }
static U fixed_mul(U a, U r, U q) {
return a * r - U(((unsigned long long)a * q) >> 32) * mod;
}
#if defined(__x86_64__) || defined(__i386__)
static bool vectorized() {
#if defined(__AVX2__)
return true;
#else
static const bool available = __builtin_cpu_supports("avx2");
return available;
#endif
}
using I8 = __m256i;
__attribute__((target("avx2"))) static I8 load8(const U *a) {
return _mm256_loadu_si256(reinterpret_cast<const I8 *>(a));
}
__attribute__((target("avx2"))) static void store8(U *a, I8 x) {
_mm256_storeu_si256(reinterpret_cast<I8 *>(a), x);
}
__attribute__((target("avx2"), always_inline)) static inline I8 reduce8(I8 x, I8 y) {
I8 r = _mm256_set1_epi32(998244351), p = _mm256_set1_epi32(mod);
x = _mm256_add_epi64(x, _mm256_mul_epu32(_mm256_mul_epu32(x, r), p));
y = _mm256_add_epi64(y, _mm256_mul_epu32(_mm256_mul_epu32(y, r), p));
return _mm256_blend_epi32(_mm256_srli_epi64(x, 32), y, 0xaa);
}
__attribute__((target("avx2"))) static I8 mul8(I8 a, I8 b) {
return reduce8(_mm256_mul_epu32(a, b), _mm256_mul_epu32(_mm256_srli_epi64(a, 32), _mm256_srli_epi64(b, 32)));
}
__attribute__((target("avx2"))) static I8 fixed_mul8(I8 a, I8 r, I8 q) {
I8 x = _mm256_srli_epi64(_mm256_mul_epu32(a, q), 32);
I8 y = _mm256_mul_epu32(_mm256_srli_epi64(a, 32), q);
I8 t = _mm256_blend_epi32(x, y, 0xaa);
return _mm256_sub_epi32(_mm256_mullo_epi32(a, r),
_mm256_mullo_epi32(t, _mm256_set1_epi32(mod)));
}
__attribute__((target("avx2"))) static I8 fixed_even8(I8 a, I8 r, I8 q) {
I8 t = _mm256_srli_epi64(_mm256_mul_epu32(a, q), 32);
return _mm256_sub_epi64(_mm256_mul_epu32(a, r),
_mm256_mul_epu32(t, _mm256_set1_epi32(mod)));
}
__attribute__((target("avx2"))) static I8 add8(I8 a, I8 b) {
I8 x = _mm256_add_epi32(a, b);
return _mm256_min_epu32(x, _mm256_sub_epi32(x, _mm256_set1_epi32(twice)));
}
__attribute__((target("avx2"))) static I8 sub8(I8 a, I8 b) {
I8 x = _mm256_sub_epi32(a, b);
return _mm256_min_epu32(x, _mm256_add_epi32(x, _mm256_set1_epi32(twice)));
}
#endif
};
}
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 5 "components/mint_factorials.hpp"
namespace mint_detail {
#if defined(__x86_64__) || defined(__i386__)
__attribute__((target("avx2"))) inline void factorial_transpose(Ops::I8 (&x)[8]) {
Ops::I8 a[8], b[8];
for (int i = 0; i < 8; i += 2) {
a[i] = _mm256_unpacklo_epi32(x[i], x[i + 1]);
a[i + 1] = _mm256_unpackhi_epi32(x[i], x[i + 1]);
}
for (int i = 0; i < 8; i += 4) {
b[i] = _mm256_unpacklo_epi64(a[i], a[i + 2]);
b[i + 1] = _mm256_unpackhi_epi64(a[i], a[i + 2]);
b[i + 2] = _mm256_unpacklo_epi64(a[i + 1], a[i + 3]);
b[i + 3] = _mm256_unpackhi_epi64(a[i + 1], a[i + 3]);
}
for (int i = 0; i < 4; ++i) {
x[i] = _mm256_permute2x128_si256(b[i], b[i + 4], 0x20);
x[i + 4] = _mm256_permute2x128_si256(b[i], b[i + 4], 0x31);
}
}
template<bool reverse>
__attribute__((target("avx2"))) inline void factorial_chain(Ops::U* out, int chunk, Ops::I8 value, Ops::I8 factor) {
using I = Ops::I8;
I step = _mm256_set1_epi32(Ops::one);
I order = _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0);
for (int j = 0; j < chunk; j += 8) {
I block[8];
for (int k = 0; k < 8; ++k) {
block[k] = value;
value = Ops::mul8(value, factor);
if constexpr (reverse) factor = Ops::sub8(factor, step);
else factor = Ops::add8(factor, step);
}
factorial_transpose(block);
for (int k = 0; k < 8; ++k) {
if constexpr (reverse) Ops::store8(out + (k + 1) * chunk - j - 8, _mm256_permutevar8x32_epi32(block[k], order));
else Ops::store8(out + k * chunk + j, block[k]);
}
}
}
__attribute__((target("avx2"))) inline void factorials8(Ops::U* fact, Ops::U* invfact, int n) {
using I = Ops::I8;
using U = Ops::U;
const int chunk = n / 256 * 8, covered = chunk * 32;
alignas(32) U factor[32], products[32], starts[33], inverse[33];
I p[4], x[4], step = _mm256_set1_epi32(Ops::one);
for (int i = 0; i < 32; ++i) factor[i] = Ops::in(i * chunk + 1);
for (int k = 0; k < 4; ++k) p[k] = _mm256_set1_epi32(Ops::one), x[k] = Ops::load8(factor + 8 * k);
for (int i = 0; i < chunk; ++i) {
for (int k = 0; k < 4; ++k) p[k] = Ops::mul8(p[k], x[k]), x[k] = Ops::add8(x[k], step);
}
for (int k = 0; k < 4; ++k) Ops::store8(products + 8 * k, p[k]);
starts[0] = Ops::one;
for (int i = 0; i < 32; ++i) starts[i + 1] = Ops::mul(starts[i], products[i]);
U last = starts[32];
for (int i = covered; i < n; ++i) fact[i] = last, last = Ops::mul(last, Ops::in(i + 1));
last = mint::word(last).inv().v;
for (int i = n - 1; i >= covered; --i) last = Ops::mul(last, Ops::in(i + 1)), invfact[i] = last;
inverse[32] = last;
for (int i = 31; i >= 0; --i) inverse[i] = Ops::mul(inverse[i + 1], products[i]);
for (int k = 0; k < 4; ++k) {
factorial_chain<false>(fact + 8 * k * chunk, chunk, Ops::load8(starts + 8 * k), Ops::load8(factor + 8 * k));
alignas(32) U values[8], factors[8];
for (int j = 0; j < 8; ++j) {
const int end = (8 * k + j + 1) * chunk;
values[j] = Ops::mul(inverse[8 * k + j + 1], Ops::in(end));
factors[j] = Ops::in(end - 1);
}
factorial_chain<true>(invfact + 8 * k * chunk, chunk, Ops::load8(values), Ops::load8(factors));
}
}
#endif
inline void factorials(Ops::U* fact, Ops::U* invfact, int n) {
if (!n) return;
#if defined(__x86_64__) || defined(__i386__)
if (n >= 256 && Ops::vectorized()) {
factorials8(fact, invfact, n);
return;
}
#endif
Ops::U value = Ops::one;
for (int i = 0; i < n; ++i) fact[i] = value, value = Ops::mul(value, Ops::in(i + 1));
value = mint::word(fact[n - 1]).inv().v;
for (int i = n - 1; i >= 0; --i) invfact[i] = value, value = Ops::mul(value, Ops::in(i));
}
template<class T> requires std::is_same_v<T, mint>
inline void factorials(T* fact, T* invfact, int n) {
assert(0 <= n && unsigned(n) <= mint::mod());
static_assert(sizeof(mint) == sizeof(Ops::U) && std::is_trivially_copyable_v<mint>);
if (unsigned(n) == mint::mod()) {
factorials(fact, invfact, n - 1);
fact[n - 1] = -fact[n - 2];
invfact[n - 1] = -invfact[n - 2];
return;
}
factorials(reinterpret_cast<Ops::U*>(fact), reinterpret_cast<Ops::U*>(invfact), n);
}
}
#line 1 "components/factorial_table_generic.hpp"
#line 1 "components/barrett32.hpp"
#line 5 "components/barrett32.hpp"
#include <cstdint>
struct Barrett32 {
std::uint32_t mod;
std::uint64_t reciprocal;
explicit Barrett32(std::uint32_t m) : mod(m), reciprocal(m > 1 ? std::uint64_t((__uint128_t(1) << 64) / m) : 0) {
assert(m > 0);
}
std::uint32_t reduce(std::uint64_t x) const {
if (mod == 1) return 0;
std::uint64_t q = (__uint128_t(x) * reciprocal) >> 64, r = x - q * mod;
return r < mod ? r : r - mod;
}
std::uint32_t mul(std::uint32_t a, std::uint32_t b) const { return reduce(std::uint64_t(a) * b); }
std::uint32_t pow(std::uint32_t a, std::uint64_t e) const {
std::uint32_t r = mod == 1 ? 0 : 1;
for (; e; e >>= 1, a = mul(a, a)) if (e & 1) r = mul(r, a);
return r;
}
};
#line 5 "components/factorial_table_generic.hpp"
#include <concepts>
#line 7 "components/factorial_table_generic.hpp"
#include <limits>
#line 9 "components/factorial_table_generic.hpp"
namespace factorial_table_detail {
template<class Mint, bool Canonical = true> void build(Mint *factorial, Mint *inverse_factorial, int n) {
if constexpr (Canonical && requires(const Mint &x) { { x.val() } -> std::integral; }) {
const auto modulus = Mint::mod();
if (std::uint64_t(modulus) <= std::numeric_limits<std::uint32_t>::max()) {
Barrett32 arithmetic(modulus);
auto make = [](std::uint32_t value) { return Mint(value); };
std::uint32_t value = 1;
factorial[0] = make(value);
for (int i = 1; i <= n; ++i) {
value = arithmetic.mul(value, i);
factorial[i] = make(value);
}
inverse_factorial[n] = factorial[n].inv();
if constexpr (std::is_signed_v<decltype(inverse_factorial[n].val())>) {
std::int64_t residue = inverse_factorial[n].val() % std::int64_t(modulus);
value = residue < 0 ? residue + modulus : residue;
} else value = arithmetic.reduce(inverse_factorial[n].val());
for (int i = n; i > 0; --i) {
value = arithmetic.mul(value, i);
inverse_factorial[i - 1] = make(value);
}
return;
}
}
factorial[0] = Mint(1);
for (int i = 1; i <= n; ++i) factorial[i] = factorial[i - 1] * Mint(i);
inverse_factorial[n] = factorial[n].inv();
for (int i = n; i > 0; --i) inverse_factorial[i - 1] = inverse_factorial[i] * Mint(i);
}
}
#line 6 "components/factorial_table.hpp"
#include <algorithm>
#line 11 "components/factorial_table.hpp"
#include <vector>
namespace factorial_detail {
template<class Mint> struct Table {
decltype(Mint::mod()) modulus;
std::vector<Mint> factorial, inverse_factorial;
explicit Table(int max_n) : modulus(Mint::mod()) {
assert(0 <= max_n && max_n < std::numeric_limits<int>::max() && modulus > 1);
int n = static_cast<int>(std::min(std::uint64_t(max_n), std::uint64_t(modulus) - 1));
factorial.resize(n + 1), inverse_factorial.resize(n + 1);
if constexpr (std::is_same_v<Mint, mint>) {
mint_detail::factorials(factorial.data(), inverse_factorial.data(), n + 1);
} else factorial_table_detail::build(factorial.data(), inverse_factorial.data(), n);
}
};
}
#line 12 "math/comb.hpp"
#ifndef COMB_MAX
#define COMB_MAX 10000000
#endif
constexpr int COMB_INF = static_cast<int>(COMB_MAX);
static_assert(COMB_INF >= 0 && COMB_INF < mint::mod());
namespace comb_detail {
inline constinit auto storage = std::bit_cast<std::array<mint, std::size_t(COMB_INF + 1) * 2>>(
std::array<unsigned, std::size_t(COMB_INF + 1) * 2>{});
inline constexpr mint *factorial = storage.data(), *inverse_factorial = factorial + COMB_INF + 1;
struct Initializer {
Initializer() {
[[maybe_unused]] static const bool initialized = [] {
mint_detail::factorials(factorial, inverse_factorial, COMB_INF + 1);
return true;
}();
}
};
[[maybe_unused]] static const Initializer initializer;
template<class Mint>
const factorial_detail::Table<Mint> &table() {
static factorial_detail::Table<Mint> value(COMB_INF);
if (value.modulus != Mint::mod()) value = factorial_detail::Table<Mint>(COMB_INF);
return value;
}
template<class Mint, bool Inverse> struct Factorial {
Mint operator[](int i) const {
if (i < 0) return Mint(0);
assert(i <= COMB_INF);
if constexpr (std::is_same_v<Mint, mint>) {
return Inverse ? inverse_factorial[i] : factorial[i];
} else {
const auto &value = table<Mint>();
assert(std::size_t(i) < value.factorial.size());
if constexpr (Inverse) return value.inverse_factorial[i];
else return value.factorial[i];
}
}
};
}
inline mint comb(int n, int k) {
if (n < 0 || k < 0 || k > n) return 0;
assert(n <= COMB_INF);
return comb_detail::factorial[n] * comb_detail::inverse_factorial[k] * comb_detail::inverse_factorial[n - k];
}
template<class Mint>
Mint comb(int n, int k) {
if constexpr (std::is_same_v<Mint, mint>) return comb(n, k);
else {
if (n < 0 || k < 0 || k > n) return Mint(0);
assert(n <= COMB_INF);
const auto &table = comb_detail::table<Mint>();
assert(std::size_t(n) < table.factorial.size());
return table.factorial[n] * table.inverse_factorial[k] * table.inverse_factorial[n - k];
}
}
template<class Mint = mint> struct Comb {
inline static constexpr comb_detail::Factorial<Mint, false> fact{};
inline static constexpr comb_detail::Factorial<Mint, true> finv{};
static Mint comb(int n, int k) { return ::comb<Mint>(n, k); }
};
inline constexpr auto &fact = Comb<>::fact;
inline constexpr auto &finv = Comb<>::finv;