hdu 5573 Binary Tree


B - Binary Tree
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 +
题意:给你一棵二叉树,从顶点1开始往下走,对于每个点你可以选择+或者-去他的权值,问如何向下走k次后能得到n值。

思路:对于这种题一般都是要构造答案的。首先发现一直向左走的话最多能得到2^(k+1)-1,题目给定n<=2^(k),所以剩下就要思考怎么构造出答案,我们可以试想一直向左下走的途中减去其中某些数就能得到答案,那么应该减去哪些数呢,这里我列个式子x+y=z1,x-y=z2,y=(z1-z2)/2,假设z2为N,z1为2^(k+1)-1,那么x为加上某个数就能得到z1减去某个数就能得到z2的状态,也就是向下过程中把权值和为y的数减去就构造出答案了,这里要注意z1-z2可能是奇数,出现这种情况时,我们可以选择第k步走右边,也就是可以得到的权值最多为2^(k+1),这样就变成偶数了,下面给代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<bitset>
using namespace std;
#define maxn 100005
typedef long long LL;
bitset<65>bit;
int main(){
	int t;
	scanf("%d", &t);
	for (int tcase = 1; tcase <= t; tcase++){
		int n, k;
		scanf("%d%d", &n, &k);
		printf("Case #%d:\n", tcase);
		if (n & 1){
			LL div = ((1ll << k) - 1 - n) / 2;
			bit = div;
			for (int i = 0; i<k; i++){
				if (bit[i] & 1){
					printf("%lld -\n", 1ll << i);
				}
				else{
					printf("%lld +\n", 1ll << i);
				}
			}
		}
		else{
			LL div = ((1ll << k) - n) / 2;
			bit = div;
			for (int i = 0; i<k-1; i++){
				if (bit[i] & 1){
					printf("%lld -\n", 1ll << i);
				}
				else{
					printf("%lld +\n", 1ll << i);
				}
			}
			printf("%lld +\n", (1ll << (k - 1)) + 1);
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值