Factorial

Struct Factorial 

Source
pub struct Factorial { /* private fields */ }
Expand description

階乗とその乗法逆元、そして二項係数を扱います。

Implementations§

Source§

impl Factorial

Source

pub fn new(size: usize, modulo: u64) -> Self

1 以上 size 未満の n について、n の階乗 (mod modulo) と、その乗法逆元を O(size) 時間で計算します。参考

逆元を正しく計算するためには

  • modulo が素数
  • modulo >= size

である必要があります。

§Examples
use factorials::Factorial;

let modulo = 1_000_000_000 + 7;
let f = Factorial::new(100, modulo);
for i in 1..100 {
    assert_eq!(f.factorial(i) * f.inversion(i) % modulo, 1);
}
§Panics

modulosize より小さい場合パニックです。

use factorials::Factorial;

let size = 100;
let modulo = 97;
Factorial::new(size, modulo);
Source

pub fn new_checking_modulo_prime(size: usize, modulo: u64) -> Self

modulo が素数でない場合パニックです。素数判定に O(sqrt(modulo)) 時間かかります。

§Panics
use factorials::Factorial;

let modulo = 42;
Factorial::new_checking_modulo_prime(10, 42);
Source

pub fn factorial(&self, n: usize) -> u64

Source

pub fn inversion(&self, n: usize) -> u64

Source

pub fn binomial(&self, n: usize, k: usize) -> u64

二項係数を返します。

§Examples
use factorials::Factorial;

let f = Factorial::new_checking_modulo_prime(5, 107);
assert_eq!(f.binomial(4, 0), 1);
assert_eq!(f.binomial(4, 1), 4);
assert_eq!(f.binomial(4, 2), 6);
assert_eq!(f.binomial(4, 3), 4);
assert_eq!(f.binomial(4, 4), 1);
§Panics

以下の少なくともひとつが成り立つ場合パニックです。

  • n が構築時の size 以上
  • k が構築時の size 以上
  • nk より小さい
use factorials::Factorial;

let f = Factorial::new_checking_modulo_prime(5, 107);
f.binomial(3, 4); // n < k
Source

pub fn binomial_or_zero(&self, n: usize, k: usize) -> u64

binomial とほとんど同じですが nk より小さいときパニックせずに 0 を返します。

use factorials::Factorial;

let f = Factorial::new_checking_modulo_prime(5, 107);
assert_eq!(f.binomial_or_zero(3, 4), 0);

Auto Trait Implementations§

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> 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, 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.