codeforces 627 A. XOR Equation(位运算,结论)

4 篇文章 0 订阅
1 篇文章 0 订阅

http://codeforces.com/contest/627/problem/A
题意:
两个数a,b a+b=s a xor b= x
给出s,x 问有多少对a,b满足条件

首先有个结论,(其实自己也能推出来,但自己太菜没想出来)
在这里插入图片描述
拥有这个公式后,这道题变得简单了些,移项后求出
a&b = (s - x) / 2,并且判断是否合法
a ^ b 我们已经知道,那么只需要通过a&b,确定一下a,b
如果在这里插入图片描述 那么 ai = bi, 那么 ai = bi = ai&bi,此时a,b已确定为1种

在这里插入图片描述
那么 ai&bi = 0 否则无解,此时a,b可为1,0 或0,1 所以答案*2

当s==x 会有 (0,x) (x,0)不合法答案 减2

#include<bits/stdc++.h>
#define fi first
#define se second
#define log2(a) log(n)/log(2)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
#define tim printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
using namespace std;
 
typedef long long ll;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<P, ll> LP;
const ll inf = 1e9+100;
const int N = 1e6 + 10;
const ll mod = 1e9+7;
const int base = 131;
const double pi = acos ( -1 );
const double eps = 1e-8;
inline ll mul(ll x,ll y) { return (x*y-(ll)((long double)x*y/mod)*mod+mod)%mod;}
inline ll ksm(ll a,ll b) {ll ans=1;while(b){if(b&1)ans=mul(ans,a);a=mul(a,a),b>>=1;}return ans;}
 
#define a(i,j) a[(i-1)*m+(j)]
#define b(i,j) b[(i-1)*m+(j)]
 
unordered_map<ll, ll> mp;
 
 
ll n, m,x,y,z,cx,k;
ll ans;
ll a[N],b[N],c[N],vis[N],pos[N];
set<ll> st;
P p[N];
ll sum,cnt,flag;
 
 
int main()
{
 
 
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
 
	ll s,x;
	cin>>s>>x;
	ll ad=(s-x)>>1;
	if((s-x)%2!=0||s<x) flag=1;
	ans=1;
	for(int i=0;i<=60;i++)
	{
		if((x>>i)&1)
		{
			if((ad>>i)&1)
			{
				flag=1;
				break;
			}
			else ans<<=1;
		}
	}
	if(s==x) ans-=2;
	if(flag) cout<<0;
	else cout<<ans<<endl;
 
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值