计算机学院大学生程序设计竞赛(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;
}
相关文章推荐
- 计算机学院大学生程序设计竞赛(2015’12)Happy Value(最大生成树)
- 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
- 网络层—广播路由选择算法
- 计算机学院大学生程序设计竞赛(2015’12)The Country List(水题)
- 网络配置笔记整理
- 数据结构实验之排序四:寻找大富翁(堆排序)
- TCP/IP (五) TCP连接的内存使用
- 计算机网络常见基本概念
- 计算机学院大学生程序设计竞赛(2015’12) 1006 01 Matrix(dp)
- 堆
- 三角网格数据结构(2)
- HttpClient用Post请求网络数据
- 三维网格数据结构
- Linux网络编程入门
- android之旅11 网络编程实例:多线程下载与断点续传
- http 监测器
- 约瑟夫环问题的几种解法
- Android检测网络连接
- (1)TCP/IP简介
- SDUT 数据结构实验之排序六:希尔排序