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)

/*
  "I knew I shouldn't have
   gotten out of bed today."
   - Squidward Tentacles
*/
#include<iostream>
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstring>
#include<climits>
#include<numeric>

#include<deque>
#include<queue>
#include<vector>
#include<map>
#include<unordered_map>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

#include<iomanip>
#include<fstream>
#include<chrono>
using namespace std;
using namespace __gnu_pbds;

#define int          long long
#define pii          pair<int,int>
#define vi           vector<int>
#define peque        priority_queue
#define pb           push_back
#define mp           make_pair
#define ff           first
#define ss           second
#define rep(i,s,e)   for(int i=s;i<=e;i++)
#define brep(i,s,e)  for(int i=s;i>=e;i--)
#define all(x)       x.begin(),x.end()
#define ball(x)      x.rbegin(),x.rend()
#define mem(x,y)     memset(x,y,sizeof(x))
#define oset         tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define VR46         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int MOD = (1e+9)+7;

int modexp(int base, int exp)
{
    int res = 1;
    while( exp>0 )
    {
        if( exp&(int)1 ) res *= base, res %= MOD;
        base *= base, base %= MOD, exp >>= (int)1;
    }
    return res;
}

int modinv(int a)
{
    return modexp( a,MOD-2 );
}

int fac[1000005];

int ferm(int n, int k) 
{
   if (k==0) return 1; 
  
    return (fac[n]* modinv(fac[k]) % MOD * modinv(fac[n-k]) % MOD) % MOD; 
} 

signed main()
{
    VR46 ofstream out("output.txt");
    // auto start = chrono::high_resolution_clock::now();

    // GANDU THODE TEST CASE BNAKE TEST KRLE
    int t = 1;
    // cin>>t;
    rep(test,1,t)
    {
        fac[0] = 1;
        rep(i,1,1000000)
        fac[i] = (fac[i-1]*i)%MOD;

        int x, y;
        cin>>x>>y;
        if( (x+y)%3 != 0 ) return cout<<"0\n", 0;

        int n = (x+y)/3;
        int xx = n, yy = 2*n;
        bool f = false;
        int idx = 0;
        while( xx<=2*n )
        {
            if( xx==x && yy==y )
            {
                f = true;
                break;
            }
            xx++, yy--, idx++;
        }
        if( !f ) return cout<<"0\n", 0;

        cout<< ferm( n,idx )%MOD <<"\n";
    }

    // auto stop = chrono::high_resolution_clock::now();
    // auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start); 
    // cout<<"Time taken by function: "<<duration.count()<<" milliseconds"<<endl;
}