给定非负整数a和s,问是否存在一组非负整数(x,y),满足x&y=a
,并且x+y=s
?
数据范围:0<=a,s<2^60
思路:异或是不进位加法,如果考虑进位,加上按位与的结果左移1位即可,也就是:x+y=(x^y)+((x&y)<<1)
,代入得x^y=s-2a
,并且x&y=a
,逐位分析可知,按位与的结果为1时,异或结果必为0。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a; i<=b; i++)
#define per(i,a,b) for(int i=b; i>=a; i--)
void solve() {
int a, s;
cin >> a >> s;
if (s < 2 * a) {
cout << "No\n";
return;
}
s -= 2 * a;
if ((s & a) != 0) {
cout << "No\n";
return;;
}
cout << "Yes\n";
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1; cin >> t;
while (t--) solve();
return 0;
}
标签:abc238D,非负,int,long,solve,按位,两数
From: https://www.cnblogs.com/chenfy27/p/18060063