分析
对于加法我们可以考虑为,两个为 ,那么该位变为
,下一位
。而异或满足两个为
,那么该位变为
。而下一位
,则可以考虑哪些位要进位,所以我们得到第一个式子
。但是我们要考虑到进位之后
和
要没有同一个位为
,这才是可行的方案。
代码
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL read() {
LL x = 0,f = 0;char ch = getchar();
while(!isdigit(ch)) {if(ch == '-')f=1;ch=getchar();}
while(isdigit(ch)) {x = x * 10 + ch - '0';ch = getchar();}
return f ? -x : x;
}
LL t,x,y;
int main() {
t = read();
while(t--) {
x = read();y = read();
if(x >= 2 * y && ((((x - 2 * y ) | y) == ((x - 2 * y) + y))) ) printf("%lld\n",x-2*y);
else printf("-1\n");
}
return 0;
}
京公网安备 11010502036488号