-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathncr-table.cpp
49 lines (40 loc) · 1.02 KB
/
ncr-table.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
// Mathematics > Combinatorics > nCr table
// Help Jim calculating nCr values
//
// https://www.hackerrank.com/challenges/ncr-table/problem
// challenge id: 1232
//
#include <bits/stdc++.h>
using namespace std;
constexpr unsigned MOD = 1000000000;
constexpr unsigned MAX = 1001;
unsigned C[MAX][MAX];
int main()
{
// Caculate value of Binomial Coefficient in bottom up manner
//https://www.geeksforgeeks.org/dynamic-programming-set-9-binomial-coefficient/
memset(C, 0, sizeof(C));
for (unsigned i = 0; i < MAX; i++)
{
for (unsigned j = 0; j <= i; j++)
{
// Base Cases
if (j == 0 || j == i)
C[i][j] = 1;
// Calculate value using previosly stored values
else
C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD;
}
}
int t;
cin >> t;
while (t--)
{
unsigned n;
cin >> n;
for (unsigned i = 0; i <= n; ++i)
cout << C[n][i] << " ";
cout << endl;
}
return 0;
}