Skip to content

Commit 4bf5876

Browse files
committed
NcR added
1 parent fe52796 commit 4bf5876

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Library/Math/NcR.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
const int mod = 1'000'000'007;
4+
5+
int mpow(int base, int exp) {
6+
int result = 1;
7+
while (exp > 0) {
8+
if (exp & 1) result = ((long long)result * base) % mod;
9+
base = ((long long)base * base) % mod;
10+
exp >>= 1;
11+
}
12+
return result;
13+
}
14+
15+
namespace NcR {
16+
int fact[200005],ifact[200005];
17+
int get(int n, int r) {
18+
if(n<r||r<0||n<0) return 0;
19+
return (((fact[n]*1LL*ifact[r])%mod)*1LL*ifact[n-r])%mod;
20+
}
21+
22+
void init() {
23+
fact[0]=1;
24+
for(int i = 1; i <= 200000; i++) {
25+
fact[i] = (fact[i-1] * 1LL * i)%mod;
26+
}
27+
28+
ifact[200000] = mpow(fact[200000], mod-2);
29+
for(int i=199999; i>=0; i--) {
30+
ifact[i] = (ifact[i+1] * 1LL * (i+1))%mod;
31+
}
32+
}
33+
};
34+
35+
int main() {
36+
37+
NcR::init();
38+
cout << NcR::get(4, 2) << endl;
39+
return 0;
40+
}

0 commit comments

Comments
 (0)