链接:https://www.nowcoder.com/acm/contest/202/F
来源:牛客网

平衡二叉树,顾名思义就是一棵“平衡”的二叉树。在这道题中,“平衡”的定义为,对于树中任意一个节点,都满足左右子树的高度差不超过 d. 空树的高度定义为0,单个节点的高度为1,其他情况下树的高度定义为根节点左右子树高度最大值 + 1. 一棵在高度上平衡的树,节点数可能不平衡,因此再定义一棵树的不平衡度为这棵树中所有节点的左右子树的节点数之差的最大值。
给定平衡的定义参数d, 你需要求出所有高度为 n 的平衡树中不平衡度的最大值。
链接:https://www.nowcoder.com/acm/contest/202/F
来源:牛客网

输入描述:

两个整数,n, d.

输出描述:

一个整数:所有高度为 n 的平衡树中不平衡度的最大值。

输入例子:
4 1
输出例子:
5

-->

示例1

输入

复制

4 1

输出

复制

5

说明

下面这棵树在 d=1 的定义下高度是平衡的,其不平衡度为 5。
 

备注:

0≤ m ≤ n ≤ 60

很容易想到思路从根节点开始一边为满二叉树,另外一边为满足题目平衡条件的树的最少节点数
我和队友手推的这一题结果手算出表了但是规律找不到 难受的一批 经验严重不足 然后仔细看可以慢慢看出规律 ans表示
另外一边为满足题目平衡条件的树的最少节点数
然后仔细观察可以看到这个
ans[i] = ans[i-1] + ans[i - d - 1] + 1
附上打表代码
 int d, sum;
struct node {
int num;
node *lson, *rson;
node () {
num = ;
lson = rson = NULL;
}
};
void del ( node* root ) {
if ( !root ) return ;
del ( root->lson );
del ( root->rson );
delete ( root );
}
int dfs ( int dep, node* root ) {
if ( dep <= ) return ;
root->lson = new node();
root->num += dfs ( dep - , root->lson );
sum++;
if ( root->num - > d ) {
root->rson = new node();
dfs ( root->num - - d, root->rson );
}
return root->num;
}
int main() {
for ( int n = ; n <= ; n++ ) {
d = , sum = ;
node* root = new node();
dfs ( n - - d, root );
printf ( "n=%d sum=%d\n", n, sum );
del ( root );
}
return ;
}

正解代码

 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)+
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("in.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
int n, d;
LL ans[];
LL expmod ( LL a, LL b ) {
LL ret = ;
while ( b ) {
if ( b & ) ret = ret * a;
a = a * a;
b = b >> ;
}
return ret;
}
int main() {
while ( ~sff ( n, d ) ) {
mem ( ans, );
for ( int i = d + ; i <= * d + ; i++ ) ans[i] = i - d - ;
for ( int i = * d + ; i <= n ; i++ ) ans[i] = ans[i-] + ans[i - d - ] + ;
// fuck(ans[n]);
printf ( "%lld\n", expmod ( , n - ) - - ans[n] );
}
return ;
}

平衡二叉树 (牛客国庆day2)解锁二叉树打表姿势&&找规律套路的更多相关文章

  1. 牛客国庆集训派对Day6 A Birthday 费用流

    牛客国庆集训派对Day6 A Birthday:https://www.nowcoder.com/acm/contest/206/A 题意: 恬恬的生日临近了.宇扬给她准备了一个蛋糕. 正如往常一样, ...

  2. 2019牛客国庆集训派对day5

    2019牛客国庆集训派对day5 I.Strange Prime 题意 \(P=1e10+19\),求\(\sum x[i] mod P = 0\)的方案数,其中\(0 \leq x[i] < ...

  3. 牛客国庆集训派对Day2 F、平衡二叉树 【构造+记忆化搜索】

    任意门:https://www.nowcoder.com/acm/contest/202/F 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 1048576K,其他语言2097152K6 ...

  4. 牛客国庆集训派对Day2 Solution

    A    矩阵乘法 思路: 1° 牛客机器太快了,暴力能过. #include <bits/stdc++.h> using namespace std; #define N 5000 in ...

  5. 牛客国庆集训派对Day1 L-New Game!(最短路)

    链接:https://www.nowcoder.com/acm/contest/201/L 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  6. 牛客国庆集训派对Day4 J-寻找复读机

    链接:https://www.nowcoder.com/acm/contest/204/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  7. 牛客国庆集训派对Day4 I-连通块计数(思维,组合数学)

    链接:https://www.nowcoder.com/acm/contest/204/I 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  8. 牛客国庆集训派对Day1-C:Utawarerumono(数学)

    链接:https://www.nowcoder.com/acm/contest/201/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  9. 牛客国庆集训day5 B 电音之王 (大数乘模)

    链接:https://www.nowcoder.com/acm/contest/205/B来源:牛客网 题目描述 终于活成了自己讨厌的样子. 听说多听电音能加快程序运行的速度. 定义一个数列,告诉你a ...

随机推荐

  1. spark提交任务的两种的方法

    在学习Spark过程中,资料中介绍的提交Spark Job的方式主要有两种(我所知道的): 第一种: 通过命令行的方式提交Job,使用spark 自带的spark-submit工具提交,官网和大多数参 ...

  2. mybatis 枚举类型使用

    一.首先定义接口,提供获取数据库存取的值得方法,如下: public interface BaseEnum { int getCode(); } 二.定义mybatis的typeHandler扩展类, ...

  3. css重修之书(一):如何用css制作比1px更细的边框

    如何用css制作比1px更细的边框 在项目的开发过程中,我们常常会使用到border:1px solid xxx,来对元素添加边框: 可是1px的border看起来还是粗了一些粗,不美观,那么有什么方 ...

  4. wwnjld第二轮迭代测试报告

    1.引言 1.1测试报告目的 被测试报告为wwnjld小组我们的时间管理软件的第二轮迭代所写的软件测试报告.在经过本小组大家不懈的努力之下,我们小组第二轮迭代的产品终于新鲜出炉了.这次测试小组的主要成 ...

  5. 第一次课堂作业---circle

    链接:circle

  6. android仿美团客户端购买框悬浮特效

        实现步骤如下: 1,新建一个项目,新建一个MyScrollView继承自ScrollView   public class MyScrollView extends ScrollView { ...

  7. 全排列 next_permutation() 函数的用法

    在头文件<algorithm>里面有如下代码: int a[]; do { } while(next_permutation(a,a+n)); 可产生1~n的全排列有如下代码: #incl ...

  8. phpcms免登录cookies设置方案

    PHPCMS的SESSION时间长一些的解决办法修改两个文件: phpsso_server/caches/configs/system.php里的 'session_ttl' => 999999 ...

  9. C# 知识回顾 - 你真的懂异常(Exception)吗?

    你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...

  10. hibernate映射表

    <?xml version="1.0"?>   <!DOCTYPE hibernate-mapping PUBLIC        "-//Hibern ...