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 <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define ll long long 
#define pii pair<int,int>
#define pll pair<ll,ll>
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
#define ceil(a,b) ((a-1)/b+1)
#define srand() mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l,r) uniform_int_distribution<int>(l,r)(rng)
typedef unsigned long long ull;
const int inf=1039074182;
using namespace std;
ll dp[65][65][2];//¸ãÁËiÎ»£¬ÀïÃæÓÐj¸ö1£¬Ð¡ÓÚ£¨0£©Æ½£¨1£©µÄ·½°¸Êý¡£ 
ll dp2[65][2][2][2];
int a[65];
int b[65];
int n;

namespace combinatorics
{
	int *fac;
	int *ifac;
	int mod;
	inline int add(int a,int b)
	{
		return (a+b)%mod;
	}
	inline int minus(int a,int b)
	{
		return (a-b+mod)%mod;
	}
	inline int multiply(int a,int b)
	{
		return (1LL*a*b)%mod;
	}
	inline int fastpow(int basic,int x)
	{
		int res=1;
		while(x)
		{
			if(x&1)	res=multiply(res,basic);
			basic=multiply(basic,basic);
			x>>=1;
		}
		return res;
	}
	inline int inv(int x)
	{
		return fastpow(x,mod-2);
	}
	void init(int n,int tmod)
	{
		mod=tmod;
		fac=new int[n+5];
		ifac=new int[n+5];
		fac[0]=1;
		for(int i=1;i<=n;i++)
		{
			fac[i]=multiply(fac[i-1],i);
		}
		ifac[n]=inv(fac[n]);
		for(int i=n-1;i>=0;i--)
		{
			ifac[i]=multiply(ifac[i+1],i+1);
		}
	}
	inline int C(int n,int m)
	{
		return multiply(multiply(fac[n],ifac[m]),ifac[n-m]);
	}
	inline int Cat(int x)
	{
		return multiply(C(x*2,x),inv(x+1));
	}
};
using namespace combinatorics;

void solve(ll x)
{
	memset(dp,0,sizeof(dp));
	n=0;
	while(x)
	{
		a[n++]=x&1;
		x>>=1;
	}
	reverse(a,a+n);
	dp[0][0][1]=1;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<=i;j++)
		{
			if(a[i]==0)
			{
				dp[i+1][j][0]+=dp[i][j][0];
				dp[i+1][j][1]+=dp[i][j][1];
				dp[i+1][j+1][0]+=dp[i][j][0];
			}
			else
			{
				dp[i+1][j][0]+=dp[i][j][0];
				dp[i+1][j][0]+=dp[i][j][1];
				dp[i+1][j+1][1]+=dp[i][j][1];
				dp[i+1][j+1][0]+=dp[i][j][0];
			}
		}
	}
}

int main()
{
//	freopen("input.txt","r",stdin);
	ll l,r;
	cin>>l>>r;
	solve(r);
	init(5,1e9+7);
	int res=0;
//	cout<<dp[1][0][0]<<' '<<dp[1][1][1]<<endl;
//	cout<<dp[2][1][0]<<' '<<dp[2][1][1]<<endl;
	for(int i=1;i<=64;i++)
	{
		res+=multiply(fastpow(2,i-1),(dp[n][i][0]+dp[n][i][1])%combinatorics::mod);
		res%=mod;
	}
//	cout<<res<<endl;
//	solve(l-1);
//	for(int i=1;i<=64;i++)
//	{
//		res-=multiply(fastpow(2,i-1),(dp[n][i][0]+dp[n][i][1])%combinatorics::mod);
//		if(res<0) res+=mod;
//	}
	ll tl=l-1,tr=r;
	int l1=0,l2=0;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	while(tl)
	{
		a[l1++]=(tl&1);
		tl>>=1;
	}
	while(tr)
	{
		b[l2++]=(tr&1);
		tr>>=1;
	}
	reverse(a,a+l2);
	reverse(b,b+l2);
	dp2[0][1][1][0]=1;
//	for(int i=0;i<l2;i++)
//	{
//		cout<<a[i];
//	}
//	cout<<endl;
//	for(int i=0;i<l2;i++)
//	{
//		cout<<b[i];
//	}
//	cout<<endl;
	for(int i=0;i<l2;i++)
	{
		for(int ea=0;ea<2;ea++)
		{
			for(int eb=0;eb<2;eb++)
			{
				for(int ua=0;ua<2;ua++)
				{
					for(int ub=ua;ub<2;ub++)
					{
						for(int ff=0;ff<2;ff++)
						{
//							if(ua>a[i]) continue;
//							if(ub>b[i]) continue;
							if(ff==0 && ub==1 && ua==0) continue;
							int na,nb,nff;
							if(ea==0 || (ea==1 && ua<a[i]))
							{
								na=0;
							}
							else if(ea==1 && ua==a[i])
							{
								na=1;
							}
							else
							{
								continue;
							}
							if(eb==0 || (eb==1 && ub<b[i]))
							{
								nb=0;
							}
							else if(eb==1 && ub==b[i])
							{
								nb=1;
							}
							else
							{
								continue;
							}
							if(ff==1 || (ff==0 && ub==1))
							{
								nff=1;
							}
							else
							{
								nff=0;
							}
							dp2[i+1][na][nb][nff]+=dp2[i][ea][eb][ff];
							dp2[i+1][na][nb][nff]%=mod;
						}
					}
				}
			}
		}
	}
	int sub=0;
	for(int i=0;i<2;i++)
	{
		for(int j=0;j<2;j++)
		{
			sub+=dp2[l2][i][j][1];
			sub%=mod;
		}
	}
//	cout<<dp2[1][1][0][0]<<endl;
//	cout<<dp2[2][1][0][0]<<endl;
//	cout<<dp2[3][1][0][0]<<endl;
//	cout<<dp2[4][1][0][0]<<endl;
//	for(int i=0;i<=l2;i++)
//	{
//		for(int j=0;j<2;j++)
//		{
//			for(int k=0;k<2;k++)
//			{
//				for(int u=0;u<2;u++)
//				{
//					cout<<dp2[i][j][k][u]<<' ';
//				}
//				cout<<endl;
//			}
//			cout<<endl;
//		}
//		cout<<endl;
//	}
//	cout<<sub<<endl;
	cout<<((ll)res-(ll)sub+mod)%mod<<endl;
	return 0;
}