-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
42 lines (37 loc) · 724 Bytes
/
main.cpp
File metadata and controls
42 lines (37 loc) · 724 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
using namespace std;
// 卡特兰数
class Solution {
public:
int numTrees(int n) {
vector<int> dp(n+1, 0);
dp[0] = 1, dp[1] = 1;
for(int i = 2; i <= n; ++i)
{
for(int j = 0, k = i - 1; j <= i - 1; ++j, --k)
{
dp[i] += dp[j]*dp[k];
}
}
return dp[n];
}
};
// catanla 数的通项公式计算
class Solution {
public:
int numTrees(int n) {
long long ans = 1;
for(int i = n + 1; i <= 2 * n; ++i)
{
ans = ans * i/(i-n);
}
return ans / (n + 1);
}
};
int main()
{
Solution s;
cout << s.numTrees(4) << endl;
return 0;
}