-
Notifications
You must be signed in to change notification settings - Fork 0
/
nCr.cpp
46 lines (39 loc) · 854 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
45
46
#include<bits/stdc++.h>
#define ll unsigned long long
#define MAX 1000000007
using namespace std;
ll nCr(ll, ll);
int _nCr(int, int);
int main(){
ll t, n, r;
cin >> t;
while(t--){
cin >> n >> r;
cout << _nCr(n, r) << endl;
}
return 0;
}
int _nCr(int n, int r){
if(n < r)
return 0;
int c[r + 1];
memset(c, 0, sizeof(c));
c[0] = 1;
for(int i = 1; i <= n; i++)
for(int j = min(i, r); j > 0; j--)
c[j] = c[j - 1] + c[j];
return c[r];
}
ll nCr(ll n, ll r){
ll c[n + 1][r + 1];
if(n < r)
return 0;
for(ll i = 0; i <= n; i++)
for(ll j = 0; j <= min(i, r); j++){
if(i == j || j == 0)
c[i][j] = 1;
else
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MAX;
}
return c[n][r];
}