-
Notifications
You must be signed in to change notification settings - Fork 522
/
BinomialCoefficients.java
58 lines (50 loc) · 1.81 KB
/
BinomialCoefficients.java
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
package combinatorics;
import java.math.BigInteger;
public class BinomialCoefficients {
public static long[][] binomialTable(int n) {
long[][] c = new long[n + 1][n + 1];
for (int i = 0; i <= n; i++)
for (int j = 0; j <= i; j++) c[i][j] = (j == 0) ? 1 : c[i - 1][j - 1] + c[i - 1][j];
return c;
}
public static long binomial(long n, long m) {
m = Math.min(m, n - m);
long res = 1;
for (long i = 0; i < m; i++) {
res = res * (n - i) / (i + 1);
}
return res;
}
// for (int i = 1; i < f.length; i++) f[i] = f[i - 1] + Math.log(i);
public static double binomial(int n, int m, double[] f) {
if (m < 0 || m > n)
return 0;
return Math.exp(f[n] - f[m] - f[n - m]);
}
// n! % mod
public static int factorial(int n, int mod) {
long res = 1;
for (int i = 2; i <= n; i++) res = res * i % mod;
return (int) (res % mod);
}
// n! mod p, p - prime, O(p*log(n)) complexity
public static int factorial2(int n, int p) {
int res = 1;
while (n > 1) {
res = (res * ((n / p) % 2 == 1 ? p - 1 : 1)) % p;
for (int i = 2; i <= n % p; ++i) res = (res * i) % p;
n /= p;
}
return res % p;
}
// fact[0] = ifact[0] = fact[1] = ifact[1] = inv[1] = 1;
// for (int i = 2; i < fact.length; i++)
// fact[i] = (int)c((long) fact[i - 1] * i % mod);
// inv[i] = (int) ((long) (p - p / i) * inv[p % i] % p);
// ifact[i] = (int)c((long) ifact[i - 1] * inv[i] % mod);
public static int binomial(int n, int m, int[] fact, int[] ifact, int mod) {
return (int) ((long) fact[n] * ifact[m] % mod * ifact[n - m] % mod);
}
// Usage example
public static void main(String[] args) {}
}