forked from ameen4455/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnCr.cpp
More file actions
27 lines (21 loc) · 675 Bytes
/
nCr.cpp
File metadata and controls
27 lines (21 loc) · 675 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
#include <iostream>
using namespace std;
// Returns n choose r. (i.e. The number of distinct sets of size k chosen from n items). [O(r)]
// Note that C(n, r) = C(n, n - r)
// So call the function with nCr(n, min(r, n-r)) for better performance.
int nCr(int n, int r) {
if (n < r)
return 0;
if (r == 0)
return 1;
return n * nCr(n - 1, r - 1) / r;
}
int main(int argc, char** argv){
if(argc != 3){
cerr<<"Usage: nCr n r\nWhere n is the number of all items and r is the number of chosen sets.";
return 1;
}
int n = atoi(argv[1]), r = atoi(argv[2]);
printf("%d choose %d = %d\n", n, r, nCr(n,r));
return 0;
}