-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination.rs
125 lines (110 loc) · 3.79 KB
/
combination.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use cargo_snippet::snippet;
#[snippet("ModComb")]
pub struct ModComb {
modulo: usize,
fac: Vec<usize>,
finv: Vec<usize>,
}
#[snippet("ModComb")]
impl ModComb {
// テーブルを作る前処理
// a^(-1) ≡ -(p%a)^(-1) * (p/a) (mod.p)
pub fn new(cap: usize, modulo: usize) -> Self {
let mut fac = vec![0; cap];
let mut finv = vec![0; cap];
let mut inv = vec![0; cap];
fac[0] = 1;
fac[1] = 1;
finv[0] = 1;
finv[1] = 1;
inv[1] = 1;
for i in 2..cap {
fac[i] = fac[i - 1] * i % modulo;
inv[i] = modulo - inv[modulo % i] * (modulo / i) % modulo; // 負の数の余りが負になるので、割る数を足して正にする
finv[i] = finv[i - 1] * inv[i] % modulo;
}
Self { modulo, fac, finv }
}
// 二項係数計算
// nCk = n!/(k!(n-k)!) = (n!) * (k!)^(-1) * ((n-k))!)^(-1)
//
// 参考:https://drken1215.hatenablog.com/entry/2018/06/08/210000
//
// 使用可能場面
// * 1 ≤ k ≤ n ≤ 10^7
// * pは素数 かつ p > n
pub fn combination(&self, n: usize, k: usize) -> usize {
if n < k {
return 0;
}
self.fac[n] * (self.finv[k] * self.finv[n - k] % self.modulo) % self.modulo
}
// 重複組合せ
// n種類のものから重複を許してk個選ぶ場合の数: nHk = (n+k-1)Ck
pub fn homogeneous(&self, n: usize, k: usize) -> usize {
self.combination(n + k - 1, k)
}
// 参考:https://algo-logic.info/combination/
// 計算量:O(k)
//
// 使用可能場面
// * n が巨大; 1 ≤ n ≤ 10^9
// * k がループ可; 1 ≤ k ≤ 10^5
// * pは素数 かつ p > n
pub fn large_n_combination(&self, n: usize, k: usize) -> usize {
if n < k {
return 0;
}
let mut res = 1;
for i in (n - k + 1)..=n {
res = res * i % self.modulo;
}
res * self.finv[k] % self.modulo
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mod_combination() {
let modulo = 1_000_000_007;
let cap = 1001;
let comb = ModComb::new(cap, modulo);
// Test initialization
assert_eq!(comb.modulo, modulo);
assert_eq!(comb.fac[0], 1);
assert_eq!(comb.finv[0], 1);
assert_eq!(comb.fac.len(), cap);
assert_eq!(comb.finv.len(), cap);
// Test combinations
assert_eq!(comb.combination(5, 3), 10);
assert_eq!(comb.combination(10, 7), 120);
assert_eq!(comb.combination(6, 6), 1);
assert_eq!(comb.combination(1000, 500), 159835829);
}
#[test]
fn test_mod_homogeneous() {
let modulo = 1_000_000_007;
let cap = 1001;
let comb = ModComb::new(cap, modulo);
// Test homogeneous combinations
assert_eq!(comb.homogeneous(5, 3), 35); // 5H3 = 7C3 = 35
assert_eq!(comb.homogeneous(10, 7), 11440); // 10H7 = 16C7 = 11440
assert_eq!(comb.homogeneous(6, 0), 1); // 6H0 = 5C0 = 1
assert_eq!(comb.homogeneous(10, 2), 55); // 10H2 = 11C2 = 55
}
#[test]
fn test_large_n_combination() {
let modulo = 1_000_000_007;
let cap = 200_001; // 1 <= k <= 2 * 10^5
let comb = ModComb::new(cap, modulo);
// This is the same as the regular combination test, but using large_n_combination.
assert_eq!(comb.large_n_combination(5, 3), 10);
assert_eq!(comb.large_n_combination(10, 7), 120);
assert_eq!(comb.large_n_combination(6, 6), 1);
// Test for cases where n is very large
let large_n = 1_000_000_000;
assert_eq!(comb.large_n_combination(large_n, 141421), 516595147);
assert_eq!(comb.large_n_combination(large_n, 173205), 589953354);
}
}