2015.12 HDU Bitwise Equations

1900: Bitwise Equations

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



注意最大有64位数,只需将所求的项数依次填到原来数字是0的位置即可。
#include <bits/stdc++.h>
using namespace std;
#define  ll long long
const int MAXN = 70;
const int INF  = 1e9;
const int mod  = 1000000007*2;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ll x,k,cnt = 0;
        int s[MAXN];
        cin >> x >> k;
        while(k != 0)
        {
            if(x&1) s[cnt++] = 0;
            else
            {
                s[cnt++] = k&1;
                k >>= 1;
            }
            x >>= 1;
        }
        ll ans = 0;
        for(int i = 0; i < cnt; i++)
            if(s[i]) ans += pow(2,i);
        printf("%lld\n",ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/flemington/p/6027895.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值