【HDU
来源:互联网 发布:网络桥接怎么设置 编辑:程序博客网 时间:2024/06/18 06:40
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, is1 1. Say froot=1 froot=1.
And for each nodeu u, labels as fu fu, the left child is fu×2 fu×2 and right child is fu×2+1 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 anotherN N years, only if he could collect exactly N Nsoul 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 nodex x, the number at the node is fx fx (remember froot=1 froot=1), he can choose to increase his number of soul gem by fx fx, or decrease it by fx fx.
He will walk from the root, visit exactlyK K nodes (including the root), and do the increasement or decreasement as told. If at last the number is N 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.
GivenN N, K K, help the King find a way to collect exactly N N soul gems by visiting exactly K K 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
Every test case contains two integers
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.
25 310 4
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 longusing 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;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- VS2013 UAC模式看不到网络驱动器
- PAT乙级 1069. 微博转发抽奖(20)
- 2017 多校 Function(置换群
- 802.11控制帧&管理帧
- STL容器vector的下标运算符[]
- 【HDU
- centos7 nginx 支持php
- js程序设计思维和if语句案例
- 程序设计思维
- Shell-alias在Shell脚本中的使用
- Js程序设计思维模式
- 嵌入式Linux完全开发手册-------------构建根文件系统
- 深入理解Linux内核-第四章笔记
- WP增加备案号超链接