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.*;
public class Main {
        static Scanner sc = new Scanner(System.in);
        static void myout(Object t){System.out.println(t);}
        static String getStr(){return sc.next();}
        static int getInt(){return sc.nextInt();}
        static Long getLong(){return sc.nextLong();}
        static boolean isNext(){return sc.hasNext();}
        public static void main(String[] args){
          int x = getInt();
          int y = getInt();
          int mod = 1000000007;
          
          if(x > y){
     if(x / 2 > y){
        myout(0);
       return;
     }else if(x / 2 == y){
        myout(1);
       return;
     }
  }else if(y > x){
      if(y / 2 > x){
         myout(0);
        return;
         }else if(y / 2 == x){
          myout(1);
        return;        
       }
  }else if((x+y) % 3 != 0){
            myout(0);
        return;
          }
	int tmpX = x;
	int tmpY = y;
	//原点に向かうべく、境界線のところ（y = 2x,またはy = x/2の座標）に帰れるか。帰れなかったら0
	int xLineCount = 0;
  int yLineCount = 0;
  while(true){
       tmpX -= 2;
      tmpY -= 1;
    yLineCount++;
      if(tmpY / 2 == tmpX){
         break;
      }else if(tmpX < 1 || tmpY < 1){
               myout(0);
        return;
       }
    }
   tmpX = x;
	 tmpY = y;
	while(true){
       tmpX -= 1;
      tmpY -= 2;
    xLineCount++;
      if(tmpX / 2 == tmpY){
         break;
      }else if(tmpX < 1 || tmpY < 1){
               myout(0);
        return;
       }
    }
          
          
          myout(originComb(xLineCount+yLineCount,xLineCount,mod));
          //myout(groovyPow(x,n,mod));
          //myout(originPow(x,n,mod));
          //myout(Math.pow(x,n));
        }
        //便利メソッド追加枠ここから

static long originComb(int n, int k, int m){
  long[] factrial = new long[n+1];
  long[] inverse = new long[n+1];
  factrial[0] = 1;inverse[0] = 1;
  for(int i = 1; i <= n; i++){
    factrial[i] = (factrial[i-1] * i) % m;
    inverse[i] = originPow(factrial[i], m-2,m) % m;
  }
  return factrial[n] * inverse[k] % m * inverse[n-k] % m;
}
  
static long originPow(long x, int n){
  return originPow(x,n,1000000007);
}
static long originPow(long x, int n,int m) {
	long ans = 1;
	while (n > 0) {
		if ((n & 1) == 1)ans = ans * x % m;
		x = x * x % m;
		n >>= 1;
	}
	return ans;
}

  
        //便利メソッド追加枠ここまで
}