-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInverseModulo.java
More file actions
30 lines (26 loc) · 908 Bytes
/
InverseModulo.java
File metadata and controls
30 lines (26 loc) · 908 Bytes
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
/*
* Calculating modulo inverse in O(N)
* http://ideone.com/mlL5F2
* https://www.youtube.com/watch?v=LE9zBhLap-U
*/
public class nCr {
public static int MOD = 1000000007,
MAX = 1000000;
public static long[] inv, fact, invFact;
public static void main(String[] args) {
inv = new long[MAX + 1];
fact = new long[MAX + 1];
invFact = new long[MAX + 1];
inv[1] = 1;
for(int i = 2; i <= MAX; i++)
inv[i] = (MOD -(MOD/i)*inv[MOD%i]%MOD)%MOD; // i^-1 mod p
fact[0] = invFact[0] = 1;
for (int i = 1; i <= MAX; i++) {
fact[i] = (fact[i-1]*i)%MOD; // i! mod p
invFact[i] = (invFact[i-1]*inv[i])%MOD; // (i!)^-1 mod p
}
}
public static long _nCr(int n, int r) {
return (fact[n]*(invFact[r]*invFact[n-r])%MOD)%MOD;
}
}