[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$的两棵树即可

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 const int M=605;
 4 const int mod=1000000007;
 5 int T,f[M][M];
 6 inline int dfs(int x,int y)
 7 {
 8     if(x==0) return 1;
 9     if(y==0) return (x==1);
10     if(~f[x][y]) return f[x][y];
11     long long ans=0;
12     for(int i=1;i<=x;i++) ans=(ans+(long long)dfs(i-1,y-1)*(long long)dfs(x-i,y-1)%mod)%mod;
13     return f[x][y]=ans;
14 }
15 int main()
16 {
17     scanf("%d",&T);
18     memset(f,-1,sizeof(f));
19     while(T--)
20     {
21         int x,y; scanf("%d%d",&x,&y);
22         printf("%d\n",(dfs(x,y)-(y?dfs(x,y-1):0)+mod)%mod);
23     }
24     return 0;
25 }

猜你喜欢

转载自www.cnblogs.com/Slrslr/p/9589970.html
BST