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

  1. 2
  2. 5 3
  3. 10 4

Sample Output

  1. Case #1:
  2. 1 +
  3. 3 -
  4. 7 +
  5. Case #2:
  6. 1 +
  7. 3 +
  8. 6 -
  9. 12 +
  1. /*
  2. 想到了贪心没想到二进制优化,贪了几发GG;
  3.  
  4. 完全二叉树左边节点1 2 4 8 ......到k层位置和为2^k-1,但n最大为2^k只需要将叶子向右移一位就可以了
  5.  
  6. 这个不同于二进制构造,减去相当于对总值作用了 2*2^k 所以要减去的和为 d=2^k-1-n,用二进制构造出d然后减去构造d的数,其它数都加上就行了
  7. */
  8.  
  9. #include<bits/stdc++.h>
  10. using namespace std;
  11. int n,k;
  12. int vis[];
  13. int bit[];
  14. int main()
  15. {
  16. //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
  17. int T;
  18. int cas =;
  19. scanf("%d",&T);
  20. while(T--)
  21. {
  22. memset(vis,,sizeof vis);
  23. memset(bit,,sizeof bit);
  24. printf("Case #%d:\n",cas++);
  25. scanf("%d%d",&n,&k);
  26. long long d=(<<k)-;
  27. d-=n;
  28. if(d%)
  29. d++;
  30. d/=;//需要减去的数字
  31. for(int i=;i<;i++)
  32. {
  33. bit[i]=d%;
  34. d/=;
  35. }
  36. long long s=;
  37. int cur=;
  38. for(int i=;i<k;i++)
  39. {
  40. printf("%d ",cur);
  41. if(bit[i])
  42. {
  43. s-=cur;
  44. printf("-\n");
  45. }
  46. else
  47. {
  48. s+=cur;
  49. printf("+\n");
  50. }
  51. cur*=;
  52. }
  53. if(s+cur==n)
  54. printf("%d +\n",cur);
  55. else
  56. printf("%d +\n",cur+);
  57. }
  58. return ;
  59. }

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. Rendering Problems Failed to load platform rendering library 为何打开布局页面时手机预览页面显示不出来?

    看到图片右上角的 android图标没有?把它改为低版本的就可以了,如我的是21就可以了.原因我想是因为sdk版本更新了不兼容导致的吧.

  2. 导航栏 .active激活

    参考 :使用angularjs写一个导航栏控制器 1.点击其中一个li其他的class不影响 2.点击其中一个li其他的class影响 <!DOCTYPE html> <html l ...

  3. wget下载整个网站

    wget下载整个网站wget下载整个网站可以使用下面的命令 wget -r -p -k -np http://hi.baidu.com/phps , -r 表示递归下载,会下载所有的链接,不过要注意的 ...

  4. 浅析Spring MVC工作机制

    1.如何使用Spring MVC? 在web.xml中配置一个DispatcherServlet DispatchServlet初始化的时候会去寻找一个在应用程序的WEB-INF目录下的配置文件,命名 ...

  5. Paint the Grid Reloaded ZOJ - 3781 图论变形

    Paint the Grid Reloaded Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %ll ...

  6. POJ1032 Parliament(数论)

    New convocation of The Fool Land's Parliament consists of N delegates. According to the present regu ...

  7. bzoj3156 防御准备 - 斜率优化

    Input 第一行为一个整数N表示战线的总长度. 第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai. Output 共一个整数,表示最小的战线花费值. Sample Input 102 3 ...

  8. apache一个ip多个端口虚拟主机

    1.打开httpd.conf,查找Listen:80,在下面一行加入Listen:8080:2.查找#Include conf/extra/httpd-vhosts.conf,将此行前面的#去掉:3. ...

  9. ch2-mysql相关

    1 mySql数据库基本 1.1 创建表必须字段 id 1.2 nodeJS数据库连接 根目录下建立 mysql.js 文件代码 var mysql = require('mysql'); var c ...

  10. js 浏览器上调试方法多样

    1.alert(111)       直接打印出 111 2.debugger        写在代码要调试的地方 3.直接在控制台 source 里找到要调试的代码打断点 4.console 常用的 ...