-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutationANDcombination.cpp
More file actions
61 lines (49 loc) · 1.1 KB
/
Copy pathpermutationANDcombination.cpp
File metadata and controls
61 lines (49 loc) · 1.1 KB
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
60
61
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
const ll MX=1000005;
ll bigmod(ll b, ll p)
{
ll ans = 1;
for(; p; p>>=1)
{
if(p&1) ans = ans * b % mod;
b = b * b % mod;
}
return ans;
}
ll F[MX], IF[MX];
ll nCr(ll n, ll r)
{
//if(r > n) return 0;
return IF[r] * IF[n-r] % mod * F[n] % mod;
}
void FCT()
{
F[0] = IF[0] = 1;
for(int i = 1; i < MX; i++) F[i] = F[i-1] * i % mod;
for(int i = 1; i < MX; i++) IF[i] = bigmod(F[i], mod-2);
}
int main()
{
FCT();
ll n, r, tc;
cin>>tc;
while(tc--)
{
cin>>n>>r;
cout<<nCr(n,r)<<endl;
}
return 0;
}
----------------------------------------------------------
Binomial Cofficient
----------------------------------------------------------
int NCR(int n, int r)
{
if(r==0||r==n) return nCr[n][r]=1;
if(~nCr[n][r]) return nCr[n][r];
return nCr[n][r]=(NCR(n-1, r-1)+NCR(n-1, r))%mod;
}
----------------------------------------------------------