CodeForces - 635C XOR Equation(搜索||数论)

20 篇文章 0 订阅
20 篇文章 0 订阅

XOR Equation

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.

Examples
input
Copy
9 5
output
4
input
Copy
3 3
output
2
input
Copy
5 2
output
0
Note

In the first sample, we have the following solutions: (2, 7)(3, 6)(6, 3)(7, 2).

In the second sample, the only solutions are (1, 2) and (2, 1).


题意:a+b=s,a^b=x,问有这样的a,b多少个(正整数)

思路:①深搜,x二进制某位为1说明a,b对应位不同,x某位为0,说明a,b对应为同时为1或者0.  深搜看看能不能满足与s对应位相等.由于有大量不合理解,所以不会超时.

②a+b = (a&b)*2+a^b  所以a&b = (s-x)/2,所以若s-x小于0或者为奇数,一定不存在这样的a,b

然后枚举x和(a&b)的每一位即可,如果(a&b)i == 1,xi == 1,一定不存在这样的a,b(肯定吧)

如果(a&b)i == 0,xi == 0,对应a,b位上只有一种情况就是均为0

如果(a&b)i == 0,xi == 1,对应a,b位上有两种情况0,1和1,0


深搜代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 5e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

ll x,s;
int max_len;
int a[520],b[520];

ll dfs(int pos,int jw)
{
	if(pos > max_len&&jw == 0)//如果深搜完并且进位为0,说明此结果可行 
		return 1;
	
	ll tmp = 0;
	if(b[pos] == 0)//如果此位a,b相亦或为0,那么a = b = 0或者a = b = 1 
	{
		if((0+0+jw)%2 == a[pos]) //判断相加后是不是与s对应为相等,下同 
			tmp+= dfs(pos+1,jw/2);
		if((1+1+jw)%2 == a[pos])
			tmp+= dfs(pos+1,(1+1+jw)/2);
	}
	else
	{
		if((1+0+jw)%2 == a[pos])
			tmp+= dfs(pos+1,(1+jw)/2)*2;//此处a上此位为0,b此位为1,或者a此位为1,b此位为0。两种情况所以*2 
	}
	
	return tmp;
}

int main()
{
	cin>>s>>x;
	
	ll ans = s == x?-2:0;
	
	int cnt = 0;
	while(s)//获取s的二进制的每一位 
	{
		a[++cnt] = s%2;
		s/= 2;
	}
	max_len = max(max_len,cnt);
	
	cnt = 0;
	while(x)//获取x的二进制的每一位 
	{
		b[++cnt] = x%2;
		x/= 2;
	}
	max_len = max(max_len,cnt);//记录二者最长值 
	
	ans += dfs(1,0);
	
	cout<<ans<<endl;
	
	return 0;
}

数论代码:

//a+b = (a&b)*2+a^b

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 5e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

ll s,x;

int main()
{
	cin>>s>>x;
	
	if(s-x< 0||(s-x)&1)
	{
		cout<<0<<endl;
		return 0;
	}
	
	ll ans = 1;
	ll And = (s-x)>>1;
	
	for(int i = 0;i<= 40;i++)
	{
		int tmp1 = (And>>i)&1;
		int tmp2 = (x>>i)&1;
		
		if(tmp1&&tmp2)
		{
			cout<<0<<endl;
			return 0;	
		}	
		
		if(tmp1 == 0&&tmp2 == 1)
			ans*= 2;
	}
	
	if(s == x)
		ans-= 2;
	
	cout<<ans<<endl;
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值