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. TA-Lib中文文档(二):talib安装

    安装 使用pip安装 PyPI: $ pip install TA-Lib Or checkout the sources and run setup.py yourself: $ python se ...

  2. python的pip的配置文件路径在哪里?如何修改pypi源?

    官方文档: https://pip.pypa.io/en/stable/user_guide/#configuration 举个例子: Windows用户想要更改pypi源,可以在%APPDATA%目 ...

  3. PostgreSQL数据库的安装与PostGIS的安装(转)

    原文:http://lovewinner.iteye.com/blog/1490915 安装postgresql sudo apt-get install postgresql-9.1 postgre ...

  4. 三个在线django速成教程(转)

    add by zhj: 除了这几个在线的,有些书也不错 1. Two Scoops of Django 2. Instant Django 1.5 Application Development St ...

  5. (2.5)DDL增强功能-触发器trigger

    SQL Server:触发器详解   1. 概述 2. 触发器的分类 3. Inserted和Deleted表 4. 触发器的执行过程 5. 创建触发器 6. 修改触发器: 7. 删除触发器: 8. ...

  6. CSS背景以及文本

    css设置背景: <style type="text/css"> /*background-image: 直接设置x,y重复而且平铺整个body*/ /*下面两句的功能 ...

  7. JOJ1202。重新操刀ACM,一天一练!做个简单的题目温习。

    http://ac.jobdu.com/problem.php?pid=1202 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100).   ...

  8. 记一次mysql启动不了的问题

    在linux上用的是xampp,mysql启动没有报任何错误,但就是查找不到进程,于是找mysql错误日志,日志在哪?在lampp/var/mysql 以.err结尾的文件里.里面内容如下; /opt ...

  9. python16_day28【crm只读、权限、堡垒机】

    一.只读 二.万能权限 三.堡垒机

  10. 前端面试题之 sum(2)(3) (链式调用,toString,柯里化,数组操作)

    写一个函数让下面两个输出结果相同:console.log(sum(2)(3));console.log(sum(2,3)); var sum = (function() { var list = [] ...