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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#define rep(i,n,m) for(int i = (n); i <(m); i++)
#define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--)
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const int MAX_X = 1000005;

ll power_mod(ll n, ll m)
{
    ll res = 1;
    ll base = n;
    while (m > 0)
    {
        if (m&1) res = (res * base) % MOD;
        base = (base * base) % MOD;
        m >>= 1;
    }
    return res;
}

ll inv_mod(ll n)
{
    return power_mod(n, MOD-2);
}

ll factorial[MAX_X];
void init_fact()
{
    factorial[0] = 1;
    rep(i, 1, MAX_X)
    {
        factorial[i] = factorial[i-1]*i;
        factorial[i] %= MOD;
    }
}

ll comb(ll n, ll m)
{
    ll res = 1;
    res *= factorial[n];
    res %= MOD;
    res *= inv_mod(factorial[n-m]);
    res %= MOD;
    res *= inv_mod(factorial[m]);
    res %= MOD;
    return res;
}

int main()
{
    init_fact();

    ll x, y;
    cin >> x >> y;

    ll res = 0;
    rep(a, 0, x+1)
    {
        ll rx = x-a;
        ll b = y-2*a;

        // cout << a << ' ' << b << ' ' << rx << endl;

        if (b < 0) continue;
        if (b*2 != rx) continue;

        res += comb(a+b, a);
        res %= MOD;
    }
    cout << res << endl;
    return 0;
}