【杭电2015年12月校赛E】【二进制拆分】Bitwise Equations 第K小的X满足X或Y=X+Y
2015-12-28 13:28
459 查看
Bitwise Equations
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 657 Accepted Submission(s): 352
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case, output one line containing the number Y.
[align=left]Sample Input[/align]
3
5 1
5 5
2000000000 2000000000
[align=left]Sample Output[/align]
2
18
16383165351936
#include<stdio.h> #include<string.h> int casenum,casei; typedef long long LL; LL b[70]; int main() { for(int i=0;i<63;++i)b[i]=1ll<<i; scanf("%d",&casenum); for(casei=1;casei<=casenum;++casei) { LL X,K; scanf("%lld%lld",&X,&K); LL ans=0; int p=0; for(int i=0;i<63;++i)if(!(X&b[i])) { if(K&b[p++])ans|=b[i]; } printf("%lld\n",ans); } return 0; } /* 【trick&&吐槽】 X和K最多只有31个二进制位 所以,答案最多只会有62个二进制位。 用LL就可以AC~ 【题意】 有T(100)组数据, 对于每组数据,给定X,K(1<=X,K<=2e9) 让你求出,满足X+Y==X|Y的第K小的Y 【类型】 二进制拆分 【分析】 很显然, X+Y==X|Y的Y,需要满足的性质是—— X二进制是1的位置,Y必须是0; 于是,我们扫描所有X为0的位置, 在这些位置填充一个数值为K的二进制数即可。 具体实现,可以通过模拟代码慢慢体会~ 【时间复杂度&&优化】 O(log(X)) */
相关文章推荐
- 使用位运算实现网页中的过滤、筛选功能实例
- C#枚举中的位运算权限分配浅谈
- shell 基本计算、逻辑运算、位运算详解
- Java位运算和逻辑运算的区别实例
- 优秀程序员必须知道的20个位运算技巧
- JavaScript使用位运算符判断奇数和偶数的方法
- java位运算加密示例
- c语言中用位运算实现加法技巧介绍
- Java 位运算(移位、位与、或、异或、非)
- 位运算应用口诀和实例
- 【位运算】之 异或
- JavaScript 位运算笔记
- 想知道&&与&及||与|之间的区别吗?
- 想知道&&与&及||与|之间的区别吗?
- 不用if判断将字母进行大小写转换
- 位级运算的一点随笔
- 优秀程序员不得不知道的20个位运算技巧
- mysql位运算的应用
- 非10进制在Java中的应用
- 利用位运算实现加法运算