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):
x, y = map(int, input().split())
if (x + y) % 3 != 0:
    print(0)
else:
    n = (x+y) // 3
    nx = x - n
    ny = y - n
    if (nx < 0 ) or (ny < 0):
        print(0)
    else:
        mod = 10 **9 +7
        fac = [1] * 1000000
        for i in range(1,n+1):
            fac[i] = fac[i-1] * i % mod
        print(fac[n] * pow(fac[nx], mod-2, mod) * pow(fac[ny], mod-2, mod) % mod)

Solution (Python):
X, Y = map(int, input().split())

mod = 10**9+7

def com(n, r):
    p, q = 1, 1
    for i in range(r):
        p = p*(n-i)%mod
        q = q*(r-i)%mod
    return (p*pow(q, mod-2, mod))%mod

if (X + Y)%3 != 0:
    print(0)

else:
    a = (2*Y-X)//3
    b = (X+Y)//3 - a
    if a < 0 or b < 0:
        print(0)
    else:
        print(com(a+b, a))

Solution (Python):

import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def lprint(l): print(*l, sep='\n')


X,Y = imn()


if (X+Y)%3 != 0:
    print(0)
    exit()

M = (2*Y-X)//3
N = (-2*M)+Y


MAX = 10**6
fact = [1,1]
factinv = [1,1]
inv = [0,1]
for i in range(2, MAX+1):
    fact.append((fact[-1]*i) % MOD)
    inv.append((-inv[MOD%i]*(MOD//i))%MOD)
    factinv.append((factinv[-1]*inv[-1])%MOD)


def cmb(n,r):
    if r < 0 or n < r:
        return 0
    r = min(r, n-r)
    return fact[n] * factinv[r] * factinv[n-r] % MOD 

print(cmb(M+N, M))

Solution (Python):
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    return [w[0],w[1]]

def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m

x, y = map(int, input().split())
if (x+y)%3 or y>2*x or x>2*y: print(0)
else:
    a, b = (2*x-y)//3, (2*y-x)//3
    m = 10**9+7
    res = 1
    for i in range(1,a+b+1):
        res = res*i%m
    for i in range(1,a+1):
        res = res*mod_inv(i,m)%m
    for i in range(1,b+1):
        res = res*mod_inv(i,m)%m
    print(res%m)

Solution (Python):

import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def lprint(l): print(*l, sep='\n')

X,Y = imn()

if (X+Y)%3 != 0:
    print(0)
    exit()

M = (2*Y-X)//3
N = (-2*M)+Y

MAX = 10**6
fact = [1,1]
factinv = [1,1]
inv = [0,1]
for i in range(2, MAX+1):
    fact.append((fact[-1]*i) % MOD)
    inv.append((-inv[MOD%i]*(MOD//i))%MOD)
    factinv.append((factinv[-1]*inv[-1])%MOD)

def cmb(n,r,p):
    if r < 0 or n < r:
        return 0
    r = min(r, n-r)
    return fact[n] * factinv[r] * factinv[n-r] % p
print(cmb(M+N, M, MOD))

Solution (Python):

import sys
MOD = 1000000007  

def combination(n: int, r:int, mod=MOD):
    n1 = n+1
    r = 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

def solve(X: int, Y: int):

    u = (2*Y - X)//3
    r = (X- u)//2

    if ((2*Y - X)%3!=0) or (r*u <0):
        print(0)
        return

    a = combination(u+r, u)
    print(a)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    Y = int(next(tokens))  
    solve(X, Y)

if __name__ == '__main__':
    main()

Solution (Python):
from sys import setrecursionlimit
setrecursionlimit(4100000)

x, y = map(int, input().split())

mod = 1000000007
f = 0
df = abs(x-y)     
tm = (x+y)//3   

if (x+y)%3 != 0:
    f = 1
elif tm < df:
    f = 1
else:
    pass

def mod_a_to_bth_power(a:int, b:int):
    if b%2 == 0:
        return (mod_a_to_bth_power(a, b//2)**2)%mod
    elif b == 1:
        return a%mod
    else:
        return ((mod_a_to_bth_power(a, b//2)**2)*a)%mod

def mod_inverse(a:int):
    return mod_a_to_bth_power(a, mod-2)

def mod_nCr(n:int, r:int):
    num, den = 1, 1 
    for i in range(r):
        num = (num*(n-i))%mod
        den = (den*(i+1))%mod
    return (num*mod_inverse(den))%mod

if f == 1:
    print(0)
else:
    if df == 0:
        c = tm//2
    else:
        c = (tm-df)//2+df
    ans = mod_nCr(tm, c)
    print(ans)

Solution (Python):
import math

mod = 10**9+7

def comb(n, r):
	x = 1
	y = 1
	if n-r<r:
		r = n-r
	for i in range(r):
		x *= n-i
		y *= (i+1)
		x %= mod
		y %= mod
	return x * pow(y, mod-2, mod) % mod

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

o = -1

if X%2 == 0:
	if (Y-X//2)%3 != 0:
		o = 0
else:
	if (Y-(X-3)//2)%3 != 0:
		o = 0

if o == -1:
	n = (2*Y-X)//3
	m = Y-2*n
	if n >= 0 and m >= 0:
		o = comb(n+m, min(n,m))
	else:
		o = 0
		
print(o)

Solution (Python):
import itertools
import math
import numpy as np
import fractions
from collections import deque
from itertools import combinations
from functools import reduce
from functools import lru_cache
import bisect
import sys
sys.setrecursionlimit(10**9)

def main():
    X, Y = map(int,input().split())
    p = 10**9+7

    i = (2*X-Y)/3
    j = (2*Y-X)/3

    if i < 0 or j < 0 or not(i.is_integer() and j.is_integer()):
        print(0)
        exit()

    i = int(i)
    j = int(j)

    def comb(n, r, mod):
        r = min(r, n-r)
        res = 1
        for i in range(r):
            res = res * (n - i) * pow(i+1, mod-2, mod) % mod
        return res

    print(comb(i+j,i,p))


if __name__ == "__main__":
  main()

Solution (Python):
import sys
import math  
import bisect  
import queue  


def input():
    return sys.stdin.readline().rstrip()


def main():
    X, Y = map(int, input().split())

    if (2 * X - Y) % 3 != 0 or (2 * Y - X) % 3 != 0:
        return print(0)

    a = (2 * X - Y) // 3
    b = (2 * Y - X) // 3

    MOD = 10 ** 9 + 7  
    N = a + b  
    fact = [1, 1]  
    factinv = [1, 1]  
    inv = [0, 1]

    for i in range(2, N + 1):
        fact.append((fact[-1] * i) % MOD)
        inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
        factinv.append((factinv[-1] * inv[-1]) % MOD)

    def cmb(n, r, p):
        if (r < 0) or (n < r):
            return 0
        r = min(r, n - r)
        return fact[n] * factinv[r] * factinv[n - r] % p

    n = a + b  
    r = min(a, b)  
    
    print(cmb(n, r, MOD))


if __name__ == '__main__':
    main()

Solution (Python):
def mod_com(n, k, mod):
    def ext_gcd(a, b):
        if b == 0:
            return a, 1, 0
        else:
            d, x, y = ext_gcd(b, a % b)
            x -= (a // b)*y
        return d, y, x
    p, q = 1, 1
    for i in range(n - k + 1, n + 1):
        p = (p * i) % mod
    for i in range(2, k+1):
        q = (q * i) % mod
    return int(p * (ext_gcd(q, mod)[1] % mod) % mod)


x, y = map(int, input().split())
mod = 10 ** 9 + 7
dif = abs(x - y)
num = min([x, y]) - dif
if num % 3 != 0:
    print(0)
    exit()
num_1 = num / 3 + dif
num_2 = num / 3
print(mod_com(int(num_1 + num_2), int(num_1), mod))

Solution (Python):
x, y = map(int, input().split())
if (x + y) % 3 != 0:
    print(0)
    exit()
a = (x + y) // 3
x -= a
y -= a
if x < 0 or y < 0:
    print(0)
    exit()
mod = 10 ** 9 + 7
def combination(n, r):
    x = 1
    y = 1
    for i in range(r):
        x = x * (n - i) % mod
        y = y * (r - i) % mod
    return (x * pow(y, mod-2, mod)) % mod
print(combination(x+y, min(x, y)))

Solution (Python):
def pow(x, n , mod):
    res = 1
    while n > 0:
        if  bin(n & 1) == bin(1) :
            res = res * x % mod
        x = x*x % mod
        n = n >>1
    return res   

def combination(n,a):
    x = 1
    for i in range(a):
        x = x * (n-i) % mod
    y = 1
    for i in range(a):
        y = y * (a-i) % mod
    y = pow(y, mod-2, mod)
    return (x*y % mod)

x, y = map(int, input().split())
mod = 10**9 + 7
if ((x+y) % 3) != 0:
    print(0)
else:
    m = (2*x-y)//3
    n = x - 2*m
    if m < 0 or n < 0:
        print(0)
    else:
        ans = combination(n+m,n)
        print(ans)

Solution (Python):
m = 10 ** 9 + 7
def cmb(x,y):
    r = 1
    for i in range(1, y + 1):
        r = (r * (x - i + 1) * pow(i, m-2, m)) % m
    return r

x, y = map(int, input().split())

a = (2 * x - y) / 3
b = (2 * y - x) / 3
if (x + y) % 3 == 0 and a >= 0 and b >= 0:
    print(cmb(int((x + y) / 3), int(a)))
else:
    print(0)

Solution (Python):
def cmb(n, r, p): 
    N = 10 ** 6  
    fact = [1, 1]  
    factinv = [1, 1]  
    inv = [0, 1]  
    
    for i in range(2, N + 1):
        fact.append((fact[-1] * i) % p)
        inv.append((-inv[p % i] * (p // i)) % p)
        factinv.append((factinv[-1] * inv[-1]) % p)

    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p




X,Y = (int(x) for x in input().split())


kotae =0
if (X+Y) %3 !=0 or X*2< Y or Y*2 <X :
    a =b =0
else: 
    b=(2*X -Y)//3
    a=X-2*b

    
    if b<a:
        a,b = b,a
    
    mod =10**9 +7
    kotae = cmb(a+b,a,mod ) 
    
print(kotae)

Solution (Python):
MOD = 10**9 + 7
def nck(n, k):
    numer, denom = 1, 1
    for i in range(k):
        numer = numer * (n - i) % MOD
        denom = denom * (k - i) % MOD
    return numer * pow(denom, MOD-2, MOD) % MOD

x, y = map(int, input().split())

n = -1
for m in range(min(x//1, y//2) + 1):
    rx = x - m
    ry = y - 2 * m
    if rx == ry * 2:
        n = ry
        break

if n == -1:
    print(0)
else:
    print(nck(m+n, min(m, n)))

Solution (Python):
mod = 10**9+7
MAX_N = 10**6

fact = [1]
fact_inv = [0]*(MAX_N+4)
for i in range(MAX_N+3):
    fact.append(fact[-1]*(i+1)%mod)

fact_inv[-1] = pow(fact[-1],mod-2,mod)
for i in range(MAX_N+2,-1,-1):
    fact_inv[i] = fact_inv[i+1]*(i+1)%mod


def mod_comb_k(n,k,mod):
    return fact[n] * fact_inv[k] % mod * fact_inv[n-k] %mod
X,Y = map(int,input().split())
if (2*Y-X) % 3 != 0 or (2*X-Y) % 3  != 0 or 2*Y-X <0 or 2*X-Y<0:
    print(0)
else:
    A = int((2*Y-X)/3)
    B = int((2*X-Y)/3)
    print(mod_comb_k(A+B,A,mod))

Solution (Python):
x,y=map(int,input().split())

mod=10**9+7
n=10**6
f=[1,1]
g=[1,1]
h=[0,1]

for i in range(2,n+1):
  f.append(f[-1]*i%mod)
  h.append((-h[mod%i]*(mod//i))%mod)
  g.append(g[-1]*h[i]%mod)
  
def cmb(n,r):
  p=f[n]*g[r]*g[n-r]%mod
  return p

if x==y:
  if x%3==0:
    print(cmb((x+y)//3,x//3))
  else:
    print(0)
elif x>y:
  if 2*y-x>=0 and (2*y-x)%3==0:
    print(cmb((x+y)//3,(2*y-x)//3))
  else:
    print(0)
else:
  if 2*x-y>=0 and (2*x-y)%3==0:
    print(cmb((x+y)//3,(2*x-y)//3))
  else:
    print(0)

Solution (Python):
import math

def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

x, y = map(int,input().split())

'''
(1, 2)をn回、(2, 1)をm回
'''

if 2*x-y >= 0 and 2*y-x >= 0 and (2*x-y) % 3 == 0 and (2*y-x) % 3 == 0:
    n = int((2*y-x)/3)
    m = int((2*x-y)/3)

    p = 10 ** 9 + 7
    N = n + m  
    fact = [1, 1]
    factinv = [1, 1]
    inv = [0, 1]

    for i in range(2, N + 1):
        fact.append((fact[-1] * i) % p)
        inv.append((-inv[p % i] * (p // i)) % p)
        factinv.append((factinv[-1] * inv[-1]) % p)

    ans = cmb(N, m, p)

    print(ans)
else:
    print(0)

Solution (Python):
X, Y = map(int, input().split())
mod = 10**9+7

plus = X+Y

if not plus%3==0:
    print(0)
elif abs(X-Y)>plus//3:
    print(0)
else:
    row = plus//3
    col = Y-row
    l, r = 1, 1
    for c in range(col):
        l = (l*(row-c))%mod
        r = r*(c+1)%mod
    print((l*pow(r, mod-2, mod))%mod)

Solution (Python):
x, y = [int(s) for s in input().split()]

a, a_mod = divmod(2 * y - x, 3)
b, b_mod = divmod(2 * x - y, 3)
if a < 0 or a_mod != 0 or b < 0 or b_mod != 0:
    print(0)
    exit()

mod = 10 ** 9 + 7

n = a + b
temp_list = [1] * (n + 1)
for i in range(1, n + 1):
    temp_list[i] = i * temp_list[i - 1] % mod

stop = a if a < b else b
i_temp_list = [1] * (n + 1)
i_temp_list[-1] = pow(temp_list[-1], mod - 2, mod)
for i in range(n, stop, -1):
    i_temp_list[i - 1] = i_temp_list[i] * i % mod

ans = temp_list[n] * i_temp_list[a] * i_temp_list[b] % mod
print(ans)

Solution (Python):
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p
p = 10**9 + 7
N = 10**6  
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

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

n = (2*Y - X) // 3
m = (2*X - Y) // 3
ans = cmb(n + m, min(n, m), p)
print(ans)

Solution (Python):
def make_inv_tables(mod, n):
    

    fac = [1, 1]
    ifac = [1, 1]
    inverse = [0, 1]

    for i in range(2, n+1):
        fac.append((fac[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod // i)) % mod)
        ifac.append((ifac[-1] * inverse[-1]) % mod)
    return fac, ifac

def cmb(n, k, mod, fac, ifac):
    k = min(k, n-k)
    return fac[n] * ifac[k] * ifac[n-k] % mod

X, Y = map(int, input().split())
total = int((X + Y) / 3)
n = X - total
if (X + Y) % 3 != 0:
    print(0)
elif X - total < 0 or Y - total < 0:
    print(0)
else:
    MOD = 10 **  9 + 7
    fac, ifac = make_inv_tables(MOD, total)
    ans = cmb(total, n, MOD, fac, ifac)
    print(ans)

Solution (Python):
x,y=map(int,input().split())

if x<y:
  x,y=y,x
  
def cmb(n,r,p):
  if r<0 or n<r:
    return 0
  r=min(r,n-r)
  return fact[n]*factinv[r]*factinv[n-r]%p

p=10**9+7
N=10**6
fact=[1,1]
factinv=[1,1]
inv=[0,1]

for i in range(2,N+1):
  fact.append((fact[-1]*i)%p)
  inv.append((-inv[p%i]*(p//i))%p)
  factinv.append((factinv[-1]*inv[-1])%p)
  

if 2*y>=x and (x+y)%3==0:
  print(cmb((x+y)//3,(2*y-x)//3,p))
else:
  print(0)

Solution (Python):

import io
import math

MOD = 10 ** 9 + 7

nim, mike = map(int, input().split())

m = 2 * nim - mike
n = 2 * mike - nim

if m % 3 or n % 3 or m < 0 or n < 0:
    print(0)
    quit()

m //= 3
n //= 3

factorial = [1] * (n + m + 1)
for i in range(n + m):
    factorial[i + 1] = factorial[i] * (i + 1) % MOD

print(factorial[m + n] * pow(factorial[m], MOD - 2, MOD) * pow(factorial[n], MOD - 2, MOD) % MOD)

Solution (Python):
def power(x, a):
    if a == 0:
        return 1
    elif a == 1:
        return x
    elif a % 2 == 0:
        return power(x, a//2) **2 % mod
    else:
        return power(x, a//2) **2 * x % mod


def modinv(x):
    return power(x, mod-2)

def binomial_coefficients(n, k):
    numera = 1  
    denomi = 1  

    for i in range(k):
        numera *= n-i
        numera %= mod
        denomi *= i+1
        denomi %= mod
    return numera * modinv(denomi) % mod

X,Y=map(int,input().split())
mod=10**9 + 7
if Y<=2*X and Y>=0.5*X and (X+Y)%3==0:
  print(binomial_coefficients((X+Y)//3,((X+Y)//3)*2-X))
else:
  print(0)

Solution (Python):
def C(n, r, m):
    num, den = 1, 1
    for i in range(1, r + 1):
        num = num * (n - i + 1) % m
        den = den * i % m
    return (num * pow(den, m - 2, m)) % m

x, y = map(int, input().split())
m = (2*x - y) // 3
n = (-x + 2*y) // 3
if (x + y) % 3 != 0 or m < 0 or n < 0:
    print(0)
else:
    print(C(n + m, n, 10**9 + 7))

Solution (Python):
X, Y = map(int, input().split(" "))
mod = 10 ** 9 + 7
if (2 * Y - X) % 3 == 0 and (2 * X - Y) % 3 == 0:
    n, m = (2 * Y - X) // 3, (2 * X - Y) // 3
    if n < 0 or m < 0:
        print(0)
    else:
        fact = [1, 1]
        inv = [1, 1]
        inv_fact = [1, 1]
        for i in range(2, n + m + 1):
            fact.append((fact[-1] * i) % mod)
            inv.append(mod - inv[mod % i] * (mod // i) % mod)
            inv_fact.append((inv_fact[-1] * inv[-1]) % mod)
        ans = fact[n + m] * inv_fact[n] * inv_fact[m]
        print(ans % mod)
else:
    print(0)

Solution (Python):
x,y=map(int,input().split())

if (2*x-y)%3!=0 or (2*x-y)<0:
  print(0)
  exit()
if (2*y-x)%3!=0 or (2*y-x)<0:
  print(0)
  exit()
p=int((2*x-y)/3)
q=int((2*y-x)/3)
M=p+q+2
Mod=10**9+7

fac=[0]*M
finv=[0]*M
inv=[0]*M

def COMinit():
  fac[0]=fac[1]=1
  finv[0]=finv[1]=1
  inv[1]=1
  for i in range(2,M):
    fac[i]=(fac[i-1]*i%Mod)%Mod
    inv[i]=Mod-inv[Mod%i]*(Mod//i) %Mod
    finv[i]=(finv[i-1]*inv[i]%Mod)%Mod
def COM(n,k):
  if n<k:
    return 0
  if n<0 or k<0:
    return 0
  return (fac[n]*(finv[k]*finv[n-k]%Mod)%Mod)%Mod

COMinit()
print(COM(p+q,p))

Solution (Python):
x,y=map(int,input().split())

if (2*x-y)%3!=0 or (2*x-y)<0:
  print(0)
  exit()
if (2*y-x)%3!=0 or (2*y-x)<0:
  print(0)
  exit()
p=int((2*x-y)/3)
q=int((2*y-x)/3)
M=p+q+2
Mod=10**9+7

fac=[0]*M
finv=[0]*M
inv=[0]*M

def COMinit():
  fac[0]=fac[1]=1
  finv[0]=finv[1]=1
  inv[1]=1
  for i in range(2,M):
    fac[i]=(fac[i-1]*i%Mod)%Mod
    inv[i]=Mod-inv[Mod%i]*(Mod//i) %Mod
    finv[i]=(finv[i-1]*inv[i]%Mod)%Mod
def COM(n,k):
  if n<k:
    return 0
  if n<0 or k<0:
    return 0
  return (fac[n]*(finv[k]*finv[n-k]%Mod)%Mod)%Mod

COMinit()
print(COM(p+q,p))

Solution (Python):
x,y=map(int,input().split())

if (2*x-y)%3!=0 or (2*x-y)<0:
  print(0)
  exit()
if (2*y-x)%3!=0 or (2*y-x)<0:
  print(0)
  exit()
p=int((2*x-y)/3)
q=int((2*y-x)/3)
M=p+q+2
Mod=10**9+7

fac=[0]*M
finv=[0]*M
inv=[0]*M

def COMinit():
  fac[0]=fac[1]=1
  finv[0]=finv[1]=1
  inv[1]=1
  for i in range(2,M):
    fac[i]=(fac[i-1]*i%Mod)%Mod
    inv[i]=Mod-inv[Mod%i]*(Mod//i) %Mod
    finv[i]=(finv[i-1]*inv[i]%Mod)%Mod
def COM(n,k):
  if n<k:
    return 0
  if n<0 or k<0:
    return 0
  return (fac[n]*(finv[k]*finv[n-k]%Mod)%Mod)%Mod

COMinit()
print(COM(p+q,p))

Solution (Python):
def framod(n, mod, a=1):
    for i in range(1,n+1):
        a=a * i % mod
    return a

def power(n, r, mod):
    if r == 0: return 1
    if r%2 == 0:
        return power(n*n % mod, r//2, mod) % mod
    if r%2 == 1:
        return n * power(n, r-1, mod) % mod

def comb(n, k, mod):
    a=framod(n, mod)
    b=framod(k, mod)
    c=framod(n-k, mod)
    return (a * power(b, mod-2, mod) * power(c, mod-2, mod)) % mod

x, y = map(int, input().split())
if  x/y > 2 or y/x > 2:
    print(0)
else:
    if (2*x-y)%3 != 0 or (2*y - x)%3 != 0:
        print(0)
    else:
        a = (2*x-y) / 3
        b = (2*y-x) / 3
        a, b = int(a), int(b)
        mod = 10**9 + 7
        print(comb(a+b, a, mod))

Solution (Python):
import math
import copy
x,y = map(int, input().split())
m = 1000000007

if (x+y)%3!=0:
    print(0)
    exit()


step = (x+y)//3

step_x = (step+abs(x-y))//2
step_y = (step-abs(x-y))//2

if (step_y<0):
    print(0)
    exit()
    
def com(n,r,m):
    
    a = 1
    for i in range(1,n+1):
        a *= i
        a %= m
    A = pow(a,m-2,m)
    
    
    b= copy.deepcopy(A)   
    for i in range(n,r,-1):
        b = (b*i)%m
    
    c = copy.deepcopy(A)
    for i in range(n,n-r,-1):
        c = (c*i)%m
       
    return (a*b*c)%m
    
    
print(int(com(step_x+step_y,step_x,m)))

Solution (Python):
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
p = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

def main():

    def combination(n, r, p):
        if (r < 0) or (n < r):
            return 0
        r = min(r, n - r)
        return fact[n] * factinv[r] * factinv[n-r] % p

    N = 10 ** 6  
    fact = [1, 1]  
    factinv = [1, 1]  
    inv = [0, 1]  
    
    for i in range(2, N + 1):
        fact.append((fact[-1] * i) % p)
        inv.append((-inv[p % i] * (p // i)) % p)
        factinv.append((factinv[-1] * inv[-1]) % p)

    x, y = nm()

    if (x + y) % 3 != 0:
        print(0)
        exit()
    
    n = (2*y - x) // 3 
    m = (2*x - y) // 3 

    if n < 0 or m < 0:
        print(0)
        exit()

    print(combination(n+m, n, p))

if __name__ == '__main__':
    main()

Solution (Python):
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
p = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

def main():

    def combination(n, r, p):
        if (r < 0) or (n < r):
            return 0
        r = min(r, n - r)
        return fact[n] * factinv[r] * factinv[n-r] % p

    N = 10 ** 6  
    fact = [1, 1]  
    factinv = [1, 1]  
    inv = [0, 1]  
    
    for i in range(2, N + 1):
        fact.append((fact[-1] * i) % p)
        inv.append((-inv[p % i] * (p // i)) % p)
        factinv.append((factinv[-1] * inv[-1]) % p)

    x, y = nm()

    if (x + y) % 3 != 0:
        print(0)
        exit()
    
    n = (2*y - x) // 3 
    m = (2*x - y) // 3 

    if n < 0 or m < 0:
        print(0)
        exit()

    print(combination(n+m, n, p))

if __name__ == '__main__':
    main()

Solution (Python):
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))


def fur(n,r):
    p,q = 1,1
    for i in range(r):
        p = p*(n-i)%mod
        q = q*(i+1)%mod
    return p * pow(q,mod-2,mod) % mod

x, y = nm()

if (x + y) % 3 != 0:
    print(0)
    exit()

n = (2*y - x) // 3 
m = (2*x - y) // 3 

if n < 0 or m < 0:
    print(0)
    exit()

print(fur(n+m, n))

Solution (Python):
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))


def fur(n,r):
    p,q = 1,1
    for i in range(r):
        p = p*(n-i)%mod
        q = q*(i+1)%mod
    return p * pow(q,mod-2,mod) % mod

x, y = nm()

if (x + y) % 3 != 0:
    print(0)
    exit()

n = (2*y - x) // 3 
m = (2*x - y) // 3 

if n < 0 or m < 0:
    print(0)
    exit()

print(fur(n+m, n))

Solution (Python):
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

def main():

    def fur(n,r):
        p,q = 1,1
        for i in range(r):
            p = p*(n-i)%mod
            q = q*(i+1)%mod
        return p * pow(q,mod-2,mod) % mod

    x, y = nm()

    if (x + y) % 3 != 0:
        print(0)
        exit()
    
    n = (2*y - x) // 3 
    m = (2*x - y) // 3 

    if n < 0 or m < 0:
        print(0)
        exit()

    print(fur(n+m, n))

if __name__ == '__main__':
    main()

Solution (Python):
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

def main():

    def fur(n,r):
        p,q = 1,1
        for i in range(r):
            p = p*(n-i)%mod
            q = q*(i+1)%mod
        return p * pow(q,mod-2,mod) % mod

    x, y = nm()

    if (x + y) % 3 != 0:
        print(0)
        exit()
    
    n = (2*y - x) // 3 
    m = (2*x - y) // 3 

    if n < 0 or m < 0:
        print(0)
        exit()

    print(fur(n+m, n))

if __name__ == '__main__':
    main()

Solution (Python):
def cmb(n, k, mod, fac, ifac):
    """
    nCkを計算する
    """
    k = min(k, n-k)
    return fac[n] * ifac[k] * ifac[n-k] % mod


def make_tables(mod, n):
    """
    階乗テーブル、逆元の階乗テーブルを作成する
    """
    fac = [1, 1] 
    ifac = [1, 1] 
    inverse = [0, 1] 

    for i in range(2, n+1):
        fac.append((fac[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod//i)) % mod)
        ifac.append((ifac[-1] * inverse[-1]) % mod)
    return fac, ifac


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

if X > Y:
    X, Y = Y, X
dist = X + Y

if dist % 3 != 0:
    print(0)
else:
    
    
    
    total = int((X+Y) / 3)
    n = X - total

    
    
    
    if Y > 2*X:
        print(0)
    else:
        MOD = 10**9 + 7

        fac, ifac = make_tables(MOD, total)
        ans = cmb(total, n, MOD, fac, ifac)
        print(ans)

Solution (Python):
def comb(n,k,mod):
    x = y = 1
    for i in range(min(k,n-k)):
        x = x*(n-i)%mod
        y = y*(i+1)%mod
    return x * pow(y, mod-2, mod) % mod

x,y = map(int,input().split())
ans = 0
mod = 10**9+7
if (x+y)%3 == 0:
    a = (-x+2*y)//3
    b = (2*x-y)//3
    if a >= 0 and b >= 0: ans = comb(a+b,a,mod)
print(ans)

Solution (Python):
X, Y = map(int, input().split())

def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 6  
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
 
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)


a = (2*X - Y)/3
b = (2*Y - X)/3

if (X+Y)%3 != 0:
    ans = 0

elif a<0 or b<0:
    ans = 0

else:
    ans = cmb(int(a + b), int(a), 10**9 + 7)

print(ans)

Solution (Python):
x,y=map(int,input().split())
if (2*x-y)%3!=0 or (x-2*y)%3!=0:
  print(0)
  exit()
a=(2*x-y)//3
b=(2*y-x)//3
if a<0 or b<0:
  print(0)
  exit()
mod=10**9+7
def comb(x,y):
    s=1
    t=1
    for i in range(1,y+1):
        s=s*(x-i+1)%mod
        t=t*(i)%mod
    return (s*pow(t,mod-2,mod))%mod
print(comb(a+b,min(a,b)))

Solution (Python):
X,Y = map(int,input().split())

class Combination(): 
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(10**6)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.fac, self.facinv = self.prepare(n_max)
        
        
    def prepare(self,n): 
        
        factorials = [1]  
        for m in range(1, n+1):
            factorials.append(factorials[m-1]*m%self.mod)
        
        invs = [1] * (n+1)
        invs[n] = pow(factorials[n], self.mod-2, self.mod)
        for m in range(n, 1, -1):
            invs[m-1] = invs[m]*m%self.mod
        return factorials, invs 
    
    def make_modinv_list(self, n): 
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            
            
            
            modinv[i] = (self.mod - self.mod//i)*modinv[self.mod%i]  % self.mod
        return modinv

    def perm_mod(self,n,r): 
        if n < r:
            return 0
        if (n < 0 or r < 0):
            return 0
        return (self.fac[n] * self.facinv[n-r]) % self.mod

    def combi_mod(self,n,r): 
        if n < r:
            return 0
        if (n < 0 or r < 0):
            return 0
        return (((self.fac[n] * self.facinv[r]) % self.mod) * self.facinv[n-r]) % self.mod


if (X+Y)%3==0:
    d = (X+Y)//3
    if d<=X<=X+Y-d and d<=Y<=X+Y-d:
        C = Combination(d+1)
        print(C.combi_mod(d,abs(d-X)))
    else:
        print(0)
else:
    print(0)