HDU 5573 Binary Tree 2015ACM/ICPC亚洲区上海站 (构造)

来源:互联网 发布:青岛灭门案犯落网知乎 编辑:程序博客网 时间:2024/05/17 03:38

Binary Tree

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 998 Accepted Submission(s): 587
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 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.

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 +

Source
2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)

题意是在一棵完全二叉树上,从根节点出发,选择一条路径(每次只能往节点的孩子走),将路径上点的编号作为值或+或-,使的恰好走k步后的结果为n。
其实思路挺玄的。如果我们一直走左孩子我们可以得到最大为2^K-1的值,如果最后一步走右孩子就可以得到2^k。刚好符合给出的数据范围。
既然一直走左孩子(1,2,4,8,16…..)是不是很像二进制。但是二进制对于每一位是取和不取,这里是+或-。所以想到减法,将2^K(或者2^K-1 这取决于数的奇偶性,而奇偶性可以通过最后一步走左孩子或右孩子来调整),我们每在路径上的某一个点填一个-号,假设那个点的标号是y,这样的操作后整个值就少了2y(一加一减显然是这样),这样问题就变成了我们减去某个偶数(经过最后一步调整可以保证)可以得到目标数n,所以将这个差的二进制形式拿出来看看,然后有1的地方是减号,其他都是加号。

#include "iostream"#include "cstring"#include "cstdio"#include "string.h"using namespace std;long long a[65];int main(){    int t;    long long n,k;    int cas=0;    scanf("%d",&t);    a[0]=1;    a[1]=2;    for(int i=2;i<60;i++)    {        a[i]=a[i-1]*2;    }    while(t--)    {        scanf("%lld%lld",&n,&k);        long long flag=0;        if(n%2==0)        {            flag=1;        }        long long temp=a[k]-(long long)1+flag-n;        //cout<<"temp"<<temp<<endl;        temp=temp>>1;        printf("Case #%d:\n",++cas);        for(int i=1;i<k;i++)        {            if(temp&1==1)            {                printf("%lld -\n",a[i-1]);            }            else            {                printf("%lld +\n",a[i-1]);            }            temp=temp>>1;        }        printf("%lld +\n",a[k-1]+flag);    }}
0 0
原创粉丝点击