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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder2 {
	class Program {
		static void Main(string[] args) {


			var inputArray = retreiveLongArray();
			var inputCount = 2;

			// 不正な入力は処理しない
			if (inputArray.Length != inputCount) {
				Console.WriteLine("invalid input");
				return;
			}
			var a = inputArray[0];
			var b = inputArray[1];

			//X>=Yとしておく
			var X = Math.Max(a, b);
			var Y = Math.Min(a, b);

			//X方向に何回、Y方向に何回とぶのか調べる

			long xCount = 0;
			long yCount = 0;

			while (Y*2 > X) {
				yCount++;
				Y = Y - 2;
				X = X - 1;
			}

			if(Y*2 != X) {
				Console.WriteLine(0);
				return;
			}
			xCount = Y;

			//(xCount+yCount)Combination(yCount)が答えになる

			long divide = 1000000000 + 7;
			var sum = xCount + yCount;
			var c = yCount;

			var result = BigMod.Combination(sum, c);

			//答えの出力
			Console.WriteLine(result);

		}

		/// <summary>
		/// スペースで区切られた文字列配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static string[] retreiveStringArray() {
			var inputArray = Console.ReadLine().Split(' ');
			return inputArray;
		}

		/// <summary>
		/// スペースで区切られた整数配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static long[] retreiveLongArray() {
			var inputArray = Console.ReadLine().Split(' ');
			var longArray = inputArray.Select(s => long.Parse(s));
			return longArray.ToArray();
		}
	}

	public static class BigMod {

		private static long _mod = 1000000000 + 7;

		/// <summary>
		/// x^n のmod　を繰り返し二乗法で求める
		/// </summary>
		/// <param name="x"></param>
		/// <param name="n"></param>
		/// <returns></returns>
		private static long repeatPow(long x,long n) {
			if (n == 0) {
				return 1;
			}
			if (n == 1) {
				return x;
			}

			if (n % 2 == 1) {
				var a = repeatPow(x, n / 2);
				var b = a*x % _mod;
				return (a * b) % _mod;
			}
			else {
				var a = repeatPow(x, n / 2);
				return (a * a) % _mod;
			}
		}

		/// <summary>
		/// nこからrこを選ぶ組み合わせの数
		/// </summary>
		/// <param name="n"></param>
		/// <param name="r"></param>
		/// <returns></returns>
		public static long Combination(long n,long r) {

			long bunshi = 1;
			for (int i = 1; i <= r; i++) {
				bunshi =  bunshi *(n - r + i) % _mod;
			}

			long bunbo = 1;
			for (int i = 1; i <= r; i++) {
				bunbo = bunbo * (i) % _mod;
			}
			//逆元を使って、組み合わせの数を求める	
			return (bunshi * repeatPow(bunbo, _mod - 2)) % _mod;

		}

	}

}