-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBinomial_coefficient.cpp
59 lines (44 loc) · 1.07 KB
/
Binomial_coefficient.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
const long long maxN=100001;
const long long MOD=1000000007;
long long factorial[maxN+1];
long long power(long long a,long long b){
a%=MOD;
long long res=1;
while(b){
if(b&1){
res=res*a;
res%=MOD;
}
a=a*a;
a%=MOD;
b>>=1;
}
return res;
}
long long binomial_coeffient(long long n,long long b){
long long afact=factorial[n];
long long bfact=factorial[b];
long long a_bfact=factorial[n-b];
long long inverse=power((bfact*a_bfact)%MOD,MOD-2);
long long value=(afact*inverse)%MOD;
return value;
}
int main(){
//pre computing faction of numbers
factorial[0] = 1;
for (long long i = 1; i <= maxN; i++) {
factorial[i] = factorial[i - 1] * i % MOD;
}
long long n=0,r=0; //defautl value of n,r=0;
cin>>n>>r;
if(r>n){
cout<<"ERROR:value r must not be greater than n!\n";
}
else{
//function to find binomial coefficent nCr
cout<<binomial_coeffient(n,r);
}
return 0;
}