题解 CF76D 【Plus and xor】
这题对于我来说有点神仙
题意:
给出两整数A,B,要求找到X,Y满足以下两个个条件:
1.A=X+Y
2.B=X xor Y 其中xor表示异或运算
且要求 X 是所有满足前两个条件中最小的
如果无解,输出-1
否则,输出 X,Y
题解
这题先从整体来看:
两个数 X,Y,如果有一位同为1(就是两个1),那么加法运算后 就是 10(当然不管前面的二进制数),xor运算后就是 0,将前者除以 2,变成01,就是两位全是 1 的这一位。其他情况 xor运算与加法运算结果相同,所以
Code
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
#define int unsigned long long
using namespace std;
inline int read() {
int x=0,f=1; char ch=getchar();
while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
return x * f;
}
int A,B,c;
signed main()
{
A = read(), B = read();
c = A-B;
if(c<0 || c&1) puts("-1");
else {
cout<<(A-B)/2<<" "<<A-((A-B)/2)<<endl;
}
return 0;
}
/*
142
76
33 109
*/
如果不懂最好不要拿数据去模拟,否则会越来越糊涂。(我就是这样的)