链接: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. spring boot 报错 Error creating bean with name

    Application 启动类 要和父目录平级

  2. spark相关脚本解析

    spark-shell/spark-submit/pyspark等关系如下: #spark-submit 逻辑: ########################################### ...

  3. 并发HashMap的put操作引起死循环

    今天研读Java并发容器和框架时,看到为什么要使用ConcurrentHashMap时,其中有一个原因是:线程不安全的HashMap, HashMap在并发执行put操作时会引起死循环,是因为多线程会 ...

  4. 如何在etherscan提交代币官方信息

    https://ethlinkersupport.zendesk.com/hc/zh-cn/articles/360001334992-%E5%A6%82%E4%BD%95%E5%9C%A8ether ...

  5. net::ERR_ABORTED ,引入js文件出现报错的解决方法

    在head头里面添加 <mvc:annotation-driven enable-matrix-variables="true"></mvc:annotation ...

  6. 【转】自定义(滑动条)input[type="range"]样式

    1.如何使用滑动条? 用法很简单,如下所示: <input type="range" value="0"> 各浏览器原始样式如下: Chrome:  ...

  7. P4编程环境搭建遇到的问题与解决方法

    在经历了无数的折腾之后,算是折腾,最后采用的是陈翔学长的脚本加上可爱的shell调整装好的. 链接:p4Install 也许是ubuntu18.04的问题,也有可能是我自己把这个系统折腾的有点杂乱的原 ...

  8. 通过设置窗体的AcceptButton属性,可以设置窗体的“接受”按钮,若此设计,则用户每次按下Enter键都相当于单击该按钮

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. Python2爬虫获取的数据存储到MySQL中时报错"Incorrect string value: '\\xE6\\x96\\xB0\\xE9\\x97\\xBB' for column 'new' at row 1"的解决办法

    由于一直使用python3进行编码,在使用Python2时,将爬虫数据连接数据库进行存储时,出现如上的报错,经查资料 是数据库编码问题. 如下转自:http://www.cnblogs.com/liu ...

  10. 内核blackhole

    1) 当arp表项不存在的时候,数据包等待表项存在了再发,还是直接把数据包给丢掉; 2)如果网络目的地址不可达,是在那一层把数据丢弃,再是路由层就判断还是arp层呢?