hdu 5573 Binary Tree
来源:互联网 发布:数据库中的模式是 编辑:程序博客网 时间:2024/04/29 20:11
B - Binary Tree
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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 . Say .
And for each node , labels as , the left child is and right child is . 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 years, only if he could collect exactly 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 , the number at the node is (remember ), he can choose to increase his number of soul gem by , or decrease it by .
He will walk from the root, visit exactly nodes (including the root), and do the increasement or decreasement as told. If at last the number is , 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 , , help the King find a way to collect exactly soul gems by visiting exactly nodes.
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
And for each node
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
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
He will walk from the root, visit exactly
Noting as the soul gem is some kind of magic, the number of soul gems the king has could be negative.
Given
Input
First line contains an integer , which indicates the number of test cases.
Every test case contains two integers and , which indicates soul gems the frog king want to collect and number of nodes he can visit.
.
.
.
Every test case contains two integers
Output
For every test case, you should output " Case #x:" first, where indicates the case number and counts from .
Then lines follows, each line is formated as 'a b', where is node label of the node the frog visited, and is either '+' or '-' which means he increases / decreases his number by .
It's guaranteed that there are at least one solution and if there are more than one solutions, you can output any of them.
Then
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
25 310 4
Sample Output
Case #1:1 +3 -7 +Case #2:1 +3 +6 -12 +
思路:对于这种题一般都是要构造答案的。首先发现一直向左走的话最多能得到2^(k+1)-1,题目给定n<=2^(k),所以剩下就要思考怎么构造出答案,我们可以试想一直向左下走的途中减去其中某些数就能得到答案,那么应该减去哪些数呢,这里我列个式子x+y=z1,x-y=z2,y=(z1-z2)/2,假设z2为N,z1为2^(k+1)-1,那么x为加上某个数就能得到z1减去某个数就能得到z2的状态,也就是向下过程中把权值和为y的数减去就构造出答案了,这里要注意z1-z2可能是奇数,出现这种情况时,我们可以选择第k步走右边,也就是可以得到的权值最多为2^(k+1),这样就变成偶数了,下面给代码:
#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<iostream>#include<algorithm>#include<cmath>#include<bitset>using namespace std;#define maxn 100005typedef long long LL;bitset<65>bit;int main(){int t;scanf("%d", &t);for (int tcase = 1; tcase <= t; tcase++){int n, k;scanf("%d%d", &n, &k);printf("Case #%d:\n", tcase);if (n & 1){LL div = ((1ll << k) - 1 - n) / 2;bit = div;for (int i = 0; i<k; i++){if (bit[i] & 1){printf("%lld -\n", 1ll << i);}else{printf("%lld +\n", 1ll << i);}}}else{LL div = ((1ll << k) - n) / 2;bit = div;for (int i = 0; i<k-1; i++){if (bit[i] & 1){printf("%lld -\n", 1ll << i);}else{printf("%lld +\n", 1ll << i);}}printf("%lld +\n", (1ll << (k - 1)) + 1);}}}
0 0
- hdu 5573Binary Tree
- hdu 5573 Binary Tree
- hdu 5573 Binary Tree
- 【HDU 5573】Binary Tree
- HDU 5573 Binary Tree
- HDU 5573 Binary Tree【构造】
- HDU 5573 Binary Tree 构造
- hdu 5573 Binary Tree 构造
- hdu 5573Binary Tree(二进制)
- HDU 5573-Binary Tree (构造)
- hdu 5573 Binary Tree(构造)
- hdu 5573 Binary Tree【思维+递推】
- HDU 5573 Binary Tree 找规律二进制
- Hdu-5573 Binary Tree(贪心构造)
- 【规律】【贪心】【数学】HDU 5573 Binary Tree
- HDU-5573 Binary Tree(构造/二进制)
- HDU 5573 Binary Tree(规律题)
- hdu 5573 Binary Tree【脑洞构造】
- 52. N-Queens II
- const 的简要介绍
- 汉诺塔游戏中的递归算法
- CodeForces 551E GukiZ and GukiZiana 【分块】
- 单片机中用lcd显示电池充电
- hdu 5573 Binary Tree
- 以生活例子说明单线程与多线程
- Linux_Hi3518 SDK 安装以及编译出错分析
- java小知识点(1)
- 欢迎使用CSDN-markdown编辑器
- Android 小项目之---- 计算器 使用同一个事件监听完成 加减乘
- 打不死的redis集群
- IO - 同步,异步,阻塞,非阻塞
- Tautology