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 1. Say froot=1 froot=1.
And for each node u 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 another N 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 node x 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 exactly K 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.
Given N 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 1 1. Say froot=1 froot=1.
And for each node u 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 another N 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 node x 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 exactly K 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.
Given N N, K K, help the King find a way to collect exactly N N soul gems by visiting exactly K K nodes.
Every test case contains two integers N N and K K, which indicates soul gems the frog king want to collect and number of nodes he can visit.
⋅ ⋅ 1≤T≤100 1≤T≤100.
⋅ ⋅ 1≤N≤109 1≤N≤109.
⋅ ⋅ N≤2K≤260 N≤2K≤260.
Then K K lines follows, each line is formated as 'a b', where a a is node label of the node the frog visited, and b b is either '+' or '-' which means he increases / decreases his number by a 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.
2 5 3 10 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 long
using 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;
}