您的位置:首页 > 理论基础

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

2015-12-27 16:34 411 查看


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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: