【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;
}


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