几天前模拟区域赛的一道题,今天发现在草稿箱里直接补个博客。

感觉这还是一道很有意思的构造题。


题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5573

题意:

给你一个二叉树,根节点为1,子节点为父节点的2倍和2倍+1,从根节点开始依次向下走k层,问如何走使得将路径上的数进行加减最终结果得到n。

分析:

首先明确由1.2.4...2k可以构造出所有小于2k+1的数,那么实际上只要走2的幂次即最左边的结点即可。

那么实际上这个过程就类似整数的二进制表示,1表示加,0表示减,如果该位为0,那么意味着在2k+1−1的基础上减去了二倍改为代表的数,所以我们求出n与2k+1−1的差,将差的一半的二进制中为1的位置为0即可~~

注意偶数的时候,差为奇数,那么我们将差多加个一,让最后一步走右边的结点,即加一即可。

代码:

#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 60 +5;
int a[maxn];
int main (void)
{
int t;cin>>t;
int cnt = 0;
while(t--){
int n, k;cin>>n>>k;
cnt++;
cout<<"Case #"<<cnt<<":"<<endl;
ll all = 1<<(k+1) - 1;
bool flg = false;
if(n % 2 == 0){flg = true;n--;}
ll res = (all - n) / 2;
for(int i = 0; i < k - 1; i++){
ll tmp = 1<<i;
if(res&1) cout<<tmp<<" -"<<endl;
else cout<<tmp<<" +"<<endl;
res = res>>1;
}
ll tmp = 1<< (k - 1);
if(flg) cout<<tmp + 1<<" +"<<endl;
else cout<<tmp<<" +"<<endl;
}
return 0;
}

HDU 5573 Binary Tree【构造】的更多相关文章

  1. HDU 5573 Binary Tree 构造

    Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...

  2. HDU 5573 Binary Tree(构造题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5573 题意:给出一个满二叉树,根节点权值为1,左儿子为2*val,右儿子为2*val+1.现在有只青蛙从根节点出 ...

  3. 【规律】【贪心】【数学】HDU 5573 Binary Tree

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 题目大意: 从1走到第k层,下一层的数是上一层的数*2或者*2+1,可以选择加上或者减去走的数 ...

  4. HDU 5573 Binary Tree(找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5573 题意:给你一个完全二叉树,节点为自然数的排列(第一行1,第二行2 3,第三行4 5 6 7... ...

  5. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  7. hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

    题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...

  8. HDU 1710 Binary Tree Traversals(二叉树遍历)

    传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...

  9. hdu 6161--Big binary tree(思维--压缩空间)

    题目链接 Problem Description You are given a complete binary tree with n nodes. The root node is numbere ...

随机推荐

  1. Mybatis和Spring整合&逆向工程

    Mybatis和Spring整合&逆向工程Mybatis和Spring整合mybatis整合Spring的思路目的就是将在SqlMapConfig.xml中的配置移植到Spring的appli ...

  2. hql语法001

    1. import java.util.List; import org.hibernate.*; import org.junit.Test; import cn.jbit.hibernatedem ...

  3. The MySQL server is running with the –secure-file-priv

    show variables like '%secure%'; 将文件导出路径更改为查询到的secure-file-priv路径下 select * from table where column = ...

  4. centos7 rsync+inotify软件实现集群服务的数据备份(二)

    上一篇文章记录了怎么安装rsync以及怎么使用该服务备份数据,但是在集群中需要实时备份客户发过来的相关数据,这样在使用命令或者定时任务的方式执行备份, 就满足不了整个服务的需求了. inotify是一 ...

  5. 360 Atlas中间件安装及使用

    1.下载Atlas wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm ...

  6. cobbler 无人值守-安装

    环境准备 准备两台主机,如centos6和centos7 centos7当作server服务器 关闭selinux 关闭防火墙 安装 cobbler包光盘里是没有的,要配置epel源,这里就说怎么配置 ...

  7. 查看mysql二进制日志报错问题

    在排查网站被黑时想通过Mysql二进制日志找出修改字段时间,但是使用mysqlbinlog报错: [root@zfszsw1 bin]# ./mysqlbinlog /opt/mysql-bin.00 ...

  8. 在不使用ssr的情况下解决Vue单页面SEO问题

    遇到的问题: 近来在写个人博客的时候遇到了大家可能都会遇到的问题 Vue单页面在SEO时显得很无力,尤其是百度不会抓取动态脚本 Vue-Router配合前后端分离无法让meta标签在蜘蛛抓取时动态填充 ...

  9. 条款9:绝不在构造和析构过程中调用virtual函数(Never call virtual functions during construction or destruction)

    NOTE:在构造和析构期间不要调用virtual函数,因为这类调用从不下降至derived class(比起当前执行构造函数和析构函数的那层)

  10. 免费开源《OdooERP系统部署架构指南》试读:第一章 Odoo架构概述

    文/开源智造联合创始人老杨 本文来自<OdooERP系统部署架构指南>的试读章节.书籍尚未出版,请勿转载.欢迎您反馈阅读意见. 从web浏览器到PostgreSQL,多层与其他层交互以处理 ...