计算机学院大学生程序设计竞赛(2015’12)Bitwise Equations

Bitwise Equations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 645    Accepted Submission(s): 343

Problem Description
You are given two positive integers X and K. Return the K-th smallest positive integer Y, for which the following equation holds: X + Y =X | Y
Where '|' denotes the bitwise OR operator.  

Input
The first line of the input contains an integer T (T <= 100) which means the number of test cases. 
For each case, there are two integers X and K (1 <= X, K <= 2000000000) in one line.  

Output
For each case, output one line containing the number Y.
 
Sample Input
  
  
3 5 1 5 5 2000000000 2000000000
 

Sample Output
  
  
2 18 16383165351936

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
#define LL long long
#define INF 0x3f3f3f3f

using namespace std;

int a[110],b[110];
int ant[110];

int main()
{
    int T;
    LL x,k;
    cin>>T;
    while(T-- && cin>>x>>k)
    {
        LL ans = 0;
        int top1 = 0;
        int top2 = 0;
        while(x)
        {
            a[top1++] = x % 2;
            x /= 2;
        }
        while(k)
        {
            b[top2++] = k % 2;
            k /= 2;
        }
        int pos = 0;
        for(int i=0;i<top1;i++)
        {
            if(a[i] == 0)
            {
                if(pos != top2)
                    ant[i] = b[pos++];
                else
                    ant[i] = 0;
            }
            else
                ant[i] = 0;
        }
        int an = top1;
        while(pos < top2)
            ant[an++] = b[pos++];
        for(int i=an-1;i>=0;i--)
            ans = ans * 2 + ant[i];
        cout<<ans<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值