Problem:
Score : 400 points

Problem Statement

There is a knight - the chess piece - at the origin (0, 0) of a two-dimensional grid.

When the knight is at the square (i, j), it can be moved to either (i+1,j+2) or (i+2, j+1).

In how many ways can the knight reach the square (X, Y)?

Find the number of ways modulo 10^9 + 7.

Constraints

1 \leq X \leq 10^6

1 \leq Y \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the number of ways for the knight to reach (X, Y) from (0, 0), modulo 10^9 + 7.

Sample Input 1

3 3

Sample Output 1

2

There are two ways: (0,0) \to (1,2) \to (3,3) and (0,0) \to (2,1) \to (3,3).

Sample Input 2

2 2

Sample Output 2

0

The knight cannot reach (2,2).

Sample Input 3

999999 999999

Sample Output 3

151840682

Print the number of ways modulo 10^9 + 7.

Solution in Kotlin (1.0.0) (judge verdict: Accepted)

import java.io.*
import java.util.*

// 二次元グリッドの原点 ( 0 , 0) にチェスのナイトの駒があります。
// ナイトの駒はマス ( i , j) にあるとき ( i + 1 , j + 2) か ( i + 2 , j + 1) のどちらかのマスにのみ動かすことができます。
// ナイトの駒をマス ( X , Y) まで移動させる方法は何通りありますか？

const val MOD = 1000000007L
// (a, 2a) + (2b, b) = (X, Y)を満たすa, bの組み合わせを求め、a+bCaを計算
fun solve(X: Int, Y: Int){
    for (a in 0..X){
        // 2b = X - a
        // b = Y - 2a
        if ((X - a) % 2 == 0 && (X - a) / 2 == Y - 2*a){
            val b = Y - 2 * a
            println(modBinomial(a + b, b, MOD))
            return
        }
    }
    println(0)
    return
}


fun main(args: Array<String>) {
    fun StringArray(size: Int, init: (Int) -> String = { "\u0000" }): Array<String> {
        return Array<String>(size, init)
    }
    class Scanner(stream: InputStream) {
        private val reader = BufferedInputStream(stream).bufferedReader()
        private var st: StringTokenizer? = null
        fun next(): String {
            while (!(st?.hasMoreTokens() ?: false)) st = StringTokenizer(reader.readLine())
            return st!!.nextToken()
        }
    }
    val sc = Scanner(System.`in`)
    val X = sc.next().toInt()
    val Y = sc.next().toInt()
    solve(X, Y)
}


tailrec fun gcd(x: Int, y: Int): Int = if (y == 0) x else gcd(y, x % y)
tailrec fun gcd(x: Long, y: Long): Long = if (y == 0L) x else gcd(y, x % y)

fun Int.pow(x: Double): Double = Math.pow(this.toDouble(), x)
fun Long.pow(x: Double): Double = Math.pow(this.toDouble(), x)

fun Long.modPow(x: Long, mod: Long): Long {
    // a ^ (2^(exp)) % mod
    tailrec fun binModPow(a: Long, exp: Long): Long = when (exp) {
        0L -> a % mod
        else -> binModPow(a * a % mod, exp - 1)
    }
    return (0..63).asSequence()
            .map { (x shr it) and 1 }
            .withIndex()
            .filter { it.value > 0 }
            .map { binModPow(this, it.index.toLong()) }
            .reduce { a, b -> a * b % mod }
}

fun Long.modInv(mod: Long): Long = this.modPow(mod - 2, mod)

fun modBinomial(n: Int, k: Int, mod: Long): Long = when(k){
    // n(n-1)...(n-k+1) / k!
    0 -> 1L
    else -> {
        val numer = (n downTo n - k + 1).map{ it.toLong() }.reduce{ a, b -> (a * b) % mod }
        val denom = (1..k).map{ it.toLong() }.reduce{ a, b -> (a * b) % mod }
        (numer * denom.modInv(mod)) % mod
    }
}