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 ...
随机推荐
- JS(二)
上周给大家介绍了一下JS基础中一点东西,今天给大家介绍一下JS基础中一个重要部分,循环和函数. 04-JS中的循环结构 一.[循环结构的步骤] 1.首先要先声明循环变量. 2.判断循环条件 3.执行循 ...
- Crayon 线段树或者树状数组
Crayon Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus ...
- 上传文件没有写权限Access to the path is denied
Access to the path is denied. asp.net程序目录放在系统盘,ntfs格式. 程序中对cfg.xml有写入操作. 运行的时候出现了这个问题. 在我自己的机器上没有问题 ...
- 详解m4文件
最近在分析speex代码,发现编译过程中需要的一个speex.m4文件不知道是何方神圣,怀着对未知知识的渴望,跑到 某哥和某基问了一下,算是认识了,为了方便以后经常见面,这里就做个记录吧. M4实际上 ...
- Tarjan LCA
强连通 迷宫城堡 Proving Equivalences Equivalent Sets Summer Holiday Intelligence System The King's Problem ...
- 初学者易上手的SSH-struts2 04值栈与ognl表达式
什么是值栈?struts2里面本身提供的一种存储机制,类似于域对象,值栈,可以存值和取值.,特点:先进后出.如果将它当做一个容器的话,而这个容器有两个元素,那么最上面的元素叫做栈顶元素,也就是所说的压 ...
- java内部类demo
内部类主要有三种:静态内部类,实例内部类,局部变量内部类 1.静态内部类,该类被static修饰,并且是成员变量,它只能访问外部类被static修饰的方法以及字段(这种说法只局限于不再内部类中创建外部 ...
- bzoj 2243 [SDOI2011]染色(树链剖分+线段树合并)
[bzoj2243][SDOI2011]染色 2017年10月20日 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询 ...
- .NET Framework基本概念
http://blog.csdn.net/T573029173/article/details/41730101 .NET是微软的新一代技术平台.对技术人员来说,想真正了解什么是.NET,须先了解.N ...
- Python和SQL 2017的强大功能
Python和SQL Server 2017的强大功能 原文来自:https://www.red-gate.com/simple-talk/sql/sql-development/power-py ...