#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
int x, y;
while (n--) {
// s1,s2分别存x和y中1的位置。
stack<int> s1, s2;
cin >> x >> y;
if (x > y) {
cout << "NO" << endl;
continue;
}
for (int i = 0; x != 0; ++i) {
int t = x % 2;
if (t == 1) s1.push(i);
x /= 2;
}
for (int i = 0; y != 0; ++i) {
int t = y % 2;
if (t == 1) s2.push(i);
y /= 2;
}
// 如果x中1比y中少,"NO"。
if (s1.size() < s2.size()) {
cout << "NO" << endl;
continue;
}
// 标志位,所有"NO"的条件都会导致flag变成false
bool flag = true;
while (s1.size() && s2.size()) {
// 如果 u 中 1 在 v中1左边,"NO"。因为u不能右移操作。
if (s1.top() > s2.top()) {
flag = false;
break;
}
s1.pop();
s2.pop();
if(s2.size() == 0) break;
// 如果u中有多余的1,将其弹出。(多余=可连续进位)
while (s1.size() && s1.top() > s2.top()) {
s1.pop();
}
// 如果x中1比y中少,"NO"。
if(s1.size() < s2.size()){
flag = false;
break;
}
}
cout << (flag ? "YES" : "NO") << endl;
}
return 0;
}
Codeforces 1491 D. Zookeeper and The Infinite Zoo (二进制处理)
原文:https://www.cnblogs.com/zhangjiuding/p/14465293.html