https://blog.csdn.net/Lulu11235813/article/details/51995965
上面博客讲解十分到位
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 11. Say froot=1froot=1.
And for each node uu, labels as fufu, the left child is fu×2fu×2 and right child is fu×2+1fu×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 NN years, only if he could collect exactly NNsoul 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 xx, the number at the node is fxfx (remember froot=1froot=1), he can choose to increase his number of soul gem by fxfx, or decrease it by fxfx.
He will walk from the root, visit exactly KK nodes (including the root), and do the increasement or decreasement as told. If at last the number is NN, 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 NN, KK, help the King find a way to collect exactly NN soul gems by visiting exactly KK nodes.
Input
First line contains an integer TT, which indicates the number of test cases.
Every test case contains two integers NN and KK, which indicates soul gems the frog king want to collect and number of nodes he can visit.
⋅⋅ 1≤T≤1001≤T≤100.
⋅⋅ 1≤N≤1091≤N≤109.
⋅⋅ N≤2K≤260N≤2K≤260.
Output
For every test case, you should output " Case #x:" first, where xx indicates the case number and counts from 11.
Then KK lines follows, each line is formated as 'a b', where aa is node label of the node the frog visited, and bb is either '+' or '-' which means he increases / decreases his number by aa.
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 +
分析:
从1走到第k层,下一层的数是上一层的数×2或者×2+1,每次加上或者减去走过的数得到n
输出每行输出这一层的数,再输出加还是减
做法:可以发现每次都往×2走时e可以得到<2^k的所有奇数,然后a将最后一个改为2^k+1就可以在原来的基础上得到所有偶数
如用1,2,4,8通过加减可以得到-1,1,-3,3,-5,5,-7,7,然后1,2,4,9通过加减就可以得到-2,2,-4,4,-6,6,-8,8
然后根据n的二进制就可以确定加还是减,如4层时n的二进制是0011,因为是正数,第一位必为+,再找到第一个是一的,一直减到这一位即可,即8-4-2+1
代码:
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#define MAX 100000
#define LL long long
using namespace std;
int a[70],n,m,k,cas=1,T;
int main()
{
//freopen("/home/user/桌面/in","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&k);
printf("Case #%d:\n",cas++);
n=m&1?m:m-1;
int c=0;
memset(a,0,sizeof(a));
while(n)
{
a[++c]=n&1;
n>>=1;
}
for(int i=k,idx;i>0;i--)
{
idx=i;
while(i>0&&!a[i])//找到为1的那一位
{
i--;
}
//printf("i:%d idx:%d\n",i,idx);
if(i<=0) break;
if(a[i]&&i!=idx) swap(a[i],a[idx]);
}
for(int i=1;i<k;i++) printf("%lld %c\n",1LL<<(i-1),a[i]?'+':'-');
printf("%lld %c\n",1LL<<(k-1)|(!(m&1)),a[k]?'+':'-');
// LL sum=0;for(int i=1;i<=k;i++) sum+=(1LL<<(i-1))*(a[i]?1:-1);
// if(!(m&1)) sum++;printf("%lld\n",sum);
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}