2015上海赛区B Binary Tree
Description
Since the king is professional in math, he sets a number to each node. Specifically, the root of the tree, where the King lives, is 
. Say 






.
And for each node 
, labels as 

, the left child is 



and right child is 





. The king looks at his tree kingdom, and feels satisfied.
Time flies, and the frog king gets sick. According to the old dark magic, there is a way for the king to live for another 
years, only if he could collect exactly 
soul gems.
Initially the king has zero soul gems, and he is now at the root. He will walk down, choosing left or right child to continue. Each time at node 
, the number at the node is 

(remember 






), he can choose to increase his number of soul gem by 

, or decrease it by 

.
He will walk from the root, visit exactly 
nodes (including the root), and do the increasement or decreasement as told. If at last the number is 
, then he will succeed.
Noting as the soul gem is some kind of magic, the number of soul gems the king has could be negative.
Given 
, 
, help the King find a way to collect exactly 
soul gems by visiting exactly 
nodes.
Input
, which indicates the number of test cases.
Every test case contains two integers 
and 
, which indicates soul gems the frog king want to collect and number of nodes he can visit.








.








.









.
Output
indicates the case number and counts from 
.
Then 
lines follows, each line is formated as 'a b', where 
is node label of the node the frog visited, and 
is either '+' or '-' which means he increases / decreases his number by 
.
It's guaranteed that there are at least one solution and if there are more than one solutions, you can output any of them.
Sample Input
2
5 3
10 4
Sample Output
Case #1:
1 +
3 -
7 +
Case #2:
1 +
3 +
6 -
12 +
/*
想到了贪心没想到二进制优化,贪了几发GG; 完全二叉树左边节点1 2 4 8 ......到k层位置和为2^k-1,但n最大为2^k只需要将叶子向右移一位就可以了 这个不同于二进制构造,减去相当于对总值作用了 2*2^k 所以要减去的和为 d=2^k-1-n,用二进制构造出d然后减去构造d的数,其它数都加上就行了
*/ #include<bits/stdc++.h>
using namespace std;
int n,k;
int vis[];
int bit[];
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
int T;
int cas =;
scanf("%d",&T);
while(T--)
{
memset(vis,,sizeof vis);
memset(bit,,sizeof bit);
printf("Case #%d:\n",cas++);
scanf("%d%d",&n,&k);
long long d=(<<k)-;
d-=n;
if(d%)
d++;
d/=;//需要减去的数字
for(int i=;i<;i++)
{
bit[i]=d%;
d/=;
}
long long s=;
int cur=;
for(int i=;i<k;i++)
{
printf("%d ",cur);
if(bit[i])
{
s-=cur;
printf("-\n");
}
else
{
s+=cur;
printf("+\n");
}
cur*=;
}
if(s+cur==n)
printf("%d +\n",cur);
else
printf("%d +\n",cur+);
}
return ;
}
2015上海赛区B Binary Tree的更多相关文章
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- 一道算法题目, 二行代码, Binary Tree
June 8, 2015 我最喜欢的一道算法题目, 二行代码. 编程序需要很强的逻辑思维, 多问几个为什么, 可不可以简化.想一想, 二行代码, 五分钟就可以搞定; 2015年网上大家热议的 Home ...
- 数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree
题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, fin ...
- Minimum Depth of Binary Tree ——LeetCode
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 【Lowest Common Ancestor of a Binary Tree】cpp
题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...
- 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告
Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【LeetCode】102. Binary Tree Level Order Traversal 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...
随机推荐
- JDK8-废弃永久代(PermGen)迎来元空间(Metaspace)
1.背景 2.为什么废弃永久代(PermGen) 3.深入理解元空间(Metaspace) 4.总结 ========正文分割线===== 一.背景 1.1 永久代(PermGen)在哪里? 根据,h ...
- easyui动态生成列
需求:一个id对应多个key value 将id作为标识列 key值作为表头 value作为值显示.数据表可分为两张表 param数据表: 下表一个id对应上表多个key及value 如下图 id_p ...
- 第1回-使用ThinkPHP的3.1.3版本轻松建网站
使用ThinkPHP的3.1.3版本轻松建网站 首先,从ThinkPHP官网下载一个ThinkPHP3.1.3版本框架包. 其次,取出ThinkPHP3.1.3包中的核心部分,部署你的服务器项目下. ...
- AES加解密算法Qt实现
[声明] (1) 本文源码 在一位未署名网友源码基础上,利用Qt编程,实现了AES加解密算法,并添加了文件加解密功能.在此表示感谢!该源码仅供学习交流,请勿用于商业目的. (2) 图片及描述 除图1外 ...
- JSON依赖的选择
json-lib 源码:https://github.com/aalmiray/Json-lib/ 最新版本:2.4 不再更新 <dependency> <groupId>ne ...
- 分享ES6中比较常用又强大的新特性
前言 es6有很多新东西,但是感觉常用的并不是很多,这里学习记录了一些我自己认为非常常用又强大的新特性. scoping 实用的块级作用域,let x = xxx 可以声明一个块级作用域的局部变量,简 ...
- javascript集合的交,并,补,子集,长度,新增,删除,清空等操作
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...
- (@WhiteTaken)设计模式学习——观察者模式
忙里抽闲,继续学习设计模式,作为自己的读书笔记,这次介绍Java下实现的观察者模式. 观察模式需要了解的三个概念: 被观察者:被观察的对象,发生变化会通知观察者集合(存放观察者的容器) 观察者:有up ...
- Java简单工厂模式以及来自lambda的优化
前言 设计模式是软件工程中一些问题的统一解决方案的模型,它的出现是为了解决一些普遍存在的,却不能被语言特性直接解决的问题,随着软件工程的发展,设计模式也会不断的进行更新,本文介绍的是经典设计模式 ...
- 初识SQL Server2017 图数据库(一)
背景: 图数据库对于表现和遍历复杂的实体之间关系是很有效果的.而这些在传统的关系型数据库中尤其是对于报表而言很难实现.如果把传统关系型数据库比做火车的话,那么到现在大数据时代,图数据库可比做高铁.它已 ...