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 JavaScript (node.js v5.12) (judge verdict: Time Limit Exceeded)

"use strict"

const arg_str = require('fs').readFileSync('/dev/stdin', 'UTF-8')
const args = arg_str.split(' ')
const X = args[0]
const Y = args[1]

let dp = []
dp.push({x:1,y:2,v:1})
dp.push({x:2,y:1,v:1})

for(let i=0;i<=X;i++){
    for(let j=0;j<=Y;j++){
        const current_dp = dp.find(x => x.x == i && x.y == j)
        if(!current_dp){
            let tmp_1 = 0
            if(i - 1 >= 0 && j - 2 >= 0){
                const tmp_dp_1 = dp.find(x => x.x == i - 1 && x.y == j - 2)
                if(tmp_dp_1){
                  tmp_1 = tmp_dp_1.v
                }
            }
            let tmp_2 = 0
            if(i - 1 >= 0 && j - 2 >= 0){
                const tmp_dp_2 = dp.find(x => x.x == i - 2 && x.y == j - 1)
                if(tmp_dp_2){
                  tmp_2 = tmp_dp_2.v
                }
            }
            dp.push({x:i,y:j,v:tmp_1 + tmp_2})
        }
    }
}

const result = dp.find(x => x.x == X && x.y == Y)
if(result){
    console.log(result.v % 1000000007)
}else{
    console.log(0)
}