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 (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

def enumfif(n, mod):
    f = [0] * (n+1)
    invf = [0] * (n+1)
    f[0] = 1
    for i in range(1, n+1):
        f[i] = f[i-1] * i % mod
    a = f[n]
    b = mod
    p, q = 1, 0
    while b > 0:
        c = a//b
        d = a
        a = b
        b = d % b
        d = p
        p = q
        q = d - c * q
    invf[n] = p + mod if p < 0 else p
    for i in range(n-1,-1,-1):
        invf[i] = invf[i+1] * (i+1) % mod
    return [f, invf]

x, y = na()
mod = 1000000007
if (x+y) % 3 != 0:
    print(0)
else:
    
    
    a = (2*x-y)//3
    b = (2*y-x)//3
    if a < 0 or b < 0:
        print(0)
    else:
        fif = enumfif(1500005, mod)
        print(fif[0][a+b] * fif[1][a] * fif[1][b] % mod)

Solution (Python):

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

mod = 10**9+7

class Combination:
    def __init__(self,N):
        self.fac = [1]*(N+1)
        for i in range(1,N+1):
            self.fac[i] = (self.fac[i-1]*i)%mod
        self.invmod = [1]*(N+1)
        self.invmod[N] = pow(self.fac[N],mod-2,mod)
        for i in range(N,0,-1):
            self.invmod[i-1] = (self.invmod[i]*i)%mod

    def calc(self,n,k):
        return self.fac[n]*self.invmod[k]%mod *self.invmod[n-k] %mod

X,Y = inpl()
X,Y = min(X,Y), max(X,Y)

A = X
B = Y - X

A -= B

if A < 0 or A%3 != 0:
    print(0)
else:
    C = Combination(10**6+10)
    a = A//3
    b = A//3 + B
    print(C.calc(a+b,a))

Solution (Python):


def combination(n, r, mod=10**9+7):
    n1, r = n+1, min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n1-i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod-2, mod) % mod

X, Y = map(int, input().split())
if (X+Y) % 3 != 0 or not (X <= 2*Y and Y <= 2*X):
    print(0)
    exit()

n = (X+Y) // 3
r = Y - n

print(combination(n, r))

Solution (Python):

import sys
def input():
	return sys.stdin.readline().strip()


X, Y = list(map(int, input().split()))

if (X + Y) % 3 != 0 or X > 2*Y or Y > 2*X:
	print(0)
	exit()

times = (X + Y) // 3

times2 = X - times


def fact(n, mod=1000000007):
	ans = 1
	for i in range(n):
		ans *= i + 1
		if mod != 0 and ans >= mod:
			ans = ans % mod
	return ans


def mod_rev(x, mod):

	return pow(x, mod - 2, mod)



def comb(a, b, mod=1000000007):
	"""
	関数mod_rev, fact, powが必要
	"""
	if a < b or b < 0:
		return 0
	if a == b or b == 0:
		return 1
	a_fact = fact(a, mod)
	b_fact = fact(b, mod)
	a_b_fact = fact(a - b, mod)
	if mod != 0:
		return (a_fact * mod_rev(b_fact, mod) * mod_rev(a_b_fact, mod))%mod
	else:
		return a_fact//(b_fact*a_b_fact)

mod = 10**9 + 7

print(comb(times, times2, mod))

Solution (Python):


SIZE=1000000; MOD=10**9+7 

SIZE += 1
inv = [0]*SIZE  
fac = [0]*SIZE  
finv = [0]*SIZE 
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE):
    inv[i] = MOD - (MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): 
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0

def chofuku(ball,box): 
    return choose(box+ball-1,box)


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 



x,y = [int(i) for i in readline().split()]


xx = x-2*y
yy = (-2)*x+y

ng = 0

if xx%3 != 0: ng = 1
if yy%3 != 0: ng = 1

xx //= 3 
yy //= 3
xx *= -1
yy *= -1



if ng: print(0)
else: print(choose(xx+yy,xx)%MOD)