Or Sum#12338

Or Sum (Editoryal)

Problem Author:

Khoi Le

Prepared by:

Khoi Le

Edited by:

Hasan Saleh (MathModel)

Observation : It's optimal to pick the integers with highest bits on

Notice for k \ge 2 we can pick two integers such that

\text{bin}(x) \cup \text{bin}(y)=\underbrace{1111...1}_{\lfloor \log_2(n) \rfloor}=2^{\lfloor \log_2(n) \rfloor+1}-1

We shall prove by construction :

Let x=2^{\lfloor \log_2(n) \rfloor}=10000...0 and y=x-1 i.e. then

x|y=\underbrace{1111...1}_{\lfloor \log_2(n) \rfloor}=2^{\lfloor \log_2(n) \rfloor+1}-1

If k=1 then the answer is .

def solve(): 
    n,k=map(int,input().split())
    if(k==1):
       print(n)
    else:
       print((1 << n.bit_length())-1)
for _ in range(int(input())):
    solve()