-
Notifications
You must be signed in to change notification settings - Fork 14
/
binomial_coeffs.cpp
69 lines (58 loc) · 1.34 KB
/
binomial_coeffs.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <cstring>
using namespace std;
#define V 8
int memo[V][V]; //DP table
int min(int a, int b) {return (a < b) ? a : b;}
void print_table(int memo[V][V])
{
for (int i = 0; i < V; ++i)
{
for (int j = 0; j < V; ++j)
{
printf(" %2d", memo[i][j]);
}
printf("\n");
}
}
int binomial_coeffs1(int n, int k)
{
// top-down DP
if (k == 0 || k == n) return 1;
if (memo[n][k] != -1) return memo[n][k];
return memo[n][k] = binomial_coeffs1(n-1, k-1) + binomial_coeffs1(n-1, k);
}
int binomial_coeffs2(int n, int k)
{
// bottom-up DP
for (int i = 0; i <= n; ++i)
{
for (int j = 0; j <= min(i, k); ++j)
{
if (j == 0 || j == i)
{
memo[i][j] = 1;
}
else
{
memo[i][j] = memo[i-1][j-1] + memo[i-1][j];
}
}
}
return memo[n][k];
}
int main()
{
int n = 5, k = 2;
printf("Top-down DP:\n");
memset(memo, -1, sizeof(memo));
int nCk1 = binomial_coeffs1(n, k);
print_table(memo);
printf("C(n=%d, k=%d): %d\n", n, k, nCk1);
printf("Bottom-up DP:\n");
memset(memo, -1, sizeof(memo));
int nCk2 = binomial_coeffs2(n, k);
print_table(memo);
printf("C(n=%d, k=%d): %d\n", n, k, nCk2);
return 0;
}