-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinatorics.rs
44 lines (40 loc) · 1.07 KB
/
combinatorics.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#[derive(Debug, Clone)]
struct Comb {
fact: Vec<Mod<MOD>>,
inv_fact: Vec<Mod<MOD>>
}
#[allow(dead_code)]
impl Comb {
fn new() -> Self {
let mut comb = Self {
fact: vec![Mod::new(0); MAX_LIM as usize],
inv_fact: vec![Mod::new(0); MAX_LIM as usize]
};
Self::init(&mut comb);
comb
}
fn init(comb: &mut Self) {
for i in 2..MAX_LIM as i64 {
comb.fact[i as usize] = comb.fact[i as usize - 1] * Mod::new(i);
comb.inv_fact[i as usize] = Mod::new(1) / comb.fact[i as usize];
}
}
fn c(&self, n: i64, r: i64) -> Mod<MOD> {
if n < 0 || r < 0 || r > n {
return Mod::new(0);
}
self.fact[n as usize] *
self.inv_fact[r as usize] * self.inv_fact[(n - r) as usize]
}
fn bin_exp(mut a: Mod<MOD>, mut b: i64) -> Mod<MOD> {
let mut res: Mod<MOD> = Mod::new(1);
while b > 0 {
if (b & 1) == 1 {
res *= a;
}
a *= a;
b >>= 1;
}
res
}
}