B - Binary Tree
 

Description

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

. 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

First line contains an integer

, 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

For every test case, you should output " Case #x:" first, where

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的更多相关文章

  1. 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 ...

  2. 一道算法题目, 二行代码, Binary Tree

    June 8, 2015 我最喜欢的一道算法题目, 二行代码. 编程序需要很强的逻辑思维, 多问几个为什么, 可不可以简化.想一想, 二行代码, 五分钟就可以搞定; 2015年网上大家热议的 Home ...

  3. 数据结构与算法(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 ...

  4. 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 ...

  5. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  6. 【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 ...

  7. 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告

    Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...

  8. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  9. 【LeetCode】102. Binary Tree Level Order Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...

随机推荐

  1. JavaScript new Boolean(false) 其实是true

    Boolean类型是JavaScript原始数据类型(primitive type)之一:常用来表示 真或假,是或否:这个类型只有两个值:保留字true和false 一般用于控制语句:如下 if(Bo ...

  2. mysql更新某个字符串字段的部分内容

    如果现在需要Mysql更新字段重部分数据,而不是全部数据,应该采用何种方法呢?下面介绍了两种情况下Mysql更新字段中部分数据的方法,供您参考. Mysql更新字段中部分数据第一种情况: update ...

  3. Windows下 如何添加开机启动项

    Windows XP,Windows 7: 开始 ----> 所有程序 ----> 启动, 右键打开"启动"这个文件夹, 把想开机自动启动的软件快捷方式拖进去即可. ( ...

  4. 对Item中定时器的理解

    一.Diamond介绍 Diamond主要提供持久配置的发布和订阅服务,最大特点是结构简单,稳定可靠. 主要的使用场景:TDDL使用Diamond动态切换数据库,动态扩容等:业务使用Diamond推送 ...

  5. 【POJ】 1061 青蛙的约会(扩欧)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 119148   Accepted: 25070 Descript ...

  6. .net窗体程序的基础知识及详细笔记

    第一章:初识Windows程序 1.1:第一个wondows程序 1.1.1:认识windows程序 Form1.cs:窗体文件:程序对窗体编写的代码一般都存放在这个文件(还有拖动控件时的操作和布局, ...

  7. Canal 同步异常分析:Could not find first log file name in binary log index file

    文章首发于[博客园-陈树义],点击跳转到原文Canal同步异常分析:Could not find first log file name in binary log index file. 公司搜索相 ...

  8. 【NOIP】OpenJudge - 15:银行利息

    #include<stdio.h>//银行利息 int main() { float a,b; int i,c,d; scanf("%f%f%d",&a,&am ...

  9. ZOJ2975 伪数组压缩+组合数

    Kinds of Fuwas Time Limit: 2 Seconds      Memory Limit:65536 KB In the year 2008, the 29th Olympic G ...

  10. OpenCV中的绘图函数-OpenCV步步精深

    OpenCV 中的绘图函数 画线 首先要为画的线创造出环境,就要生成一个空的黑底图像 img=np.zeros((512,512,3), np.uint8) 这是黑色的底,我们的画布,我把窗口名叫做i ...