Skip to content

Commit 41656ea

Browse files
committed
Add function that computes a nCr table.
1 parent f594f45 commit 41656ea

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Math/Combinations/nCr_table.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
constexpr int MOD = 1000000007;
7+
8+
// compute a table ncr[i][j] = C(i, j) % MOD for i <= n, j <= m
9+
vector<vector<int>> ncr_table(int n, int m) {
10+
vector<vector<int>> ncr(n + 1, vector<int>(m + 1, 0));
11+
for(int i = 0; i <= n; i++) {
12+
ncr[i][0] = 1;
13+
}
14+
for(int i = 1; i <= n; i++) {
15+
for(int j = 1; j <= m && j <= i; j++) {
16+
ncr[i][j] = (ncr[i - 1][j - 1] + ncr[i - 1][j]) % MOD;
17+
}
18+
}
19+
return ncr;
20+
}
21+
22+
int main() {
23+
int n, m;
24+
cin >> n >> m;
25+
vector<vector<int>> ncr(ncr_table(n, m));
26+
for (int i = 1; i <= n; ++i) {
27+
for (int j = 1; j <= m; ++j) {
28+
cout << ncr[i][j] << " ";
29+
}
30+
cout << "\n";
31+
}
32+
return 0;
33+
}

0 commit comments

Comments
 (0)