[BZOJ]BST again

时间:2023-03-09 04:53:33
[BZOJ]BST again

Description

求有多少棵大小为n的深度为h的二叉树。(树根深度为0;左右子树有别;答案对1000000007取模)

Input

第一行一个整数T,表示数据组数。
以下T行,每行2个整数n和h。

Output

共T行,每行一个整数表示答案(对1000000007取模)

Sample Input

2
2 1
3 2

Sample Output

2
4

HINT

对于100%的数据,1<=n<=600,0<=h<=600,1<=T<=10

Source

不会做,抄的题解,还被卡常。设$f[i][j]$表示放$i$个节点深度小于等于$j$的个数,然后我们会有这么一个想法:已知两棵树,如何拼成一棵新的树?我们可以加一个顶点,让两棵树分别为左右子树,而深度刚好在原先的基础上加1。所以枚举最大深度小于等于$j-1$的两棵树去更新最大深度小于等于$j$的两棵树即可

代码:

 #include<cstdio>
#include<cstring>
const int M=;
const int mod=;
int T,f[M][M];
inline int dfs(int x,int y)
{
if(x==) return ;
if(y==) return (x==);
if(~f[x][y]) return f[x][y];
long long ans=;
for(int i=;i<=x;i++) ans=(ans+(long long)dfs(i-,y-)*(long long)dfs(x-i,y-)%mod)%mod;
return f[x][y]=ans;
}
int main()
{
scanf("%d",&T);
memset(f,-,sizeof(f));
while(T--)
{
int x,y; scanf("%d%d",&x,&y);
printf("%d\n",(dfs(x,y)-(y?dfs(x,y-):)+mod)%mod);
}
return ;
}