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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

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

public class Main {
	private static void solve(){
	//Implement solution here.
		int x = ni();
		int y = ni();
		mod = 1000000007;		      
		int a = 2 * y - x;
		int b = 2 * x - y;
		if(a % 3 != 0 || a < 0 || b % 3 != 0 || b < 0) {
			System.out.println(0);
			return;
		}		
		a = (2 * y - x)/3;
		b = (2 * x - y)/3;
		System.out.println(a);
		System.out.println(combinationMod(a+b, a));
	}
	
	static long combination(int n, int k) {
		double out = 1;
		for(int i = 0; i < k; i++) {
			out = (out * (double)(n - i) / (k - i));
		}
		return (long)out;
	}
	
	static long combinationMod(int n, int k) {
        if (k == 0)return 1; 
        long[] fac = new long[n + 1];
        fac[0] = 1; 
        for (int i = 1 ;i <= n; i++) {
        	fac[i] = modMul(fac[i - 1], (long)i); 
        }            
        return modMul(modMul(fac[n],modInverse(fac[k])),modInverse(fac[n - k]));
    }
	
	static int mod;
	static long modSum(long a, long b) {
		return (a % mod + b % mod) % mod;
	}
	static long modMul(long a, long b) {
		return ((a % mod) * (b % mod)) % mod;
	}
	
	static long modInverse(long n) {
        return power(n, mod-2);
    }
	
	static long power(long a, long b) {
        long result = 1;
        a = a % mod;
        while (b > 0) {
            if (b % 2 == 1)
            	result = (result * a) % mod;
            b = b >> 1;
            a = (a * a) % mod;
        }
        return result;
    }

    //Switch input source (stdin/file)
	private static BufferedReader br  = new BufferedReader(new InputStreamReader(System.in));
	public static void main(String[] args){
        String debugDataPath = System.getenv("DD_PATH");        
        if(debugDataPath != null){
            try{
            	br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(debugDataPath))));
            }catch(Exception e){
                throw new RuntimeException(e);
            }
        }
        solve();
	}

    //Input read utility
	private static StringTokenizer tokenizer = null;
	public static String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}
    //Get next single int
	private static int ni() {
		return Integer.parseInt(next());
	}
    //Get next single long
	private static long nl() {
		return Long.parseLong(next());
	}
    //Get next single double
	private static double nd() {
		return Double.parseDouble(next());
	}
    //Get next int array from one line
	private static int[] na(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = ni();
		return a;
	}
	//Get next char array from one line
	private static char[] ns() {
		return next().toCharArray();
	}
    //Get next long array from one line
	private static long[] nal(int n) {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = nl();
		return a;
	}
}