最大不平衡度就是一边为满二叉树,另一边为最小平衡树。如何求最小平衡树的节点数呢?
对于每一个节点,都可以看做一个平衡树的根节点,当左子树为n时,右子树只需要n-1-d就行
了。所以遍历差量d和层数n,递推方程就是a[i][j]=a[i][j-1]+a[i][max(0,j-i-1)]+1。代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100;
ll a[maxn][maxn];
int main()
{
ll n,d;
while(scanf("%lld%lld",&n,&d)!=EOF)
{
if(d==0)
{
printf("0\n");
continue;
}
ll t=n-1;
a[0][1]=1;
for(int i=0;i<=d;i++)//差
{
for(int j=1;j<=n;j++)//层
{
a[i][j]=a[i][j-1]+a[i][max(0,j-1-i)]+1;
}
}
ll ans=pow(2,t);
printf("%lld\n",ans-1-a[d][max(0LL,n-1-d)]);
}
return 0;
}