HDU 5573 Binary Tree(递推)

Problem A    Binary Tree


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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 N 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 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 NK, 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 +

 

大致题意:有一棵K层完全二叉树,每次都需要从更开始,到达每个点可加可减。现在给出两个数N和K,N是国王想得到的数,K是要走的步数(也是树的高度),要求输出(任意)一条合适的路径。

题解:

思路:逐层遍历或深搜在实现上都不太可能,所以先找找规律。

问题:总是找父节点的左/右边子节点是否可取:

如果总是找父节点的左边子节点(绿色路径):1、2、4三个数,因为每个数字仅有加和减两个情况,所以可以表示的无重复的数字个数:2^3=8个。由于根节点是1,其他节点都是偶数,所以表示的数据必定是奇数。由此可得:K层可以表示2^K个奇数,由于正负的对称关系,K层可以表示的奇正整数为2^(K-1)个,也就是0-2^(K)范围内的所有奇数都可以表示。由于偶数=奇数+1,而且N一定是正数,所以第K层数字一定为正,所以N为偶数时取右节点即可(图中红色路径)。

步骤:

  1. 判断N是奇数还是偶数,如果是奇数:N = N-2^(K-1),如果是偶数N = N-2^(K-1)-1,处理完后N都为奇数。
  2. 对之后的路径进行规律寻找。寻找规律(以15以内的奇数如何构成为例):

15 = 8 + 4 + 2 + 1;

13 = 8 + 4 + 2 – 1;

11 = 8 + 4 - 2 + 1;

9 = 8 + 4 - 2 - 1;

7 = 8 – 4 + 2 + 1;

5 = 8 – 4 + 2 – 1;

3 = 8 – 4 - 2 + 1;

1 = 8 – 4 – 2 – 1;

-1 = -8+4+2+1;

……

不难看出,每一步数字是加还是减主要取决于N是正数还是负数。

以N=3,K=4为例:3>0,所以8取正,N=N-8;此时N=-5,所以4取负,N=N+4;此时N=-1,所以2取负,N=N+2;此时N = 1,所以1取正,N=N-1=0结束。

程序流程

实现代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll mpow(int x,ll k){
    ll r = 1;
    while(k--){
        r = r*x;
    }
    return r;
}
int main()
{
    int t,i = 1;
    char in = '+';
    char des = '-';
    stack<ll>ds;
    stack<char>os;
    ll n,k;
    scanf("%d",&t);
    while(t--){
        scanf("%I64d %I64d",&n,&k);
        printf("Case #%d:\n",i);
        if(n%2==1){
            ds.push(mpow(2,k-1));
            os.push(in);
            n = n-mpow(2,k-1);
        }
        else if(n%2==0){
            ds.push(mpow(2,k-1)+1);
            os.push(in);
            n = n-mpow(2,k-1)-1;
        }
        k--;
        while(n!=0){
            ds.push(mpow(2,k-1));
            if(n>=0){
                os.push(in);
                n = n-mpow(2,k-1);
            }
            else if(n<0){
                os.push(des);
                n = mpow(2,k-1)+n;
            }
            k--;
        }
        while(!os.empty()){
            printf("%I64d %c\n",ds.top(),os.top());
            ds.pop();
            os.pop();
        }
        i++;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39458250/article/details/89431602
今日推荐