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, re
from math import ceil, floor, sqrt, pi, factorial
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left
from functools import reduce
input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [s_list() for _ in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7

def mod_cmb(n: int, k: int, p: int) -> int:
    if n < 0 or k < 0 or n < k: return 0
    if n == 0 or k == 0: return 1
    if (k > n - k):
        return mod_cmb(n, n - k, p)
    c = d = 1
    for i in range(k):
        c *= (n - i)
        d *= (k - i)
        c %= p
        d %= p
    return c * pow(d, p - 2, p) % p

def main():
    x, y = i_map()

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

    a = int((2*x-y) / 3)
    b = int((-x+2*y) / 3)

    if a < 0 or b < 0:
        print(0)
        exit()

    print(mod_cmb(a+b,a,MOD))

if __name__ == '__main__':
    main()

Solution (Python):
from math import factorial
MOD = 10**9+7

def com(n, r):
    X = Y = 1
    if r < 0 or n < r:
        return 0
    if n-r < r:
        r = n-r
    for i in range(1, r+1):
        Y = Y*i % MOD
        X = X*(n-i+1) % MOD
    Y = pow(Y, MOD-2, MOD)
    return X*Y

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

dif = max(x, y) - min(x, y)
small = min(x, y)
tie = small-dif
res = tie % 3
num = tie / 3  

if res != 0:
  print(0)
else:
  print(com(int(num*2+dif), int(num))%MOD)

Solution (Python):


MOD = 10 ** 9 + 7
x, y = map(int, input().split())

n, r1 = divmod(2 * y - x, 3)
m, r2 = divmod(2 * x - y, 3)

def com(n, r):
    X = Y = 1
    if r < 0 or n < r:
        return 0
    if n - r < r:
        r = n - r
    for i in range(1, r + 1):
        Y = Y * i % MOD
        X = X * (n - i + 1) % MOD
    Y = pow(Y, MOD - 2, MOD)
    return X * Y

if r1 != 0 or r2 != 0:
    print(0)
else:
    print(com(n + m, n) % MOD)

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10 ** 7)

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

class Combination:
    def __init__(self, max_n=100007, mod=mod):
        self.mod = mod
        self.fac = [0] * max_n
        self.finv = [0] * max_n
        self.inv = [0] * max_n

        self.fac[0] = 1
        self.fac[1] = 1
        self.finv[0] = 1
        self.finv[1] = 1
        self.inv[1] = 1
        for i in range(2, max_n):
            self.fac[i] = self.fac[i - 1] * i % self.mod
            self.inv[i] = self.mod - self.inv[self.mod % i] * (self.mod // i) % self.mod
            self.finv[i] = self.finv[i - 1] * self.inv[i] % self.mod
    
    def choose(self, n, k):
        if n < k:
            return 0
        if n < 0 or k < 0:
            return 0
        return self.fac[n] * (self.finv[k] * self.finv[n - k] % self.mod) % self.mod


x, y = na()

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

n, m = -1, -1
for i in range(10 ** 6 + 7):
    if (x - i) // 2 + 2 * i == y:
        n = i
        m = (x - i) // 2
        break

if n < 0 or m < 0:
    print(0)
    exit()
c = Combination(10 ** 6 + 7)
print(c.choose(n + m, n) % mod)

Solution (Python):
X,Y =map(int,input().split())
 
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 
N = 10**6
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 
 
for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )
 
 
if (X+Y)%3 !=0:
  print(0)
  
else:
  n=(X+Y)//3
  r=(-2*X+Y)//(-3)
  ans=cmb(n,r,mod)
  print(ans)

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

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 
N = 10**6
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )


if (X+Y)%3 !=0:
  print(0)
  
else:
  n=(X+Y)//3
  r=(-2*X+Y)//(-3)
  ans=cmb(n,r,mod)
  print(ans)

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

def nCk(n,k):
  MAX_N=n+5
  fac = [1,1] + [0]*MAX_N
  finv = [1,1] + [0]*MAX_N
  inv = [0,1] + [0]*MAX_N
  for i in range(2,MAX_N):
    fac[i] = fac[i-1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    finv[i] = finv[i-1] * inv[i] % mod
  
  if n<k:
    return 0
  if n<0 or k<0:
    return 0
  return fac[n] * (finv[k] * finv[n-k] % mod) % mod

n=(y-0.5*x)/1.5
if int(n)!=n:
    print(0)
else:
    m=(y+x)//3
    l=int(n)
    print(nCk(m,l))

Solution (Python):
def cmb(n, r, mod):
    if r < 0 or r > n:
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9 + 7 
N = 10**6
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 

for i in range(2, N + 1):
    g1.append((g1[-1] * i) % mod)
    inverse.append((-inverse[mod % i] * (mod//i)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)

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

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

A = (2 * Y - X) // 3
B = (2 * X - Y) // 3

print(cmb(A+B, A, mod))

Solution (Python):



import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect




class Mint(object):
    def cmb(self, n, r, mod: int = 10 ** 9 + 7):
        if (r < 0) or (r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % mod

    def cmb_prep(self, N: int, mod: int = 10 ** 9 + 7):
        self.g1 = [1, 1]  
        self.g2 = [1, 1]  
        inverse = [0, 1]  

        for i in range(2, N + 1):
            self.g1.append((self.g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            self.g2.append((self.g2[-1] * inverse[-1]) % mod)

    def cmb_(n, r):
        """ 普通にnCrを求める
        >>> p = Mint()
        >>> p.cmb_(10, 2)
        45
        """
        from operator import mul
        from functools import reduce

        r = min(n - r, r)
        if r == 0:
            return 1
        over = reduce(mul, range(n, n - r, -1))
        under = reduce(mul, range(1, r + 1))
        return over // under


mod = 10**9 + 7
x, y = map(int, input().split())

if (2 * y -x) % 3 != 0:
    print(0)
    sys.exit()

if (2 * x -y) % 3 != 0:
    print(0)
    sys.exit()

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

mint = Mint()
mint.cmb_prep(n + m, mod)

print(mint.cmb(n+m, n, mod))

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

def nCr(n,r,p):
    r=min(r,n-r)
    if r==0:
        return 1
    x,y=1,1
    for i in range(r):
        x=x*(n-i)%p
        y=y*(i+1)%p
    return x*pow(y,p-2,p)%p

if (x + y) % 3 != 0:
    print(0)
else:
    ans = 0
    n = (x + y)//3
    for i in range(n+1):
        a = n - i
        b = i
        if a + 2 * b == x and 2 * a + b == y:
            ans += nCr(n, min(a, b), 10**9 + 7)
            ans = ans % (10**9 + 7)
    print(ans)

Solution (Python):
MOD = 10**9+7
x, y = map(int, input().split())
n, r1 = divmod(2*y-x, 3)
m, r2 = divmod(2*x-y, 3)


def com(n, r):
    X = Y = 1
    if r < 0 or n < r:
        return 0
    if n-r < r:
        r = n-r
    for i in range(1, r+1):
        Y = Y*i % MOD
        X = X*(n-i+1) % MOD
    Y = pow(Y, MOD-2, MOD)
    return X*Y


if r1 or r2:
    print(0)
else:
    print(com(n+m, n) % MOD)

Solution (Python):
MOD = 10**9+7
X, Y = map(int, input().split())
x, y = X, Y
while 0 <= y:
    if x == 2*y:
        break
    x += 1
    y -= 1
else:
    print(0)
    exit()


def com(n, r):
    X = Y = 1
    if r < 0 or n < r:
        return 0
    if n-r < r:
        r = n-r
    for i in range(1, r+1):
        Y = Y*i % MOD
        X = X*(n-i+1) % MOD
    Y = pow(Y, MOD-2, MOD)
    return X*Y


print(com(x//2, x-X) % MOD)

Solution (Python):
def cmb(n,r,mod):
    if r<0 or r>n:return 0
    r=min(r,n-r)
    return g1[n]*g2[r]*g2[n-r]%mod


mod=10**9+7
x,y=map(int,input().split())
g1=[1,1]
g2=[1,1]
inverse=[0,1]

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

for i in range(2,n+m+1):
    g1.append((g1[-1]*i)%mod)
    inverse.append((-inverse[mod%i]*(mod//i))%mod)
    g2.append((g2[-1]*inverse[-1]%mod))

print(cmb(n+m,n,mod))

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from functools import reduce
def mod_comb4(n,r,mod=10**9+7):
    if r==1:return n
    elif r==0:return 1
    else:
        num=reduce(lambda x,y:x*y%mod,range(n,n-r,-1))
        den=reduce(lambda x,y:x*y%mod,range(1,r+1))
        return num*pow(den,mod-2,mod)%mod
def main():
    x,y=map(int,input().split())
    mod=10**9+7
    n=2*x-y
    m=2*y-x
    if n%3==0 and m%3==0 and n>=0 and m>=0:
        print(mod_comb4(n//3+m//3,n//3,mod))
    else:
        print(0)


if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from functools import reduce
def mod_comb4(n,r,mod=10**9+7):
    if r==1:return n
    elif r==0:return 1
    else:
        num=reduce(lambda x,y:x*y%mod,range(n,n-r,-1))
        den=reduce(lambda x,y:x*y%mod,range(1,r+1))
        return num*pow(den,mod-2,mod)%mod
def main():
    x,y=map(int,input().split())
    mod=10**9+7
    n=2*x-y
    m=2*y-x
    if n%3==0 and m%3==0 and n>=0 and m>=0:
        print(mod_comb4(n//3+m//3,n//3,mod))
    else:
        print(0)


if __name__=='__main__':
    main()

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

mod=10**9+7
def C(n, r ,mod=10**9+7):
    if n-r<r: r=n-r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k]) %mod

    return result % mod

a=(-x+2*y)/3
b=(2*x-y)/3
if a<0 or b<0 : print(0) ; exit()
if a%1!=0 or b%1!=0:
    print(0) 
    exit()
a=int(a) ; b=int(b)
print(C(a+b,a))

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

mod=10**9+7
def C(n, r ,mod=10**9+7):
    if n-r<r: r=n-r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k]) %mod

    return result % mod

a=(-x+2*y)/3
b=(2*x-y)/3
if a<0 or b<0 : print(0) ; exit()
if a%1!=0 or b%1!=0:
    print(0) 
    exit()
a=int(a) ; b=int(b)
print(C(a+b,a))

Solution (Python):
from scipy.misc import comb
def modpow(a, n, p):
  if n == 1:
    return a % p 
  elif n % 2 == 1:
    return a * modpow(a, n-1, p) % p
  else:
    return modpow(a, n//2, p) **2 % p
x, y = map(int, input().split())
t = (2*y-x)/3
s = (2*x-y)/3
if not t.is_integer() or t<0 or not s.is_integer() or s<0:
  print(0)
else:
  t = int(t)
  s = int(s)
  bunsi = 1
  for i in range(s+t-s+1, s+t+1):
    bunsi = bunsi * i % 1000000007
  bunbo = 1
  for i in range(1, s+1):
    bunbo = bunbo * i % 1000000007
  ans = bunsi * modpow(bunbo, 1000000007-2, 1000000007) % 1000000007
  print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
large_p = 10**9 + 7

def comb_mod(n, r, large_p):
    a, b = 1, 1
    r = min(r, n - r)
    for i in range(r):
        a = a * (n - i) % large_p
        b = b * (r - i) % large_p
    return a * pow(b, large_p - 2, large_p) % large_p

def main():
    x, y = map(int, input().split())
    if (x + y) % 3 != 0:
        print(0)
        sys.exit()
    p1, p2 = 0, 0
    for i1 in range(0, x + 1):
        if x - i1 == (y - i1 * 2) * 2:
            p1 = i1
            p2 = y - i1 * 2
            break
    if p1 < 0 or p2 < 0 or (p1 == 0 and p2 == 0):
        print(0)
        sys.exit()

    r = comb_mod(p1 + p2, p1, large_p)
    print(r)

if __name__ == '__main__':
    main()

Solution (Python):
import math
def cmb(n, r, mod):
    r = min(r, n - r)
    x = 1
    y = 1
    for i in range(r):
        x = x * (n - i) % mod
        y = y * (i + 1) % mod
    y = pow(y, mod-2, mod)
    return x * y % mod
"""
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
"""
X1, Y1 = map(int, input().split())

if X1 >= Y1:
  X = X1
  Y = Y1
  pass
else:
  X = Y1
  Y = X1
if X > 2 * Y:
  print(0)
  exit()
else:
  pass

m = (2 * X - Y) // 3
n = (2 * Y - X) // 3 

if (2 * X - Y) % 3 != 0 or (2 * Y - X) % 3 != 0:
  print(0)
  exit()
else:
  ans = cmb(m + n, n, 10 ** 9 + 7)
print(ans)

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

def modpow(a, n):
    ret = 1
    while n > 0:
        if n & 1:
            ret = ret * a % MOD
        a = a * a % MOD
        n >>= 1
    return ret

def modinv(a):
    return modpow(a, MOD - 2)

def modfac(x):
    ret = 1
    for i in range(2, x + 1):
        ret *= i
        ret %= MOD
    return ret

ans = 0
if (X + Y) % 3 == 0:
    a = (2 * X - Y) // 3
    b = (2 * Y - X) // 3
    if a >= 0 and b >= 0:
        n = a + b
        r = a
        ans = modfac(n)
        ans *= modinv(modfac(n - r) * modfac(r))
        ans %= MOD
print(ans)

Solution (Python):
a,b = map(int,input().split())
def fac(n):
    p = 1
    for i in range(1,n+1):
        p = (p*i) % (10**9+7)
    return p
if (2*a-b) % 3 == 0 and (-a+2*b) % 3 == 0 and 2*a-b>=0 and -a+2*b >= 0:
    x = (2*a-b) // 3
    y = (-a+2*b) // 3
    print((fac(x+y)*pow(fac(x),10**9+5,10**9+7)*pow(fac(y),10**9+5,10**9+7))%(10**9+7))
else:
    print(0)

Solution (Python):


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

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

n = (2*X - Y) / 3
m = (2*Y - X) / 3

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



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)

print(cmb(int(n + m), int(m), p))

Solution (Python):

import sys


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

    
    a = [None] * (n+1)
    inva = [None] * (n+1)
    a[0] = 1
    inva[0] = 1
    p = 10**9 + 7

    for i in range(1, n+1):
        a[i] = i * a[i-1] % p
        inva[i] = pow(i, p-2, p) * inva[i-1] % p

    ans = (a[n] * inva[r] % p) * inva[n-r] % p
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
x, y  = map(int, input().split())
import itertools
mod = 10**9 + 7
def comb(a,b):
    x,y = 1,1
    for i in range(a):
        x *= (a+b-i)
        y *= (i+1)
        x %= mod 
        y %= mod
    gyakugen = pow(y,mod-2,mod)
    return x*gyakugen %mod
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(int(comb(a,b)))

Solution (Python):






import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


def lcm(a, b):
    return a * b // gcd (a, b)





X,Y = inputIL()

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

x = n*2-Y
y = n-x

print(nCr(x+y,x))

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

MOD = 1000000007
if (2*X-Y)%3!=0:
    print(0)
    sys.exit()

A = int((2*X-Y)/3)
B = int(X-2*A)

if A<0 or B<0:
    print(0)
    sys.exit()

ans = 1
inv = 1
for i in range(B):
    ans = ans * (A+B-i) %MOD
    inv = inv * (i+1) %MOD

ans = ans * pow(inv, MOD-2, MOD) %MOD
print(ans)

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

m = (2*Y - X)/3
n = (2*X - Y)/3

if m.is_integer() == False or n.is_integer() == False:
  print(0)
  exit()
if m < 0 or n < 0:
  print(0)
  exit()

def comb(n, k):
  if k > n - k:
    return comb(n, n - k)
  mul, div = 1, 1
  for i in range(k):
    mul = mul * (n - i) % mod
    div = div * (k - i) % mod
  return mul * pow(div, mod-2, mod) % mod

ans = comb(int(n+m), int(n))
print(ans)

Solution (Python):
import math

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*y-x)/3
b = (2*x-y)/3
if a != int(a) or b != int(b) or a < 0 or b < 0 or (a == 0 and b == 0):
    print(0)
else:
    print(cmb(int(a+b), int(a), p))

Solution (Python):
import sys
read = sys.stdin.buffer.read
input = sys.stdin.readline
input = sys.stdin.buffer.readline



def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))




def main():
	x,y=MI()
	mod=10**9+7

	a=2*y-x
	b=2*x-y

	if a%3!=0 or b%3!=0 or a<0 or b<0:
		print(0)
		exit(0)

	a//=3
	b//=3

	f=[1]
	for i in range(1,10**6):
		f.append(f[-1]*i%mod)

	ans=f[a+b]*pow(f[a],mod-2,mod)*pow(f[b],mod-2,mod)
	print(ans%mod)


if __name__ == "__main__":
	main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
input = sys.stdin.readline
input = sys.stdin.buffer.readline



def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))




def main():
	x,y=MI()
	mod=10**9+7

	a=2*y-x
	b=2*x-y

	if a%3!=0 or b%3!=0 or a<0 or b<0:
		print(0)
		exit(0)

	a//=3
	b//=3

	f=[1]
	for i in range(1,10**6):
		f.append(f[-1]*i%mod)

	ans=f[a+b]*pow(f[a],mod-2,mod)*pow(f[b],mod-2,mod)
	print(ans%mod)


if __name__ == "__main__":
	main()

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

import sys
sys.setrecursionlimit(200000000)
p = 10**9+7

def exp(y,x):
  xbin=str(bin(x))[2:]
  ans=1
  yl=[0]*(len(xbin)+1)
  yl[0]=y
  yl[1]=y**2
  for d,i in enumerate(xbin[::-1],start=0):
    if d > 1:
      yl[d]=yl[d-1]**2%p
    if i == "1":
      ans=(ans*yl[d])%p
  return ans%p

def ff(n,ca):
  if ca == 0:
    return 1
  if ca == 1:
    return n
  return n*ff(n-1,ca-1)%p

if (y+x)%3 != 0:
  print(0)
elif y/x > 2:
  print(0)
elif y/x < 0.5:
  print(0)
else:
  n=(2*x-y)//3
  m=(2*y-x)//3
  fact=1
  for i in range(2,n+1):
    fact=fact*i%p
  ans=ff(n+m,n)*exp(fact,p-2)%p
  print(ans)

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

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

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

n=(x+y)//3

x-=n
y-=n
if x < 0 or y < 0: print(0);exit()

print(cmb(x+y,x,mod))

Solution (Python):
def framod(x,p=10**9+7):
  a=1
  for i in range(1,x+1):
    a = (a*i)%p
  return a

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

if (X+Y)%3!=0:
  print(0)
else:
  a = (2*X-Y)//3
  b = (2*Y-X)//3
  t = a+b
  if a<0 or b<0:
    print(0)
  else:
    S=framod(t)
    T=pow(framod(a),p-2,p)
    U=pow(framod(b),p-2,p)
    print(((S*T)%p)*U%p)

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

import math

def combination(n, x, mod=10**9+7):
    factorial = [1] * (n+1)
    t = 1
    for i in range(1, n+1):
        t = (t * i) % mod
        factorial[i] = t
    tmp = factorial[n]
    tmp = (tmp * pow(factorial[x], mod-2, mod)) % mod
    tmp = (tmp * pow(factorial[n-x], mod-2, mod)) % mod
    return tmp

if 2*Y-X<0 or 2*X-Y<0:
	print(0)
elif (2*Y-X)%3!=0 or (2*X-Y)%3!=0:
	print(0)
else:
	a=(2*Y-X)//3
	b=(2*X-Y)//3
	print(combination(a+b,a,mod=10**9+7))

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

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

n = int((2*Y-X)/3)
m = int((2*X-Y)/3)

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

mod = 10**9+7


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

print(comb(n+m, n, mod))

Solution (Python):
def solve():
    x, y = map(int, input().split())
    n = (2*y-x) / 3
    m = (2*x-y) / 3

    mod = 10**9+7

    if n < 0 or m < 0 or (x+y)% 3 != 0:
        return 0
    cmb = 1
    n = int(n)
    m = int(m)
    
    cnt = 0
    for i in range(n):
        cnt += 1
        cmb *= n+m-i
        cmb *= pow(i+1, mod-2, mod)
        cmb %= mod
    return cmb

print(solve())

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

def cmb(n, r):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9 + 7
N = 10**6
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range( 2, N +1):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

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

A = (2 * Y - X) // 3
B = (2 * X - Y) // 3

print(cmb(A + B, A))

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

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


def comb(m,n):
    MOD = 10**9 + 7
    fac, ifac = make_tables(MOD, m)
    ans = cmb(m, n, MOD, fac, ifac)
    return ans

def calculate(x,y):
    m = 0
    n = 0

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

    result = []

    while (2*m <= x) and (m <= y):
        n = x - 2 * m
        if (2*m + n == x) and (2*n + m == y):
            result.append(comb(m+n,n))

        m = m + 1

    if len(result) == 0:
        print(0)
    else:
        print(min(result))

calculate(X,Y)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): return k.join(list(map(str, lst)))
INF = float('inf')

def mcomb(n, k, mod):
    def mfac(l, r, mod):
        ans = l
        for i in reversed(range(r, l)):
            ans *= i
            ans %= mod
        return ans

    A = mfac(n,n-k+1,mod)
    B = mfac(k,1,mod)
    
    B = pow(B, mod-2, mod)
    return A * B % mod

def solve():
    x, y = MI()
    if (x + y) % 3 != 0:
        print(0)
        return

    n = (x + y) // 3
    x -= n
    y -= n
    if x < 0 or y < 0:
        print(0)
        return

    mod = 1000000007
    ans = mcomb(x+y, max(x, y), mod)
    print(ans % mod)

if __name__ == '__main__':
    solve()

Solution (Python):
X, Y = map( int, input().split())
MOD = 10 ** 9 + 7
  
def n_H_r( n, r ):
  n1, n2 = 1, 1
  for i in range( 1, r + 1 ):
    n1 = n1 * ( n + i )  % MOD
    n2 = n2 * i % MOD
  return ( n1 * pow( n2, MOD - 2, MOD )) % MOD

cnt = 0
if ( ( X + Y ) % 3 == 0 ) and ( max( X, Y ) <= 2 * min( X, Y )):
  base = ( X + Y ) // 3
  a, b = 0, 0
  for i in range( base + 1 ):
    j = base - i
    if ( 2 * i + j == X ) and ( i + 2 * j == Y ):
      a = i
      b = j
      break
  cnt = n_H_r( a, b )
print( cnt )

Solution (Python):
'''
nCk mod pを求める。

nCk= n!/k!(n-k)!を考えるが、単純計算だと途中の桁が膨大になり計算が間に合わない。
そこで計算中にmodをとるが、割り算にmodを適用するためには逆元を求める必要がある。


'''

from collections import defaultdict,deque
import sys, bisect, math, itertools
from functools import lru_cache

sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp():
    '''
    一つの整数
    '''
    return int(input())
def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


class combination():
    def __init__(self, mod):
        '''
        modを指定して初期化
        '''
        self.mod=mod
        self.fac = [1, 1]  
        self.ifac = [1, 1]  
        self.inv = [0, 1]  

    def calc(self,n,k):
        '''
        nCk%modを計算する
        '''
        if k < 0 or n < k:
            return 0
        self.make_tables(n) 
        k = min(k, n - k)
        return self.fac[n] * (self.ifac[k] * self.ifac[n - k] % self.mod) % self.mod

    def make_tables(self, n):
        '''
        階乗テーブル・階乗の逆元テーブルを作成
        '''
        for i in range(len(self.fac), n + 1):
            self.fac.append((self.fac[-1] * i) % self.mod)
            self.inv.append((-self.inv[self.mod % i] * (self.mod // i)) % self.mod)
            self.ifac.append((self.ifac[-1] * self.inv[-1]) % self.mod)

comb = combination(mod)

x, y = inpl()
if x > y:
    x, y = y, x
dist = x + y
if dist % 3:
    print(0)
    exit()
total = int((x + y) // 3)
n = x - total
if y > 2 * x:
    print(0)
    exit()

print(comb.calc(total, n))

Solution (Python):
'''
nCk mod pを求める。

nCk= n!/k!(n-k)!を考えるが、単純計算だと途中の桁が膨大になり計算が間に合わない。
そこで計算中にmodをとるが、割り算にmodを適用するためには逆元を求める必要がある。


'''

from collections import defaultdict,deque
import sys, bisect, math, itertools
from functools import lru_cache

sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp():
    '''
    一つの整数
    '''
    return int(input())
def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


class combination():
    def __init__(self, mod):
        '''
        modを指定して初期化
        '''
        self.mod=mod
        self.fac = [1, 1]  
        self.ifac = [1, 1]  
        self.inv = [0, 1]  

    def calc(self,n,k):
        '''
        nCk%modを計算する
        '''
        if k < 0 or n < k:
            return 0
        self.make_tables(n) 
        k = min(k, n - k)
        return self.fac[n] * (self.ifac[k] * self.ifac[n - k] % self.mod) % self.mod

    def make_tables(self, n):
        '''
        階乗テーブル・階乗の逆元テーブルを作成
        '''
        for i in range(len(self.fac), n + 1):
            self.fac.append((self.fac[-1] * i) % self.mod)
            self.inv.append((-self.inv[self.mod % i] * (self.mod // i)) % self.mod)
            self.ifac.append((self.ifac[-1] * self.inv[-1]) % self.mod)

comb = combination(mod)

x, y = inpl()
if x > y:
    x, y = y, x
dist = x + y
if dist % 3:
    print(0)
    exit()
total = int((x + y) // 3)
n = x - total
if y > 2 * x:
    print(0)
    exit()

print(comb.calc(total, n))

Solution (Python):
MOD = 10 ** 9 + 7
MAX = 700000
fact = [0] * MAX
finv = [0] * MAX
inv = [0] * MAX


def CombInit():
    fact[0] = fact[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, MAX):
        fact[i] = fact[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD


def Comb(n, k):
    if n < k: return 0
    if n < 0 or k < 0: return 0
    return fact[n] * (finv[k] * finv[n - k] % MOD) % MOD


X, Y = map(int, input().split())
if (X + Y) % 3:
    print(0)
else:
    CombInit()
    m = (2 * X - Y) // 3
    n = (2 * Y - X) // 3
    print(int(Comb(m + n, m)))