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. A glimpse of Support Vector Machine

    支持向量机(support vector machine, 以下简称svm)是机器学习里的重要方法,特别适用于中小型样本.非线性.高维的分类和回归问题.本篇希望在正篇提供一个svm的简明阐述,附录则提 ...

  2. 静态页面如何实现 include 引入公用代码

    一直以来,我司的前端都是用 php 的 include 函数来实现引入 header .footer 这些公用代码的,就像下面这样: <!-- index.php --> <!DOC ...

  3. 翻译连载 |《你不知道的JS》姊妹篇 |《JavaScript 轻量级函数式编程》- 引言&前言

    原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 译者团队(排名不分先后):阿希.blueken.brucec ...

  4. [js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解

    路径在canvas绘图中,经常被用到,是一个非常重要的概念. 比如:我们要在canvas画出3条直线,要求用不同的颜色加以区分. <style> body { background: #0 ...

  5. 外设位宽为8、16、32时,CPU与外设之间地址线的连接方法

    有不少人问到:flash连接CPU时,根据不同的数据宽度,比如16位的NOR FLASH (A0-A19),处理器的地址线要(A1-A20)左移偏1位.为什么要偏1位? (全文有点晦涩,建议收藏本文对 ...

  6. 说下browserslist

    browserslist 是一个开源项目 见到有些package.json里会有如下的配置参数 "browserslist": [ "> 1%", &qu ...

  7. spring事务不会进行回滚的情况

    if(userSave){ try { userDao.save(user); userCapabilityQuotaDao.save(capabilityQuota); } catch (Excep ...

  8. Java的基本程序设计结构【2】

    注释 与大多数程序设计语言一样,Java 中的注释也不会出现在可执行程序中.因此,可以在源程序中根据需要添加任意多的注释,而不必担心可执行代码会膨胀.在Java 中,有三种书写注释的方式. 最常用的方 ...

  9. javascript的数值转换 number()详解

    ---恢复内容开始--- number() parseInt() parseFloat()这三个函都可以把数非数值转换为数值,我们看看他们的区别在哪里 一 Number() 转型函数Number()是 ...

  10. ASP.NET没有魔法——为什么使用ASP.NET

    ASP.NET是什么? ASP.NET是一个使用HTML.CSS.Javascript来构建动态网站或者网站应用程序的Web框架,并且也可以使用它来构建web API和实时通信技术如web soket ...