hdu 5573 Binary Tree

版权声明:本文为博主原创文章,转载请标明原博客。 https://blog.csdn.net/sdau_fangshifeng/article/details/86535654

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5573

Binary Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2021    Accepted Submission(s): 1208
Special Judge

 

Problem 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 1. Say froot=1.

And for each node u, labels as fu, the left child is fu×2 and right child is fu×2+1. 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 N years, only if he could collect exactly Nsoul 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 x, the number at the node is fx (remember froot=1), he can choose to increase his number of soul gem by fx, or decrease it by fx.

He will walk from the root, visit exactly K nodes (including the root), and do the increasement or decreasement as told. If at last the number is N, 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 N, K, help the King find a way to collect exactly N soul gems by visiting exactly K nodes.

Input

First line contains an integer T, which indicates the number of test cases.

Every test case contains two integers N and K, which indicates soul gems the frog king want to collect and number of nodes he can visit.

⋅ 1≤T≤100.

⋅ 1≤N≤109.

⋅ N≤2K≤260.

Output

For every test case, you should output "Case #x:" first, where x indicates the case number and counts from 1.

Then K lines follows, each line is formated as 'a b', where a is node label of the node the frog visited, and b is either '+' or '-' which means he increases / decreases his number by a.

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,子节点为父节点的2倍和2倍+1,从根节点开始依次向下走k层,问如何走使得将路径上的数进行加减最终结果得到n。

思路:

满2叉树,最左边的那一列,k行能够通过加减,组成小于2^(k+1)的任何数,这道题目给的是n<=2^k,所以可以使用最左边的那一列组成我们需要的数,如果n是偶数,只需要最后一列加一就可以。

然后通过手动模拟会发现最后一位一定是+,

int tem=(1<<k)-n;

对于k=4的时候

1表示减,0表示加

n··

能将n表达的二进制

tem

15

0000

0001

13

1000

0011

11

0100

0101

9

1100

0111

7

0010

1001

5

1010

1011

3

0110

1101

1

1110

1111

对于能n表打出来的式子会发现与后面那个式子又共同点,,,,同时不考虑最后一个数,tem从右向左正好是左边的从做到右,

最后一行一定是加,所以如果n是整的,最后一行加1输出就可以(相当于右子树)

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
int main()
{
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;++t)
    {
        printf("Case #%d:\n",t);
        LL n,k;
        scanf("%lld%lld",&n,&k);
        LL tem=n;
        tem=(1ll<<k)-tem;
        LL cnt=1;
        for(LL i=1;i<k;++i)
        {
            if(tem&(1ll<<i))
                printf("%lld -\n",cnt);
            else
                printf("%lld +\n",cnt);
            cnt<<=1;
        }
        if(n&1)
            printf("%lld +\n",cnt);
        else
            printf("%lld +\n",cnt+1ll);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdau_fangshifeng/article/details/86535654
今日推荐