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. ubuntu下pip的安装和使用

    对于python包的安装而言,需要pip包,对python包资源管理.pip包的安装.对于python2.x和python 3.x 方法不同 : Python 2: sudo dnf upgrade ...

  2. GCD之死锁体会

    1.先看下几句代码 1 2 3 4 5 6 7 dispatch_queue_t serialqueue=dispatch_queue_create("serialqueue", ...

  3. IOS7 点击空白处隐藏键盘的几种方法

    IOS7 点击空白处隐藏键盘的几种方法   iOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我们 ...

  4. C++拷贝构造函数专题

    C++拷贝构造函数 在定义任何C++类时,拷贝空置操作都是必要部分,类需要控制拷贝.赋值和销毁对象时发生的行为.拷贝构造函数的用途:当创建了一个新对象时,可以用另一个对象的数据初始化这个新建的对象. ...

  5. 编写通用shell脚本启动java项目,适用于多数服务,只需修改服务名即可

    文件名:service-user.sh 文件内容: ##shell脚本的头文件必须有#!/bin/sh ##再次配置java环境变量以防报其他错误## java env#jdk安装目录export J ...

  6. php中常用的字符串查找函数strstr()、strpos()实例解释

    string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...

  7. 配置和启动Kubernetes服务

    安装etcd服务 下载安装包 wget https://github.com/coreos/etcd/releases/download/v3.1.3/etcd-v3.1.3-linux-amd64. ...

  8. vue2购物车ch2-(商品列表显示)

    1 index.html <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...

  9. centos 7.1系统更改Mariadb数据存储位置步骤分享

    一.首先确保你要更改Mariadb数据存储的位置的空间够大 现在已将Mariadb存储位置更改到/opt/目录下 1.然后将Mariadb服务stop:systemctl stop mariadb 2 ...

  10. 客户机中PLSQL DEV访问虚拟机中的ORCLE11g,错误百出!

    客户机中PLSQL DEV访问虚拟机中的ORCLE11g,错误百出! 创建时间: 2017/10/14 18:44 作者: CNSIMO 标签: ORACLE 忙了一下午,只有两个字形容:麻烦!   ...