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); }}
- HDU 5573 Binary Tree 2015ACM/ICPC亚洲区上海站 (构造)
- HDU 5573 Binary Tree ACM/ICPC 2015 Shanghai(构造)
- HDU 5578 Friendship of Frog(暴力)——2015ACM/ICPC亚洲区上海站
- HDU 5584 LCM Walk(逆推)——2015ACM/ICPC亚洲区上海站
- HDU 5584 LCM Walk (2015ACM/ICPC亚洲区上海站)
- 【HDU 5584】 LCM Walk(逆推)——2015ACM/ICPC亚洲区上海站
- hdu 5578 Friendship of Frog 2015ACM/ICPC亚洲区上海站-重现赛
- hdu 5583 Kingdom of Black and White 2015ACM/ICPC亚洲区上海站-重现赛
- hdu 5584 LCM Walk 2015ACM/ICPC亚洲区上海站-重现赛
- 【解题报告】2015ACM/ICPC亚洲区上海站
- 2015ACM/ICPC亚洲区上海站 LCM Walk
- 2015ACM/ICPC亚洲区上海站【solved:4 / 12】
- 【HDU 6228、2017ACM/ICPC亚洲区沈阳站】Tree
- HDU 5573 Binary Tree(2015-ICPC-上海赛区)
- HDU 5583 Kingdom of Black and White(依旧是暴力)——2015ACM/ICPC亚洲区上海站
- HDU 5578.Friendship of Frog【字符串相同字符最近距离】【2015ACM/ICPC亚洲区上海站】【12月30】
- HDU-5583-Kingdom of Black and White(2015ACM/ICPC亚洲区上海站-重现赛)
- HDU 5572 An Easy Physics Problem(计算几何)——2015ACM/ICPC亚洲区上海站-重现赛
- PostgreSQL basebackup备份
- static class 静态类(Java)
- Java in PowerShell on Windows 10
- etting pink textures in build
- 十一月英语学习
- HDU 5573 Binary Tree 2015ACM/ICPC亚洲区上海站 (构造)
- LeetCode 45. Jump Game II
- 积累的相关资源网址
- DB2 中row_number() over()分析函数用法
- android序列化
- 2.Libgdx扩展学习之Box2D_刚体和形状
- 删除数组零元素
- 1058. 选择题(20)-有点难
- 1130/2016下午