-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatalan number.cpp
More file actions
39 lines (34 loc) · 792 Bytes
/
Catalan number.cpp
File metadata and controls
39 lines (34 loc) · 792 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
31
32
33
34
35
36
37
38
39
// nth catalan number modulo M (prime) in O(n).
// formula : nth catalan num = (2n C n) / (n + 1) = (2n!) / ( (n + 1)! * n! )
int modexp(int base, int exp,int M)
{
int res = 1;
while(exp)
{
if(exp & 1)
res = (1LL * res * base) % M;
base = (1LL * base * base) % M;
exp /= 2;
}
return res % M;
}
int nthCatalan(int n)
{
const int mod = 1e9 + 7;
int x = 1LL, a = 1, b = 1, c = 1;
for(int i = 1; i <= 2 * n; i++)
{
x = (1LL * x * i) % mod;
if(i == 2 * n)
a = x;
if(i == n + 1)
b = x;
if(i == n)
c = x;
}
b = modexp(b, mod - 2, mod);
c = modexp(c, mod - 2, mod);
a = (1LL * a * b) % mod;
a = (1LL * a * c) % mod;
return (int)a;
}