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
 
 
题目大意:X + Y =X | Y。给出X和一个数K,问能使该式成立的第k小的Y是多少。
题目分析:要想使上式成立,X和Y必须没有交集。所以,此问题其实是求X的补集中第K小的子集是多少。
 
代码如下:
# include<iostream>
# include<cstdio>
# include<cmath>
# include<string>
# include<vector>
# include<list>
# include<set>
# include<map>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std; # define LL long long
# define REP(i,s,n) for(int i=s;i<n;++i)
# define CL(a,b) memset(a,b,sizeof(a))
# define CLL(a,b,n) fill(a,a+n,b) const double inf=1e30;
const int INF=1<<30;
const int N=1000; unsigned long long n,k; const unsigned long long a=0xffffffffffffffff;
unsigned long long ans; void dfs(unsigned x)
{
if(x<=0) return ;
int t=x;
int base=0;
while(t){
t/=2;
++base;
}
unsigned long long p=1;
int id;
for(int cnt=0,i=0;i<64&&cnt<=base;++i){
id=i;
if(n&(p<<i)){
++cnt;
if(cnt==base) ans|=(p<<i);
}
}
dfs(x-(p<<(base-1)));
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
cin>>n>>k;
n=~n;
ans=0;
dfs(k);
cout<<ans<<endl;
}
return 0;
}

  

Bitwise Equations的更多相关文章

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

    Bitwise Equations Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

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

    #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using ...

  3. CF 1064B Equations of Mathematical Magic(思维规律)

    Description Colossal! — exclaimed Hawk-nose. — A programmer! That's exactly what we are looking for. ...

  4. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  5. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. JS魔法堂:再识Bitwise Operation & Bitwise Shift

    Brief linkFly的<JavaScript-如果...没有方法>中提及如何手写Math.round方法,各种奇技淫招看着十分过瘾,最让我惊叹的是 ~~(x + )) ,完全通过加法 ...

  8. A.Kaw矩阵代数初步学习笔记 5. System of Equations

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  9. 【leetcode】Bitwise AND of Numbers Range(middle)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

随机推荐

  1. HTML实例 - 购物商场页面

    效果图 代码 https://coding.net/u/James_Lian/p/Shopping-page/git 示例 https://coding.net/u/James_Lian/p/Shop ...

  2. MySQL插入性能优化(转)

    原文:http://tech.uc.cn/?p=634 对于一些数据量较大的系统,数据库面临的问题除了查询效率低下,还有就是数据入库时间长.特别像报表系统,每天花费在数据导入上的时间可能会长达几个小时 ...

  3. SQL基础--查询之二--连接查询

    SQL基础--查询之二--连接查询

  4. kubernetes实战(五):k8s持久化安装Redis Sentinel

    1.PV创建 在nfs或者其他类型后端存储创建pv,首先创建共享目录 [root@nfs ~]# cat /etc/exports /k8s/redis-sentinel/ *(rw,sync,no_ ...

  5. 安装Anaconda3

    wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh bash Anaconda3-5.0.1-Linux-x8 ...

  6. 高性能Web服务器Nginx

    高性能Web服务器Nginx介绍 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.其特点是占有内存少,并发能力强 ...

  7. L1正则与L2正则

    L1正则是权值的绝对值之和,重点在于可以稀疏化,使得部分权值等于零. L1正则的含义是 ∥w∥≤c,如下图就可以解释为什么会出现权值为零的情况. L1正则在梯度下降的时候不可以直接求导,可以有以下几种 ...

  8. 非线性方程(组):一维非线性方程(一)二分法、不动点迭代、牛顿法 [MATLAB]

    1. 二分法(Bisection) 1) 原理 [介值定理] 对于连续的一元非线性函数,若其在两个点的取值异号,则在两点间必定存在零点. [迭代流程] 若左右两端取值不同,则取其中点,求其函数值,取中 ...

  9. swoole gets

    控制器调用: function gets() { $model = Model('ap_pic'); $model->select = ' id, size_type '; $gets['pag ...

  10. 算法第四版 在Linux 中调用Algs4库

    一: 搭建Java 环境   : 确认版本: 1.8及以上 [username:~/] javac -version javac 1.8.0_111 [username:~/] java -versi ...