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++11 (GCC 4.8.1) (judge verdict: Accepted)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <stack>
#include <set>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
#define all(v) v.begin(), v.end()

const int N = 1234567;

ll mpow(ll a, ll b){
	if(b==0) return 1;
	else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;}
	else return mpow(a,b-1) * a % MOD;
}
ll gcd(ll a, ll b){
	if(b==0) return a;
	else return gcd(b, a%b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n){
	if(kaijo_memo.size() > n) return kaijo_memo[n];
	if(kaijo_memo.size() == 0) kaijo_memo.push_back(1);
	while(kaijo_memo.size() <= n) kaijo_memo.push_back(kaijo_memo[kaijo_memo.size()-1] * kaijo_memo.size() % MOD);
	return kaijo_memo[n];
}
ll nCr(ll n, ll r){
	if(n < r || r < 0) return 0;
	ll ret = 1;
	ret *= kaijo(n); ret %= MOD;
	ret *= mpow(kaijo(r), MOD-2); ret %= MOD;
	ret *= mpow(kaijo(n-r), MOD-2); ret %= MOD;
	return ret;
}

string nisin(ll r){
	string ret = "";
	while(r > 0){
		if(r % 2 == 0){
			ret = "0" + ret;
		}else{
			ret = "1" + ret;
		}
		r /= 2;
	}
	return ret;
}

int main(void){
	ll l_,r_;
	cin>>l_>>r_;
	string l = nisin(l_);
	string r = nisin(r_);

	int fl = r.size() - l.size();
	int fr = 0;
	while(l.size() < r.size()){
		l = "0" + l;
	}
	/*cout<<"l: "<<l<<endl;
	cout<<"r: "<<r<<endl;
	cout<<fl<<endl;*/

	ll dp[4][2];
	rep(i,4){
		rep(j,2){
			dp[i][j] = 0;
		}
	}
	rep(i,r.size()){
		rep(j,4){
			dp[j][1] = 0;
		}
		if(fl == i && fr == i){
			dp[3][1] = 1;
		}else if(fl == i && fr != i){
			dp[2][1] = 1;
		}else if(fl != i && fr == i){
			dp[1][1] = 1;
		}else if(i < fl){
			dp[0][1] = 1;
		}

		dp[0][1] += dp[0][0] * 3;
		if(r[i] == '1'){
			dp[0][1] += dp[1][0];
		}
		if(l[i] == '0'){
			dp[0][1] += dp[2][0];
		}

		if(r[i] == '0'){
			dp[1][1] += dp[1][0];
		}
		if(r[i] == '1'){
			dp[1][1] += dp[1][0] * 2;
		}
		if(l[i] == '0' && r[i] == '1'){
			dp[1][1] += dp[3][0];
		}

		if(l[i] == '0'){
			dp[2][1] += dp[2][0] * 2;
		}
		if(l[i] == '1'){
			dp[2][1] += dp[2][0];
		}
		if(l[i] == '0' && r[i] == '1'){
			dp[2][1] += dp[3][0];
		}

		if(l[i] == '0' || r[i] == '1'){
			dp[3][1] += dp[3][0];
		}

		rep(j,4){
			dp[j][0] = dp[j][1] % MOD;
			//cout<<dp[j][0]<<" ";
		}
		//cout<<endl;
	}
	ll ans = 0;
	rep(i,4){
		ans += dp[i][0];
	}
	ans %= MOD;
	cout<<ans<<endl;
	return 0;
}