数学-二进制
D - AND and SUM
题目链接
https://atcoder.jp/contests/abc238/tasks/abc238_d
题意
给你a和s,问是否存在x和y,使得\(x + y = s, x \& y = a\)
解析
\(x + y = x \& y + x | y\)
已知\(x \& y = a\), 所以\(x | y = s - a\), 而\((x | y - x \& y)\& (x \& y) = 0\),
所以只需求出\(s - 2 * a\),与a相与便可以判断这里\(s - a\)是不是恰好是\(x | y\).
Ac代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a, s;
int t;
int main()
{
scanf("%d", &t);
while(t --)
{
scanf("%lld%lld", &a, &s);
if(s - 2 * a >= 0 && ((s - 2 * a) & a) == 0) puts("Yes");
else puts("No");
}
return 0;
}

浙公网安备 33010602011771号