CodeForces 627 A.XOR Equation(位运算)

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值