CodeForces 635C XOR Equation(数论)

题意:给定a+b的和,以及a和b的异或,问有多少组这样的(a,b)

思路:百度到a+b=(a&b)*2+a^b,那么显然我们就可以把a&b的值求出来,那么根据a&b和a^b就可以统计出对数,另外注意的是当s==x的时候要特判减去2对包含0的情况


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 100000
#define LL long long
int cas=1,T;

int main()
{
	LL s,x;
	while (scanf("%lld%lld",&s,&x)!=EOF)
	{
		int flag = 0;
		if (s==x)
			flag=1;
		s-=x;
		if (s%2==1)
		{
			puts("0");
			continue;
		}
		s/=2;
        LL ans = 1;
		for (int i = 0;i<60;i++)
		{
			int x1=(s>>i)&1;
			int x2=(x>>i)&1;
			if (x1==1 && x2==1)
			{
				puts("0");
				return 0;
			}
			if (x2==1)
				ans*=2;
		}
		if (!flag)
		   printf("%lld\n",ans);
		else
		   printf("%lld\n",ans-2);
	}
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

Description

Two positive integers a and b have a sum of s and a bitwise XOR of x. How many possible values are there for the ordered pair (a, b)?

Input

The first line of the input contains two integers s and x (2 ≤ s ≤ 10120 ≤ x ≤ 1012), the sum and bitwise xor of the pair of positive integers, respectively.

Output

Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0.

Sample Input

Input
9 5
Output
4
Input
3 3
Output
2
Input
5 2
Output
0



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值