bitmasks math

Please click on ads to support us..

Python Code:

import bisect
import collections
import heapq
import math
import sys
from sys import maxsize

sys.setrecursionlimit(10**6)

p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def MI(): return map(int, sys.stdin.buffer.readline().split())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
def li(): return [int(i) for i in input().split()]
def lli(rows): return [li() for _ in range(rows)]
def si(): return input()
def ii(): return int(input())
def ins(): return input().split()



def drank(d, processing, da, rank):
    tmp = 10 ** 9
    if len(d[da]) == 1:
        return 1
    for di in d[da]:
        if processing[di - 1] == 0:
            processing[di - 1] = 1
            tmp = min(tmp, drank(d, processing, di, rank))
            processing[di - 1] = 0
    rank[da - 1] = tmp + 1
    return tmp + 1



def gcd(a,b):
    if b==0:
        x = 1
        y = 0
        return x, y, a
    x, y, g = gcd(b, a%b)
    return y, x- (a//b)*y, g

        import heapq

def dis(cd, dd, da, root):
    x = root
    while x!=[]:
        z = []
        for xi in x:
            da[xi - 1] = cd
            y = dd[xi]
            for yi in y:
                if da[yi - 1] == -1:
                    z.append(yi)
        x = z.copy()
        cd+=1
    return da

def solve():
    n, m = li()
    if m == 0: return n
    l, r = max(0, n - m), n + m
    ans,x = 0, 1
    while l >= 0:
        x = 2 ** (int(math.log2(r)))
        ans += x
        l, r = l - x, r - x
    return ans+x-1

def main():
            for _ in range(ii()):
        sys.stdout.write(str(solve()) + "\n")
                                    
            



if __name__ == "__main__":
    main()


Comments

Submit
0 Comments
More Questions

227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet