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. Redis讲解

    buffer  缓冲  用于写 cache  缓存  用于读 redis 支持持久化 安装redis yum -y install redis 配置文件/etc/redis.conf 是否在后台运行 ...

  2. ValueError: Only call `sparse_softmax_cross_entropy_with_logits` with named a

    第五章中完整的训练MNIST数据的神经网络模型的程序代码中,直接运行程序的话会遇到以下的错误. 把下面的这行代码 # 计算交叉熵及其平均值 cross_entropy = tf.nn.sparse_s ...

  3. Tunnelblick 覆盖安装失败

    公司搬家, jira, sourceTree都链接不上了,发现是VPN断了的原因,需要重新链接VPN Tunnelblick坏掉了,覆盖安装总是提示安装失败,安装超时 nil,搜索一大堆文章, 看了这 ...

  4. python面向对象(类的成员及类方法)

    类的普通成员 字段 方法 属性 类的高级成员 静态字段 静态方法 属性方法   类方法 类成员修饰符 类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象中, ...

  5. Docker+Jenkins_自动化持续集成环境搭建

    前一篇文章里已经在内网环境搭建好docker 详见:https://www.cnblogs.com/befer/p/9107503.html, 现在接着搭建一个Docker+Jenkins的集成环境 ...

  6. (转)二十三种设计模式及其python实现

    本文源码寄方于github:https://github.com/w392807287/Design_pattern_of_python 参考文献: <大话设计模式>——吴强 <Py ...

  7. ZOHO 免费小型企业邮箱和个人邮箱

    Zoho Mail 提供免费小型企业邮箱注册.精简版只能添加一个域到您的机构帐号,最多允许10用户.如果您想添加多个域,您可以升级到标准版.10用户免费,5 GB /每用户,5 GB (共享). 除了 ...

  8. docker——网络配置

    一.网络启动与配置参数 Docker启动时会在主机上自动创建一个docker0虚拟网桥,实际上是一个Linux网桥,可以理解为一个软件交换机,它会在挂载其上的接口之间进行数据转发.同时,Docker随 ...

  9. Hadoop HDFS的Shell操作实例

    本文发表于本人博客. 我们知道HDFS是Hadoop的分布式文件系统,那既然是文件系统那最起码会有管理文件.文件夹之类的功能吧,这个类似我们的Windows操作系统一样的吧,创建.修改.删除.移动.复 ...

  10. 服务器相关 HTTP 请求错误

    HTTP 400 - 请求无效 HTTP 401.1 - 未授权:登录失败 HTTP 401.2 - 未授权:服务器配置问题导致登录失败 HTTP 401.3 - ACL 禁止访问资源 HTTP 40 ...