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: Accepted)

import java.util.*;

class Main {
	Scanner sc;
	int M = 1_000_000_000+7;
	void calc() {
		sc = new Scanner(System.in);
		int X = sc.nextInt();
		int Y = sc.nextInt();
		if ( (X+Y)%3 != 0 ) {
			System.out.println(0);
			return;
		}
		int N = (X+Y)/3;
		int a = X-N;
		int b = Y-N;
		if (a < 0 || b < 0) {
			System.out.println(0);
			return;
		}
		
		// ans = nCr(a+b, a)
		if (a < b) {
			int t = a; a = b; b = t;
		}
		long u = 1;
		for (int i = a+b; i > a; i--) u = (u * i) % M;
		long l = 1;
		for (int i = b; i >= 1; i--) l = (l * i) % M;
		
		int inv = extgcd((int)l, M)[1];
		
		System.out.println((u * inv)%M);
	}

	/**
	 * 拡張ユークリッド互除法
	 * ax+by=gcd(a,b) となる gcd,x,y を求める。
	 */
	static int[] extgcd(int a, int b) {
		int aa = a, bb = b;
		int x0 = 1, x1 = 0;
		int y0 = 0, y1 = 1;
	
		while (b != 0) {
			int q = a / b;
			int r = a % b;
			int x2 = x0 - q * x1;
			int y2 = y0 - q * y1;
	
			a = b; b = r;
			x0 = x1; x1 = x2;
			y0 = y1; y1 = y2;
		}
		if (bb > 0 && x0 < 0) {
			int n = -x0/bb;
			if (-x0%bb != 0) n++;
			x0 += n*bb; y0 -= n*aa;
		}
	    return new int[]{a, x0, y0};
	}
	
	public static void main(String[] args) {
		new Main().calc();
	}
}