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 Rust (1.42.0) (judge verdict: Accepted)

use proconio::input;
use std::collections::HashMap;

type State = (usize,bool,bool,bool);
const MOD:i64 = 1_000_000_007;

enum Resp {
    Bad,
    Fin,
    Nex(State)
}
use Resp::Bad;
use Resp::Fin;
use Resp::Nex;

fn trans(st:State,x:usize,y:usize,l:i64,r:i64) -> Resp{
    let x = x as i64;
    let y = y as i64;
    if y<x {
        return Bad;
    }
    let (p,yx,ry,xl) = st;
    if !yx && y>x {
        return Bad;
    }
    if !ry && ((r>>p)&1)<y {
        return Bad;
    }
    if !xl && ((l>>p)&1)>x {
        return Bad;
    }
    if p==0 {
        return Fin;
    }
    let nex_yx = x==1 || yx;
    let nex_ry = ((r>>p)&1)>y || ry;
    let nex_xl = ((l>>p)&1)<x || xl;
    Nex((p-1,nex_yx,nex_ry,nex_xl))
}
fn calc(st:State,mut dp:&mut HashMap<State,i64>,l:i64,r:i64) -> i64{
    if dp.contains_key(&st) {
        return dp[&st];
    }
    let mut ans = 0;
    for i in 0..2 {
        for j in 0..2 {
            let ret = trans(st,i,j,l,r);
            ans +=  match ret {
                        Bad => 0,
                        Fin => 1,
                        Nex(nexst) => calc(nexst,&mut dp,l,r)
                    };
            ans %= MOD;
        }
    }
    dp.insert(st,ans);
    // println!("{:?} {}",st,ans);
    ans
}
fn main() {
    input! {
        l:i64,r:i64
    };

    let mut dp = HashMap::new();
    let st:State = (60,false,false,false);
    
    let ans = calc(st,&mut dp,l,r);
    println!("{}",ans);
}