The Old Frog King lives on the root of an infinite tree. According to the law, each node should connect to exactly two nodes on the next level, forming a full binary tree.

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 1. Say froot=1.

And for each node u, labels as fu, the left child is fu×2 and right child is fu×2+1. 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 N years, only if he could collect exactly N 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 x, the number at the node is fx (remember froot=1), he can choose to increase his number of soul gem by fx, or decrease it by fx.

He will walk from the root, visit exactly K nodes (including the root), and do the increasement or decreasement as told. If at last the number is N, 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 N, K, help the King find a way to collect exactly N soul gems by visiting exactly K

nodes.

InputFirst line contains an integer T, which indicates the number of test cases.

Every test case contains two integers N and K, which indicates soul gems the frog king want to collect and number of nodes he can visit.

⋅ 1≤T≤100.

⋅ 1≤N≤109.

⋅ N≤2K≤260.OutputFor every test case, you should output "
Case #x:" first, where x indicates the case number and counts from 1.

Then K lines follows, each line is formated as 'a b', where a is node label of the node the frog visited, and b is either '+' or '-' which means he increases / decreases his number by a.

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 +

OJ-ID:
hdu-5573

author:
Caution_X

date of submission:
20191021

tags:
construction

description modelling:
给定一个完全二叉树,从上向下遍历节点,对于每一个节点可以选择相加或者相减,目标是在第K层达到值为N

major steps to solve it:
(1)根据二进制数的原理,第K层的数可以由1,2,4,8,........,2^(K-1)来表示 (因此本题和二叉树实际上没有什么关系)
(2)我们只要在1,2,4,8,........,2^(K-1),[2^(K)(奇数)或者2^(K)+1(偶数)]中选择数进行相加或者相减即可
(3)首先将所有1,2,3,4,.......,2^K相加得到Sum,之后再选择哪些数应该要相减,对于相减的数应该减去2*(2^i),直到所减数之和为Sum-N,不妨取sum=(Sum-N)/2,判断是否应该相减只要判断该数在sum的二进制位上是否为1,为1则应该减去,否则不应该减去

AC code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
//freopen("input.txt","r",stdin);
ll kase=;
ll T;
scanf("%lld",&T);
while(T--) {
printf("Case #%lld:\n",kase++);
ll N,K;
scanf("%lld%lld",&N,&K);
ll n=<<K;
ll sum=(n-N)/;
ll tmp=;
for(int i=;i<K;i++) {
printf("%lld ",tmp);
if(tmp&sum) printf("-\n");
else printf("+\n");
tmp<<=;
}
if(N&) printf("%lld +\n",tmp);
else printf("%lld +\n",tmp+);
}
return ;
}

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走到第k层,下一层的数是上一层的数*2或者*2+1,可以选择加上或者减去走的数 ...

  3. HDU 5573 Binary Tree(找规律)

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

  4. HDU 5573 Binary Tree(构造题)

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

  5. HDU 5573 Binary Tree【构造】

    几天前模拟区域赛的一道题,今天发现在草稿箱里直接补个博客. 感觉这还是一道很有意思的构造题. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 ...

  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(树的建立,前序中序后序)

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

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

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

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

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

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

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

随机推荐

  1. jQuery 源码分析(十一) 队列模块 Queue详解

    队列是常用的数据结构之一,只允许在表的前端(队头)进行删除操作(出队),在表的后端(队尾)进行插入操作(入队).特点是先进先出,最先插入的元素最先被删除. 在jQuery内部,队列模块为动画模块提供基 ...

  2. 两种查看EFCore生成Sql语句的方法

    一.利用反射生成查询语句 该方法转载自:https://jhrs.com/2019/28488.html (略有修改) using Microsoft.EntityFrameworkCore.Quer ...

  3. SpringBoot捕获AccessDeniedException

    https://www.jianshu.com/p/bb14cca5ab3d 自定义AccessDeniedHandler /** * @Author: jialing xu * @Descripti ...

  4. windows7_删除”右键-新建“菜单中的多余项

    这边文章比较好用:分享下 https://blog.csdn.net/ddgweb/article/details/17993251 在使用windows7的过程中,由于安装了较多的软件,在桌面或者资 ...

  5. 在CAD中进行圆角标注的方法

    在CAD中,大家经常都用听到CAD标注.那其实在CAD中进行标注也是比较常见的工作,CAD标注有文字标注,数值标注等一些标注的方式.下面要来说的就是在CAD中给圆角图形标注的方法,具体操作步骤如下: ...

  6. RV32FDQ/RV64RDQ指令集(1)

    Risc-V架构定义了可选的单精度浮点指令(F扩展指令集)和双精度浮点指令(D扩展指令集),以及四精度浮点指令集(Q扩展指令集).Risc-V架构规定:处理器可以选择只实现F扩展指令子集而不支持D扩展 ...

  7. Word模板注入攻击

    Word模板注入攻击 0x00 工具准备 phishery:https://github.com/ryhanson/phishery/releases office版本:office 2010 0x0 ...

  8. ORA-1562 and ORA-1650 Unable to Extend Rollback Segment (Doc ID 1066542.6)

    ORA-1562 and ORA-1650 Unable to Extend Rollback Segment (Doc ID 1066542.6) APPLIES TO: Oracle Databa ...

  9. 必学PHP类库/常用PHP类库大全,php 类库分类-收集

    依赖管理( Dependency Management ) 用于依赖管理的包和框架 Composer / Packagist - 一个包和依赖管理器. Composer Installers - 一个 ...

  10. bootstrap如何去除自带的样式----导航栏中的菜单实现平滑的过渡到对应的菜单区域-------动态跟换模态框中的内容

    问题1:如何去除bootstap中css中自带的overflow:hidden这个样式 今天遇见在bootstap中轮播图上的  附带图  片不能够显示出来,图片始终有一部分的高度  被隐藏了 后来通 ...