Problem:
Score : 600 points

Problem Statement

You are given a positive integer N.
Find the number of the pairs of integers u and v (0≦u,v≦N) such that there exist two non-negative integers a and b satisfying a xor b=u and a+b=v.
Here, xor denotes the bitwise exclusive OR.
Since it can be extremely large, compute the answer modulo 10^9+7.

Constraints

1≦N≦10^{18}

Input

The input is given from Standard Input in the following format:

N

Output

Print the number of the possible pairs of integers u and v, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

5

The five possible pairs of u and v are:

u=0,v=0 (Let a=0,b=0, then 0 xor 0=0, 0+0=0.)

u=0,v=2 (Let a=1,b=1, then 1 xor 1=0, 1+1=2.）

u=1,v=1 (Let a=1,b=0, then 1 xor 0=1, 1+0=1.）

u=2,v=2 (Let a=2,b=0, then 2 xor 0=2, 2+0=2.）

u=3,v=3 (Let a=3,b=0, then 3 xor 0=3, 3+0=3.）

Sample Input 2

1422

Sample Output 2

52277

Sample Input 3

1000000000000000000

Sample Output 3

787014179

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::collections::{BTreeMap, BTreeSet};
const MOD: usize = 1e9 as usize + 7;
fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let n: i64 = sc.read();
    let mut memo = BTreeMap::new();
    println!("{}", solve(n, n, &mut memo));
}

fn naive(n: i64) -> usize {
    let mut set = BTreeSet::new();

    for a in 0..(n + 1) {
        for b in 0..(n + 1) {
            let u = a ^ b;
            let v = a + b;
            if u <= n && v <= n {
                set.insert((u, v));
            }
        }
    }
    println!("{:?}", set);
    set.len()
}

fn solve(sum: i64, xor: i64, memo: &mut BTreeMap<(i64, i64), usize>) -> usize {
    match memo.get(&(sum, xor)) {
        Some(&ans) => ans,
        None => {
            if sum == 0 {
                return 1;
            }
            let mut result = 0;

            // odd & odd
            if sum >= 2 {
                result += solve((sum - 2) >> 1, xor >> 1, memo);
            }

            // odd & even
            result += solve((sum - 1) >> 1, xor >> 1, memo);

            // even & even
            result += solve(sum >> 1, xor >> 1, memo);

            result %= MOD;
            memo.insert((sum, xor), result);
            result
        }
    }
}

pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}