Problem:
Score : 600 points

Problem Statement

Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x.

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

1 \leq L \leq R \leq 10^{18}

Input

Input is given from Standard Input in the following format:

L R

Output

Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7.

Sample Input 1

2 3

Sample Output 1

3

Three pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).

Sample Input 2

10 100

Sample Output 2

604

Sample Input 3

1 1000000000000000000

Sample Output 3

68038601

Be sure to compute the number modulo 10^9 + 7.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=100005,INF=1<<30;

int main(){
    
    ll L,R;cin>>L>>R;
    
    ll dp[80][2][2][2];
    
    memset(dp,0,sizeof(dp));
    
    dp[0][1][1][1]=1;
    
    for(int i=0;i<62;i++){
        
        dp[i+1][0][0][0]+=3*dp[i][0][0][0];
        
        dp[i+1][0][0][0]+=dp[i][0][0][1];
        dp[i+1][0][0][1]+=dp[i][0][0][1];
        
        if(L&(1LL<<(61-i))){
            if(R&(1LL<<(61-i))){
                dp[i+1][1][0][0]+=dp[i][1][0][0];
                
                dp[i+1][1][0][0]+=dp[i][1][0][1];
                
                dp[i+1][0][0][0]+=dp[i][0][1][0];
                dp[i+1][0][1][0]+=2*dp[i][0][1][0];
                
                dp[i+1][0][0][1]+=dp[i][0][1][1];
                dp[i+1][0][1][0]+=dp[i][0][1][1];
                
                dp[i+1][1][1][0]+=dp[i][1][1][0];
                
                dp[i+1][1][1][0]+=dp[i][1][1][1];
                
            }else{
                dp[i+1][1][0][0]+=dp[i][1][0][0];
                
                dp[i+1][1][0][0]+=dp[i][1][0][1];
                
                dp[i+1][0][1][0]+=dp[i][0][1][0];
                
                dp[i+1][0][1][1]+=dp[i][0][1][1];
                
            }
        }else{
            if(R&(1LL<<(61-i))){
                
                dp[i+1][1][0][0]+=2*dp[i][1][0][0];
                dp[i+1][0][0][0]+=dp[i][1][0][0];
                
                dp[i+1][1][0][1]+=dp[i][1][0][1];
                dp[i+1][0][0][0]+=dp[i][1][0][1];
                
                dp[i+1][0][0][0]+=dp[i][0][1][0];
                dp[i+1][0][1][0]+=2*dp[i][0][1][0];
                
                dp[i+1][0][1][0]+=dp[i][0][1][1];
                dp[i+1][0][0][1]+=dp[i][0][1][1];
                
                dp[i+1][1][0][0]+=dp[i][1][1][0];
                dp[i+1][1][1][0]+=dp[i][1][1][0];
                dp[i+1][0][1][0]+=dp[i][1][1][0];
                
                dp[i+1][0][1][0]+=dp[i][1][1][1];
                dp[i+1][1][0][1]+=dp[i][1][1][1];
                
            }else{
                dp[i+1][1][0][0]+=2*dp[i][1][0][0];
                dp[i+1][0][0][0]+=dp[i][1][0][0];
                
                dp[i+1][0][0][0]+=dp[i][1][0][1];
                dp[i+1][1][0][1]+=dp[i][1][0][1];
                
                dp[i+1][0][1][0]+=dp[i][0][1][0];
                
                dp[i+1][0][1][1]+=dp[i][0][1][1];
                
                dp[i+1][1][1][0]+=dp[i][1][1][0];
                
                dp[i+1][1][1][1]+=dp[i][1][1][1];
            }
        }
        
        for(int j=0;j<2;j++){
            for(int k=0;k<2;k++){
                for(int l=0;l<2;l++){
                    dp[i+1][j][k][l]%=mod;
                }
            }
        }
    }
    
    ll ans=0;
    
    for(int j=0;j<2;j++){
        for(int k=0;k<2;k++){
            for(int l=0;l<2;l++){
                ans+=dp[62][j][k][l];
                ans%=mod;
            }
        }
    }
    
    cout<<ans<<endl;
}