【HDU - 5573 Binary Tree】 构造

F - Binary Tree


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

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

He will walk from the root, visit exactly KK nodes (including the root), and do the increasement or decreasement as told. If at last the number is NN, 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 NNKK, help the King find a way to collect exactly NN soul gems by visiting exactly KK nodes.
InputFirst line contains an integer TT, which indicates the number of test cases. 

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

 1T1001≤T≤100

 1N1091≤N≤109

 N2K260N≤2K≤260. OutputFor every test case, you should output " Case #x:" first, where xx indicates the case number and counts from 11

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

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,左右儿子分别为1 << x和1 << x + 1(和线段树的节点编号方法相同)。现需要在访问k个节点的条件下,加上或减去节点编号的值,使之结果为n。


分析:从给的数据范围可以看到n <= 2^k,所以我们可以选择树上最左边的那条路走。n为偶数的情况可以先进行n-1的处理,在最后一个节点的时候使之走向右儿子。

因为2 ^ 0 + 2 ^ 1 + … +2 ^ (k-1) + 1 == 2 ^ k,所以我们先求出左边那条路上的节点值之和sum = 2 ^ k - 1。然后x ==(sum-n)/2,这个x就是我们需要在这条路上减去的值(x的计算可以想想,因为不但不加上,还要减去那个值,所以要除以2)。


代码如下:

#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mod 835672545
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
int n, k;
int main(){
    int t, cas = 0;
    scanf("%d", &t);
    while(t--){
        int flag = 0;
        scanf("%d%d", &n, &k);
        if(!(n & 1)){
            n--;
            flag = 1;
        }
        LL sum = ((LL)pow(2, k) - 1 - n) / 2;
        printf("Case #%d:\n", ++cas);
        for(int i = 0; i < k-1; i++){
            if((sum >> i) & 1){
                printf("%I64d -\n", (LL)pow(2, i));
            }
            else    printf("%I64d +\n", (LL)pow(2, i));
        }
        printf("%I64d %c\n", (LL)pow(2, k-1) + (flag ? 1 : 0), (sum>>(k-1)) & 1 ? '-' : '+');
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章