hdu 5573Binary Tree(二进制)

来源:互联网 发布:仙侣情缘手游java 编辑:程序博客网 时间:2024/05/17 06:47
Binary TreeTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1034    Accepted Submission(s): 609Special JudgeProblem DescriptionThe 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.InputFirst 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.OutputFor 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 Input25 310 4Sample OutputCase #1:1 +3 -7 +Case #2:1 +3 +6 -12 +Source2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)Recommendwange2014
位运算查找一个数二进制中1的位置: for(int i=k;i>=0;i--){            if((div>>i)&1){                vis[i]=true;            }        }
挺不错的一道构造题目。首先呢,先看清楚限制条件N<=2^k<=2^60,很重要。我们就可以从二进制的角度出发。一开始想到的是2^0,2^1...2^k-1等。确实是从这个方面出发的。那么我们只需要考虑树的最左边一列(1,2,4,8,16.....)。等比数列得出sum=(2^k)-1;这样子sum肯定比N大,那么多出来的部分我们选择减掉,应该怎么减掉呢。设置div=sum-n(需要根据奇偶性来判断)如果div为奇数的话,那么我们只需要把把最后一步的树的节点右移即可,那么sum=2^k,则div为偶数了。还要就是我们要把div/2(这里应该可以懂吧),然后在2^0,2^1,...2^(k-1)中找出能够构成div/2的数即可,这些数字是需要减去的,所以对应的符号是"-".这样子就可以了。
#include<iostream>#include<cstring>#include<cstdlib>#include<algorithm>#include<cctype>#include<cmath>#include<ctime>#include<string>#include<stack>#include<deque>#include<queue>#include<list>#include<set>#include<map>#include<cstdio>#include<limits.h>#define MOD 1000000007#define fir first#define sec second#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)#define mes(x, m) memset(x, m, sizeof(x))#define Pii pair<int, int>#define Pll pair<ll, ll>#define INF 1e9+7#define inf 0x3f3f3f3f#define Pi 4.0*atan(1.0)#define lowbit(x) (x&(-x))#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define max(a,b) a>b?a:btypedef long long ll;typedef unsigned long long ull;const double eps = 1e-12;const int maxn = 3000;using namespace std;inline int read(){    int x(0),f(1);    char ch=getchar();    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();    return x*f;}bool vis[61];ll n,k;//bool dfs(int cnt,ll re,ll sum){//    if(cnt==k){//        return sum==re;//    }//    vis[cnt]=false;//    if(dfs(cnt+1,re,sum)){//        return true;//    }//    vis[cnt]=true;//    if(dfs(cnt+1,re,sum+(1<<cnt))){//        return true;//    }//    return false;//}int main(){    int t;    t=read();    for(int i=1;i<=t;++i){        mes(vis,false);        scanf("%lld%lld",&n,&k);        ll flag=0;        ll sum=(1<<k)-1;        if((sum-n)&1){            sum+=1;            flag=1;        }        ll div=(sum-n)/2;        stack<int> st;        while(div){            st.push(div%2);            div/=2;        }        int l=(int)(st.size()-1);        while(!st.empty()){            if(st.top()&1){                vis[l]=true;            }            st.pop();            --l;        }        printf("Case #%d:\n",i);        for(int i=0;i<k;++i){            if(vis[i]==true){                cout<<(1<<i)<<" "<<"-"<<endl;            }else{                cout<<((1<<i)+(i==k-1?flag:0))<<" "<<"+"<<endl;            }        }    }    return 0;}
0 0
原创粉丝点击