Skip to main content

Combinatorics

Struct Combinatorics 

Source
pub struct Combinatorics<const MOD: i64> { /* private fields */ }
Expand description

階乗と逆階乗を前計算し、順列・組合せを高速に計算します。

法 MOD 上の階乗・順列・組合せを前計算して扱います。

new(max_n) は 0..=max_n の階乗と逆階乗を保存します。以後、引数 n がこの範囲内なら n!、順列 nPr、組合せ nCr を O(1) で求められます。r > n の順列・組合せは 0 です。

MOD は素数であり、max_n < MOD を満たす必要があります。 構築の計算量は O(max_n + log MOD)、使用メモリは O(max_n) です。

§Panics

  • MOD <= 1 の場合
  • max_n >= MOD の場合
  • 前計算範囲を超える n を指定した場合

§Examples

use atcoder::math::combinatorics::Combinatorics;

let comb = Combinatorics::<998_244_353>::new(10);

assert_eq!(comb.factorial(5).value(), 120);
assert_eq!(comb.permutation(5, 2).value(), 20);
assert_eq!(comb.combination(5, 2).value(), 10);

Implementations§

Source§

impl<const MOD: i64> Combinatorics<MOD>

Source

pub fn new(max_n: usize) -> Self

0..=max_n の階乗と逆階乗を法 MOD で前計算します。

MOD は 1 より大きい素数、かつ max_n < MOD でなければなりません。

計算量は O(max_n + log MOD)、使用メモリは O(max_n) です。

§Panics

MOD <= 1、max_n >= MOD、または max_n が i64 に収まらない場合に panic します。

Source

pub fn max_n(&self) -> usize

前計算した最大の引数 n を返します。利用可能な範囲は 0..=max_n() です。

Source

pub fn factorial(&self, n: usize) -> ModInt<MOD>

n! を法 MOD で返します。

計算量は O(1) です。

§Panics

n > max_n() の場合に panic します。

Source

pub fn permutation(&self, n: usize, r: usize) -> ModInt<MOD>

順列 nPr = n! / (n-r)! を法 MOD で返します。

r > n の場合は 0 を返します。n は前計算範囲内である必要があります。 計算量は O(1) です。

Source

pub fn combination(&self, n: usize, r: usize) -> ModInt<MOD>

組合せ nCr を法 MOD で返します。

r > n の場合は 0 を返します。n は前計算範囲内である必要があります。 計算量は O(1) です。

Trait Implementations§

Source§

impl<const MOD: i64> Clone for Combinatorics<MOD>

Source§

fn clone(&self) -> Combinatorics<MOD>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const MOD: i64> Debug for Combinatorics<MOD>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const MOD: i64> Freeze for Combinatorics<MOD>

§

impl<const MOD: i64> RefUnwindSafe for Combinatorics<MOD>

§

impl<const MOD: i64> Send for Combinatorics<MOD>

§

impl<const MOD: i64> Sync for Combinatorics<MOD>

§

impl<const MOD: i64> Unpin for Combinatorics<MOD>

§

impl<const MOD: i64> UnsafeUnpin for Combinatorics<MOD>

§

impl<const MOD: i64> UnwindSafe for Combinatorics<MOD>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.