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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

ll A, B;

ll calc1(ll a, int bit) {
    if(bit == -1) return 1;
    for(int i = bit; i >= 0; i--) {
        if(a & (1LL << i)) return 2 * (1LL << i);
    }
}
ll calc2(ll a, int bit) {
    if(bit == -1) return 1;
    for(int i = bit; i >= 0; i--) {
        if(a & (1LL << i)) continue;
        return (1LL << i) + calc2(a, i - 1);
    }
}

ll solve3(ll a, ll b, int bit) {
    if(bit == -1) return 1;
    if(b >= (1LL << bit)) return solve3(a >= (1LL << bit)? (1LL << bit) - 1 : a, b - (1LL << bit), bit - 1);
    else return solve3(a >= (1LL << bit)? (1LL << bit) - 1 : a, b, bit - 1) + (1LL << bit);
}

ll solve2(ll a, ll b, int bit) {
    if(a + 1 >= b) return (1LL << (bit + 1));
    if(a & (1LL << bit)) return (1LL << (bit + 1));
    if(b < (1LL << bit)) return (1LL << bit) + solve2(a, b, bit - 1);
    return calc1(a, bit) + solve3(a, b, bit);
}

ll solve1(ll a, ll b, int bit) {
    if(a > b) return 0;
    if(bit == -1) return 1;
    if(a < (1LL << bit) && (1LL << bit) <= b) return solve2(b - (1LL << bit), a, bit - 1) + calc2(a, bit - 1);
    else return solve1(a & ((1LL << bit) - 1), b & ((1LL << bit) - 1), bit - 1);
}

int main() {
    cin >> A >> B;
    cout << solve1(A, B, 60);
}