【Codeforces】CF 9 D How many trees?(dp)

时间:2022-04-20 04:56:03

题目

传送门:QWQ

分析

用$ dp[i][j] $表示用i个节点,有多少深度小于等于j的二叉树。

答案是$ dp[n][n] - dp[n][h-1] $

转移时枚举左子树的节点数量,就可以知道右子树的节点数量。不断相乘加到$ dp[i][j] $上。

代码

 #include <bits/stdc++.h>
#define debug(a) cerr<<(#a)<<" = "<<a<<endl;
#define debug2(a,b) cerr<<(#a)<<" = "<<a<<" "<<(#b)<<" = "<<b<<" ";
using namespace std;
typedef long long ll;
const int maxn=;
ll dp[maxn][maxn];
int main(){
int n,h;
scanf("%d%d",&n,&h);
for(int i=;i<=n;i++) dp[][i]=;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
ll qa=;
for(int k=;k<i;k++){
qa+=dp[k][j-]*dp[i-k-][j-];
}
dp[i][j]=qa;
}
}
cout<<dp[n][n]-dp[n][h-]<<endl;
return ;
}