-
Notifications
You must be signed in to change notification settings - Fork 0
/
nCr.cpp
44 lines (38 loc) · 898 Bytes
/
nCr.cpp
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
unsigned long long power(unsigned long long x, int y, int p)
{
unsigned long long res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
unsigned long long modInverse(unsigned long long n, int p)
{
return power(n, p - 2, p);
}
unsigned long long mul(unsigned long long x, unsigned long long y, int p)
{
return x * 1ull * y % p;
}
unsigned long long divide(unsigned long long x, unsigned long long y, int p)
{
return mul(x, modInverse(y, p), p);
}
unsigned long long nCr(unsigned long long n, int r, int p)
{
if (n < r)
return 0;
if (r == 0)
return 1;
if (n - r < r)
return nCr(n, n - r, p);
unsigned long long res = 1;
for (int i = r; i >= 1; i--)
res = divide(mul(res, n - i + 1, p), i, p);
return res;
}