Description
两个正整数a和b的和为s,异或和为x,问满足条件的a和b有多少种情况
Input
两个整数s和x分别表示a和b的和,异或和(2<=s<=1e12,0<=x<=1e12)
Output
输出满足条件的(a,b)对对数
Sample Input
9 5
Sample Output
4
Solution
s=(a^b)+2*(a&b)=x+2*(a&b),知道了a^b和a&b,直接枚举a和b的每一位,如果与是0异或是0那么是0 0,如果与是1异或是0则1 1,如果与是0异或是1则是1 0或者0 1,如果与是1异或是1则无解,注意到如果s=x,那么a&b=0,在统计过程中会有a=0和b=0的两组不合法解,所以要减掉
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 55
ll s,x,y;
int main()
{
while(~scanf("%I64d%I64d",&s,&x))
{
int gg=0,cnt=0;
ll ans=0;
if(s==x)ans=-2;
if(s>=x&&(s-x)%2==0)
{
y=(s-x)/2;
while(x||y)
{
int a=x%2,b=y%2;
x/=2,y/=2;
if(a+b==2)
{
gg=1;
break;
}
if(a==1&&b==0)cnt++;
}
}
else gg=1;
if(gg)printf("0\n");
else printf("%I64d\n",ans+(1ll<<cnt));
}
return 0;
}