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.
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.
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;
}

本文探讨了一道关于位运算的编程题,题目要求找出满足特定条件的第K小正整数Y。通过分析输入X的二进制表示并结合K的值,文章提供了一种算法来高效解决这个问题。
1569

被折叠的 条评论
为什么被折叠?



