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;

trait Automaton { 
  type Alphabet;
  type State;
  fn init(&self) -> Self::State;
  fn next(&self, state: Self::State, a: Self::Alphabet) -> Self::State;
  fn accept(&self, state: Self::State) -> bool;
}
struct IntersectionAutomaton<X,Y>(X,Y) where X: Automaton, Y: Automaton;
impl<X,Y,A> Automaton for IntersectionAutomaton<X,Y> 
where X: Automaton<Alphabet=A>, 
      Y: Automaton<Alphabet=A>, 
      A: Copy {
  type Alphabet = A;
  type State = (X::State, Y::State);
  fn init(&self) -> Self::State {
    (self.0.init(), self.1.init())
  }
  fn next(&self, state: Self::State, a: Self::Alphabet) -> Self::State {
    (self.0.next(state.0, a), self.1.next(state.1, a))
  }
  fn accept(&self, state: Self::State) -> bool {
    self.0.accept(state.0) & self.1.accept(state.1)
  }
}
struct UnionAutomaton<X,Y>(X,Y) where X: Automaton, Y: Automaton;
impl<X,Y,A> Automaton for UnionAutomaton<X,Y> 
where X: Automaton<Alphabet=A>, 
      Y: Automaton<Alphabet=A>, 
      A: Copy {
  type Alphabet = A;
  type State = (X::State, Y::State);
  fn init(&self) -> Self::State {
    (self.0.init(), self.1.init())
  }
  fn next(&self, state: Self::State, a: Self::Alphabet) -> Self::State {
    (self.0.next(state.0, a), self.1.next(state.1, a))
  }
  fn accept(&self, state: Self::State) -> bool {
    self.0.accept(state.0) | self.1.accept(state.1)
  }
}
struct NegateAutomaton<X>(X) where X: Automaton;
impl<X: Automaton> Automaton for NegateAutomaton<X> {
  type Alphabet = X::Alphabet;
  type State = X::State;
  fn init(&self) -> Self::State {
    self.0.init()
  }
  fn next(&self, state: Self::State, a: Self::Alphabet) -> Self::State {
    self.0.next(state, a)
  }
  fn accept(&self, state: Self::State) -> bool {
    !self.0.accept(state) 
  }
}
struct ProductAutomaton<X,Y>(X,Y) where X: Automaton, Y: Automaton;
impl<X: Automaton,Y: Automaton> Automaton for ProductAutomaton<X,Y> {
  type Alphabet = (X::Alphabet, Y::Alphabet);
  type State = (X::State, Y::State);
  fn init(&self) -> Self::State {
    (self.0.init(), self.1.init())
  }
  fn next(&self, state: Self::State, a: Self::Alphabet) -> Self::State {
    (self.0.next(state.0, a.0), self.1.next(state.1, a.1))
  }
  fn accept(&self, state: Self::State) -> bool {
    self.0.accept(state.0) & self.1.accept(state.1)
  }
}

use std::hash::Hash;
fn count<X: Automaton>(dfa: &X, n: usize, alphabet: &[X::Alphabet]) -> usize 
where X::State: Copy + Eq + Hash, X::Alphabet: Copy {
  let init = dfa.init();
  let mut curr = std::collections::HashMap::new();
  curr.insert(init, 1);
  for _ in 0..n {
    let mut next = std::collections::HashMap::new();
    for (&state, value) in &curr {
      for &a in alphabet {
        *next.entry(dfa.next(state, a)).or_insert(0) += value;
        *next.entry(dfa.next(state, a)).or_insert(0) %= 1_000_000_007;
      }
    }
    curr = next;
  }
  let mut total = 0;
  for (&state, value) in &curr {
    if dfa.accept(state) {
      total += value;
      total %= 1_000_000_007;
    }
  }
  total
}


struct ContainChar(char);
impl Automaton for ContainChar {
  type Alphabet = char;
  type State = bool;
  fn init(&self) -> Self::State { false }
  fn next(&self, state: Self::State, a: Self::Alphabet) -> Self::State {
    state | (a == self.0)
  }
  fn accept(&self, state: Self::State) -> bool {
    state
  }
}

struct LessThanEqualAutomaton(Vec<char>);
impl Automaton for LessThanEqualAutomaton {
  type State = Result<(usize, bool),()>;
  type Alphabet = char;
  fn init(&self) -> Self::State { 
    Ok((0, true))
  }
  fn next(&self, s: Self::State, a: Self::Alphabet) -> Self::State {
    if let Ok((i, tight)) = s {
      if i >= self.0.len() { return Err(()); }
      if tight {
        if a  > self.0[i] { return Err(()); }
        if a == self.0[i] { return Ok((i+1, true)); }
      }
      return Ok((i+1, false));
    } else { Err(()) }
  }
  fn accept(&self, s: Self::State) -> bool {
    return s.is_ok();
  }
}

struct SameMSB;
impl Automaton for SameMSB {
  type Alphabet = (char, char);
  type State = usize;
  fn init(&self) -> Self::State { 0 } // 0: not started; 1: same msb, 2: not same msb
  fn next(&self, state: Self::State, a: Self::Alphabet) -> Self::State {
    match (state, a) {
      (0, ('0', '0')) => 0,
      (0, ('1', '1')) => 1,
      (0, _) => 2,
      (s, _) => s,
    }
  }
  fn accept(&self, state: Self::State) -> bool {
    state <= 1
  }
}
struct Between;
impl Automaton for Between {
  type Alphabet = (char, char);
  type State = bool;
  fn init(&self) -> Self::State { true }
  fn next(&self, state: Self::State, a: Self::Alphabet) -> Self::State {
    state & (a.0 <= a.1)
  }
  fn accept(&self, state: Self::State) -> bool {
    state
  }
}

fn main() {
  input!{
    l: usize,
    r: usize,
  }
  let r = format!("{:b}", r);
  let l = format!("{:0width$b}", l-1, width = r.len());

  let at_least_l = {
    let num: Vec<char> = l.chars().collect();
    NegateAutomaton(LessThanEqualAutomaton(num))
  };
  let at_most_r = {
    let num: Vec<char> = r.chars().collect();
    LessThanEqualAutomaton(num)
  };
  let dfa = IntersectionAutomaton(ProductAutomaton(at_least_l, at_most_r), 
                                  IntersectionAutomaton(SameMSB, Between));

  let alphabet = vec![('0','0'), ('0','1'), ('1','0'), ('1','1')];
  println!("{}", count(&dfa, r.len(), &alphabet));
}