Zookeeper and The Infinite Zoo (Codeforces 1491D)
Tags: Greedy, bitmasks

There is a new attraction in Singapore Zoo: The Infinite Zoo.The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled $$$1,2,3,\ldots$$$. There is a directed edge from vertex $$$u$$$ to vertex $$$u+v$$$ if and only if $$$u\&v=v$$$, where $$$\&$$$ denotes the bitwise AND operation. There are no other edges in the graph.Zookeeper has $$$q$$$ queries. In the $$$i$$$-th query she will ask you if she can travel from vertex $$$u_i$$$ to vertex $$$v_i$$$ by going through directed edges.

Input: The first line contains an integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$) — the number of queries.The $$$i$$$-th of the next $$$q$$$ lines will contain two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i, v_i < 2^{30}$$$) — a query made by Zookeeper.

Output: For the $$$i$$$-th of the $$$q$$$ queries, output "YES" in a single line if Zookeeper can travel from vertex $$$u_i$$$ to vertex $$$v_i$$$. Otherwise, output "NO".You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.

Note: The subgraph on vertices $$$1,2,3,4,5,6$$$ is shown below.  

Sample Inputs:
5
1 4
3 6
1 6
6 2
5 5

Sample Outputs:
YES
YES
NO
NO
YES

Solution (Python):
import sys
pin = sys.stdin.readline
Q = int(pin())
for q in range(Q):
    (u, v) = map(int, pin().split())
    if u > v:
        print('NO')
    else:
        (u, v) = (bin(u)[2:][::-1], bin(v)[2:][::-1])
        if u.count('1') < v.count('1'):
            print('NO')
        else:
            F = 1
            S = 0
            for i in range(len(u)):
                if u[i] == v[i]:
                    continue
                elif u[i] == '1':
                    S += 1
                else:
                    S -= 1
                    if S < 0:
                        F = 0
                        break
            print(['NO', 'YES'][F])

Solution (Python):
from collections import OrderedDict
from math import log2

def solve():
    n = int(input())
    for _ in range(n):
        (a, b) = map(int, input().split())
        flag = a <= b
        num = 0
        for i in range(31):
            if a >> i & 1:
                num += 1
            if b >> i & 1:
                num -= 1
            if num < 0:
                flag = False
                break
        if flag:
            print('YES')
        else:
            print('NO')
if __name__ == '__main__':
    t = 1
    for _ in range(t):
        solve()

Solution (Python):
import gc
import heapq
import itertools
import math
from collections import Counter, deque, defaultdict
from sys import stdout
import time
from math import factorial, log, gcd
import sys
from decimal import Decimal
import threading
from heapq import *
from fractions import Fraction
import bisect

def S():
    return sys.stdin.readline().split()

def I():
    return [int(i) for i in sys.stdin.readline().split()]

def II():
    return int(sys.stdin.readline())

def IS():
    return sys.stdin.readline().replace('\n', '')

def main():
    (u, v) = I()
    if u > v:
        print('NO')
        return
    (u, v) = map(lambda x: bin(x)[2:][::-1], [u, v])
    idx_u = []
    for i in range(len(u)):
        if u[i] == '1':
            idx_u.append(i)
    c = v.count('1')
    if c > len(idx_u):
        print('NO')
        return
    idx = 0
    for i in range(len(v)):
        if v[i] == '1':
            if i < idx_u[idx]:
                print('NO')
                return
            idx += 1
            if idx == c:
                break
    print('YES')
if __name__ == '__main__':
    for _ in range(II()):
        main()