hdu-5573 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 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 N, K, help the King find a way to collect exactly N soul gems by visiting exactly K

nodes.
InputFirst 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.

1T100.

1N109.

N2K260.OutputFor 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 +

OJ-ID:
hdu-5573

author:
Caution_X

date of submission:
20191021

tags:
construction

description modelling:
给定一个完全二叉树,从上向下遍历节点,对于每一个节点可以选择相加或者相减,目标是在第K层达到值为N

major steps to solve it:
(1)根据二进制数的原理,第K层的数可以由1,2,4,8,........,2^(K-1)来表示 (因此本题和二叉树实际上没有什么关系)
(2)我们只要在1,2,4,8,........,2^(K-1),[2^(K)(奇数)或者2^(K)+1(偶数)]中选择数进行相加或者相减即可
(3)首先将所有1,2,3,4,.......,2^K相加得到Sum,之后再选择哪些数应该要相减,对于相减的数应该减去2*(2^i),直到所减数之和为Sum-N,不妨取sum=(Sum-N)/2,判断是否应该相减只要判断该数在sum的二进制位上是否为1,为1则应该减去,否则不应该减去

AC code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    //freopen("input.txt","r",stdin);
    ll kase=1;
    ll T;
    scanf("%lld",&T);
    while(T--) {
        printf("Case #%lld:\n",kase++);
        ll N,K;
        scanf("%lld%lld",&N,&K);
        ll n=1<<K;
        ll sum=(n-N)/2;
        ll tmp=1;
        for(int i=1;i<K;i++) {
            printf("%lld ",tmp);
            if(tmp&sum) printf("-\n");
            else printf("+\n");
            tmp<<=1;
        }
        if(N&1)    printf("%lld +\n",tmp);
        else printf("%lld +\n",tmp+1);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/cautx/p/11716619.html
今日推荐