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 Kotlin (1.0.0) (judge verdict: Accepted)

import java.util.*
import kotlin.*

import java.io.InputStream
import java.lang.NumberFormatException
import java.lang.StringBuilder


fun main(args: Array<String>) {
    Program().solve()
}

class Program {
    fun solve() {
        val sc = FastScanner()
        val N = sc.nextLong()

        /*
        v <= Nなので a <= N && b <= N
        また u <= v

        大小は v <= Nのみ考えれば良い

        Nが大きいので多分 O(log N)

        bitDP

        下から
        vのbitが立ってる

        a,b片方立てる u += 2^i v += 2^i

        立てない u +=0 v+=0
        両方立てる u += 2^(i+1) v += 0

         */
        var tmp = N

        // iBit目 立てるか? 繰り上がりあるか?
        val dp = Array(61) {
            Array(2) { Array(2) { ModInt(0) } }
        }

        // n以下
        val dp2 = Array(61) {
            Array(2) { Array(2) { ModInt(0) } }
        }

        for (i in 0..60) {
            if (i == 0) {
                // 両方立てない
                dp[0][0][0] = ModInt(1)

                // 片方立てる
                dp[0][1][0] = ModInt(1)

                // 両方立てる
                dp[0][0][1] = ModInt(1)

                if (tmp % 2 == 1L) {
                    // 片方立てる
                    dp2[0][1][0] = ModInt(1)
                }

                // 両方立てない
                dp2[0][0][0] = ModInt(1)

                // 両方立てる
                dp2[0][0][1] = ModInt(1)
            } else {
                // 両方立てない 繰り上がり無し
                dp[i][0][0] += dp[i - 1][0][0] + dp[i - 1][1][0]

                // 片方立てる 繰り上がり無し
                dp[i][1][0] += dp[i - 1][0][0] + dp[i - 1][1][0]

                // 両方立てる 繰り上がり無し
                dp[i][0][1] += dp[i - 1][0][0] + dp[i - 1][1][0]

                //両方立てない 繰り上がり
                dp[i][1][0] += dp[i - 1][0][1] + dp[i - 1][1][1]

                // 片方立てる 繰り上がり
                dp[i][0][1] += dp[i - 1][0][1] + dp[i - 1][1][1]

                // 両方立てる 繰り上がり
                dp[i][1][1] += dp[i - 1][0][1] + dp[i - 1][1][1]

                if (tmp % 2 == 0L) {
                    // 両方立てない 繰り上がり無し
                    dp2[i][0][0] += dp2[i - 1][0][0] + dp2[i - 1][1][0]

                    // 両方立てる 繰り上がり無し
                    dp2[i][0][1] += dp2[i - 1][0][0] + dp2[i - 1][1][0]

                    // 片方立てる 繰り上がり
                    dp2[i][0][1] += dp2[i - 1][0][1] + dp2[i - 1][1][1]
                } else {
                    // 両方立てない 繰り上がり無し
                    dp2[i][0][0] += dp[i-1][0][0] + dp[i-1][1][0]

                    // 両方立てる 繰り上がり無し
                    dp2[i][0][1] += dp[i-1][0][0] + dp[i-1][1][0]

                    // 片方立てる 繰り上がり
                    dp2[i][0][1] += dp[i-1][0][1] + dp[i-1][1][1]

                    // 両方立てない 繰り上がり
                    dp2[i][1][0] += dp2[i-1][0][1] + dp2[i-1][1][1]

                    // 片方立てる 繰り上がりなし
                    dp2[i][1][0] += dp2[i-1][0][0] + dp2[i-1][1][0]

                    // 両方立てる 繰り上がり
                    dp2[i][1][1] += dp2[i-1][0][1] + dp2[i-1][1][1]
                }
            }

            tmp /= 2
        }

        println(dp2[60][0][0])
    }
}

class ModInt(_num: Number) : Number() {
    companion object {
        const val mod: Long = 1000000007L
    }

    private val num: Long = if (_num.toLong() < 0) (_num.toLong() % mod) + mod else _num.toLong() % mod
    operator fun plus(right: Number) = ModInt(num + (right.toLong() % mod))
    operator fun minus(right: Number) = ModInt(num - (right.toLong() % mod))
    operator fun times(right: Number) = ModInt(num * (right.toLong() % mod))

    // modが素数の時のみ
    operator fun div(right: Number) = this * ModInt(right).inverse()

    fun power(n: Number): ModInt {
        var a = this
        var result = ModInt(1)
        var tmp = n.toLong()
        while (tmp > 0) {
            if (tmp % 2 == 1L) {
                result *= a
            }
            tmp /= 2
            a *= a
        }
        return result
    }

    // modが素数のときのみ 逆元
    fun inverse(): ModInt {
        return power(mod - 2L)
    }

    override fun toString() = num.toString()
    override fun toInt() = num.toInt()
    override fun toLong() = num
    override fun toByte() = num.toByte()
    override fun toChar() = num.toChar()
    override fun toDouble() = num.toDouble()
    override fun toFloat() = num.toFloat()
    override fun toShort() = num.toShort()
}

class FastScanner {
    companion object {
        val input: InputStream = System.`in`
        val buffer = ByteArray(1024) { 0 }
        fun isPrintableChar(c: Int): Boolean = c in 33..126
    }

    var ptr = 0
    var buflen = 0
    private fun hasNextByte(): Boolean {
        if (ptr < buflen) {
            return true
        } else {
            ptr = 0
            buflen = input.read(buffer)
            if (buflen <= 0) {
                return false
            }
        }
        return true
    }

    private fun readByte(): Int = if (hasNextByte()) buffer[ptr++].toInt() else -1

    private fun skipUnprintable() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr].toInt())) ptr++
    }

    fun hasNext(): Boolean {
        skipUnprintable()
        return hasNextByte()
    }

    fun next(): String {
        if (!hasNext()) throw NoSuchElementException()
        val sb = StringBuilder()
        var b = readByte()
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b)
            b = readByte()
        }
        return sb.toString()
    }

    fun nextInt(): Int {
        if (!hasNext()) throw NoSuchElementException()
        var n = 0
        var b = readByte()
        // '-' = 45
        val minus = b == 45
        if (minus) {
            b = readByte()
        }

        // '0' = 48 '9' = 57
        if (b !in 48..57) {
            throw NumberFormatException()
        }

        while (true) {
            if (b in 48..57) {
                n *= 10
                n += b - 48
            } else if (b == -1 || !isPrintableChar(b)) {
                return if (minus) -n else n
            } else {
                throw NumberFormatException()
            }
            b = readByte()
        }
    }

    fun nextLong(): Long {
        if (!hasNext()) throw NoSuchElementException()
        var n = 0L
        var b = readByte()
        // '-' = 45
        val minus = b == 45
        if (minus) {
            b = readByte()
        }

        // '0' = 48 '9' = 57
        if (b !in 48..57) {
            throw NumberFormatException()
        }

        while (true) {
            if (b in 48..57) {
                n *= 10
                n += b - 48
            } else if (b == -1 || !isPrintableChar(b)) {
                return if (minus) -n else n
            } else {
                throw NumberFormatException()
            }
            b = readByte()
        }
    }

    fun nextDouble(): Double = next().toDouble()
}