Problem:
Score : 900 points

Problem Statement

Nukes has an integer that can be represented as the bitwise OR of one or more integers between A and B (inclusive).
How many possible candidates of the value of Nukes's integer there are?

Constraints

1 ≤ A ≤ B < 2^{60}

A and B are integers.

Input

The input is given from Standard Input in the following format:

A
B

Output

Print the number of possible candidates of the value of Nukes's integer.

Sample Input 1

7
9

Sample Output 1

4

In this case, A=7 and B=9. There are four integers that can be represented as the bitwise OR of a non-empty subset of {7, 8, 9}: 7, 8, 9 and 15.

Sample Input 2

65
98

Sample Output 2

63

Sample Input 3

271828182845904523
314159265358979323

Sample Output 3

68833183630578410

Solution in Python (3.4.3) (judge verdict: Accepted)


import math
A = int(input())
B = int(input())
if B < A: A,B = B,A

if A == B:
    print(1)
else:
    t = int(math.floor(math.log2(B)))
    while (A & 2**t) == (B & 2**t):
        if A & 2**t:
            A -= 2**t
            B -= 2**t
        t-=1
    ans = 2**t - A              # [A,2^t-1]
    r = t-1
    while 2**t + 2**r > B and r>=0:
        r-=1
    # [2^t,2^t+2^{r+1}-1] \cup [2^t+A,2^{t+1}-1]
    C = 2**(r+1)
    if C < A:
        ans += C + 2**t - A
    else:
        ans += 2**t
    print(ans)