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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use super::binomial_coefficient::BinomialCoefficient;
use super::permutation::permutation;
use crate::factorial::Factoriable;
use crate::modint::ModInt;
use cargo_snippet::snippet;
#[snippet(name = "combination", include = "binomial_coefficient")]
pub fn combination(n: ModInt, k: usize) -> ModInt {
if k > n.get() as usize {
panic!("n < k, where n in ModInt, k in usize, so cannot calculate n C k")
}
permutation(n, k) / k.factorial()
}
#[test]
fn comb_test() {
use num_integer::binomial;
let n = ModInt::new(4, 1000000007);
let k = 2;
assert_eq!(
combination(n, k).get(),
binomial(n.get() as usize, k) as i64
);
assert_eq!(combination(ModInt::new(10, 1000000007), 3).get(), 120);
}
#[snippet(name = "combination", include = "binomial_coefficient")]
pub fn combination_with_table<T: BinomialCoefficient>(table: &T, n: usize, k: usize) -> ModInt {
table.binomial(n, k)
}
#[test]
fn comb_tbl_test() {
use super::binomial_coefficient::{BCTSmallNK, BCTholdN, BCTDP};
use num_integer::binomial;
let tbl = BCTDP::new(10000, 1000000007);
assert_eq!(combination_with_table(&tbl, 10, 3).get(), binomial(10, 3));
assert_eq!(
combination_with_table(&tbl, 100, 100).get(),
binomial(100, 100)
);
assert_eq!(combination_with_table(&tbl, 2, 0).get(), binomial(2, 0));
let tbl = BCTholdN::new(100, 1000000007);
assert_eq!(combination_with_table(&tbl, 100, 3).get(), binomial(100, 3));
assert_eq!(combination_with_table(&tbl, 100, 2).get(), binomial(100, 2));
assert_eq!(combination_with_table(&tbl, 100, 0).get(), binomial(100, 0));
let tbl = BCTSmallNK::new(1000, 1000000007);
assert_eq!(combination_with_table(&tbl, 10, 3).get(), binomial(10, 3));
assert_eq!(
combination_with_table(&tbl, 100, 100).get(),
binomial(100, 100)
);
assert_eq!(combination_with_table(&tbl, 2, 0).get(), binomial(2, 0));
}