Official

D - AND and SUM Editorial by en_translator


Consider the binary notations of \(x\) and \(y\). Let \(A\) be the set of places where both have the digit \(1\), and \(B\) be the set of places where either of them is \(1\) (\(2^0\)’s place, \(2^1\)’s place, \(\cdots\)).

First, it is obvious that \(A \cap B = \emptyset\). Also, we can see that \(\sum_{x \in A} x = a\) and \(2 \times \sum_{x \in A} x)+(\sum_{y \in B} y)=s\).

Therefore, we can recover as \(\sum_{x \in A} x = a\) and \(\sum_{y \in B} y = s-(2 \times \sum_{x \in A} x)\). Since \(B\) and \(\sum_{y \in B} y\) corresponds one-by-one (\(B\) is the set of places with the value \(1\) in the binary notation of \(\sum_{y \in B} y\)), the necessary condition of the existence of a pair of non-negative integers \((x,y)\) that satisfies the conditions in Problem Statement is:

  • \(2a \leq s\) and \((s-2a)\ \text{AND}\ a = 0\).

In fact, this condition is a sufficient condition too. If \(a\) and \(s\) satisfies this condition, the pair \((x,y)\) such that \(x=a,y=s-a\) always satisfies the condition in Problem Statement.

Therefore, the problem has been solved. When implementing, it is useful to use the bit operations equipped with each language.

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;

using ll = long long;

int main(){
	ll T; cin >> T;
	while(T--){
		ll a,s; cin >> a >> s;
		string ans = "No";
		if(2*a <= s){
			ll differ = s-2*a;
			if((differ & a) == 0) ans = "Yes";
		}
		cout << ans << endl;
	}
}

posted:
last update: