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):


g1 = [1, 1] 
g2 = [1, 1] 

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 



def resolve():
    global g1
    global g2
    X,Y=(int(i) for i in input().split())
    if (2*X-Y)%3!=0 or 2*X-Y <0:
        print(0)
        return 
    if (2*Y-X)%3!=0 or 2*Y-X <0:
        print(0)
        return 
    a=(2*X-Y)//3
    b=(2*Y-X)//3

    g1 = [1, 1] 
    g2 = [1, 1] 
    inverse = [0, 1] 

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

    print(cmb(a+b,a,mod))


if __name__ == "__main__":
    resolve()

Solution (Python):
def main():
    X, Y = (int(i) for i in input().split())
    fac = [0] * max(X, Y)
    finv = [0] * max(X, Y)
    inv = [0] * max(X, Y)
    MOD = (10**9) + 7

    def COMinit(m):
        fac[0] = 1
        finv[0] = 1
        if m > 1:
            fac[1] = 1
            finv[1] = 1
            inv[1] = 1
            for i in range(2, m):
                fac[i] = fac[i-1] * i % MOD
                inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
                finv[i] = finv[i - 1] * inv[i] % 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

    COMinit(max(X, Y))
    if (X+Y) % 3 != 0:
        return print(0)
    n = (2*Y - X) // 3
    m = (2*X - Y) // 3
    print(COM(n+m, m))


if __name__ == '__main__':
    main()

Solution (Python):
def combination_modp(n, r, p): 
    r = min(n-r, r)
    result = 1
    for i in range(n, n-r, -1):
        result *= i
        result %=p
    for i in range(1, r+1):
        result *= pow(i,p-2,p)
        result %= p
    return result

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

if (x+y)%3 != 0 or 2*y-x <0 or 2*x-y<0:
    print(0)
    exit()

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

ans = combination_modp(a+b,a,mod)
print(ans)

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

if (2*x-y)%3 != 0 or (-x+2*y)%3 != 0 or y>2*x or x>2*y:
  print(0)
  exit()
  
a = ((2*x-y)//3)%mod
b = ((-x+2*y)//3)%mod


def cmb(n, r):
    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])

    return result

print(cmb(a+b,a)%mod)

Solution (Python):

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

mod = 10 ** 9 + 7

a = (2 * Y - X) / 3
b = (2 * X - Y) / 3
if a < 0 or b < 0 or (X + Y) % 3 != 0:
    print(0)
    sys.exit()

a = int(a)
b = int(b)
N = a + b
f = [1]
for i in range(1,a+b+1):
    f.append((f[-1] * i) % mod)
ans = f[a+b] * (pow(f[a], mod-2, mod) * pow(f[b], mod-2, mod) % mod) % mod
print(ans)

Solution (Python):
def cmb(n, r):
    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])
 
    return result
x,y = map(int,input().split())
mod=10**9+7
if (x+y)%3!=0 or x>2*y or y>2*x:
  print('0')
else:
  p=(2*y-x)//3
  q=(2*x-y)//3
  print(cmb(p+q,p)%mod)

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

def comb(n,r,mod, g):
    if r < 0 or n < r: return 0
    return g[n] * pow(g[r], mod-2, mod) * pow(g[n-r], mod-2, mod) % mod

def cmb_mod(n, r, mod):
    g = [1,1]
    for i in range(2, n+1):
        g.append( (g[-1] * i) % mod)
    return comb(n, r, mod, g)

if (x+y) % 3 != 0:
    print('0')
else:
    n = (2 * y - x)
    m = (2 * x - y)
    if (n % 3 != 0 or m % 3 != 0) or (n < 0 or m < 0):
        print('0')
    else:
        n = n // 3
        m = m // 3
        print(cmb_mod(n+m, min(n, m), mod))

Solution (Python):


def s_comb(n,k):
    s=1
    r=min(k,n-k)
    p=10**9+7
    for c in range(r):
        s=(s*(n-c)*pow(c+1,p-2,p))%p
    return s

x,y=map(int,input().split())
if (x+y)%3!=0 or 2*y<x or 2*x<y:
    print(0)
else:
    r=int(min(2*y-x,2*x-y)/3)
    print(s_comb(int((x+y)/3),r))

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

if (X+Y) % 3 != 0:
    print(0)
else:
    n = (2*Y-X)//3
    m = (2*X-Y)//3
    '''
    n*(1,2) + m*(2,1) = (x, y)
    x = n+2m
    y = 2n+m
    2x = 2n+4m
    2x-y = 3m
    m = (2x-y)/3
    n = (2y-x)/3 
    '''
    if 0 > n or 0 > m:
        print(0)
    else:
        ans = 1
        for i in range(1,n+1):
            ans *= (n+m+1-i)*pow(i,mod-2,mod)
            ans %= mod
        print(ans)

Solution (Python):
import math

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

if __name__ == "__main__":
    X, Y = map(int, input().split())

    

    num = 10**9 + 7
    sum = 0
    
    for i in range(X+1):
        if (X-i)% 2 == 0:
            j = (X-i)//2
            
            if i * 2 + j  == Y:
                
                sum += combination(i+j, i, num)  

    print(int(sum))

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

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

def main():
    if (X+Y) % 3 or X > 2*Y or Y > 2*X:
        print(0)
        return
    n = (X+Y) // 3
    r = Y - n
    print(combination(n, r))

main()

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

g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
N = 10**6

def main():
    x, y = map(int, input().split(' '))
    if (2*x-y) % 3 == 0 and (2*y-x) % 3 == 0:
        n = (2*x-y) // 3
        m = (2*y-x) // 3
        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 )
        print(cmb(n+m, n, mod))
    else:
        print(0)


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


if __name__ == '__main__':
    main()

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

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


def nCk(n, k, mod=10**9 + 7):
    x = max(k, n - k)
    y = min(k, n - k)
    kkai = 1
    for i in range(2, y + 1):
        kkai = (kkai * i) % mod
    nkkai = 1
    for i in range(x + 1, n + 1):
        nkkai = (nkkai * i) % mod
    answer = sq(kkai, mod - 2, mod) * nkkai % mod
    return answer


from sys import exit
X, Y = map(int, input().split())
if (X + Y) % 3 != 0:
    print(0)
    exit()

if X % 2 == 0:
    syurui = X - (X // 2) + 1
else:
    syurui = X - (X // 2)
for i in range(syurui):
    two = i
    one = X - two * 2
    if (two + one * 2) == Y:
        print(nCk(two + one, two))
        exit()
print(0)

Solution (Python):

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





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

if a<0 or b<0 or b-int(b)>0.00001 or a-int(a)>0.00001:
  print(ans)
  exit()

MAX = int(a+b)+1
MOD = 1000000007

fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]


def COMinit():
    fac[0],fac[1] = 1,1
    finv[0],finv[1] = 1,1
    inv[1] = 1
    for i in range(2,MAX):
      fac[i] = fac[i - 1] * i % MOD
      inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
      finv[i] = finv[i - 1] * inv[i] % 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

COMinit()
print(COM(int(a+b),int(a)))

Solution (Python):

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





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

if a<0 or b<0 or b-int(b)>0.00001 or a-int(a)>0.00001:
  print(ans)
  exit()
 
MAX = 1000000;
MOD = 1000000007;
 
fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]
 

def COMinit():
    fac[0],fac[1] = 1,1
    finv[0],finv[1] = 1,1
    inv[1] = 1
    for i in range(2,MAX):
      fac[i] = fac[i - 1] * i % MOD
      inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
      finv[i] = finv[i - 1] * inv[i] % 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
 
COMinit()
print(COM(int(a+b),int(a)))

Solution (Python):
def comb(n, k, mod):
    if k > (n // 2):
        k = n - k
    a = 1
    for i in range(k):
        a = (a * (n - i)) % mod
    b = 1
    for i in range(k - 1):
        b = (b *(k - i)) % mod
    a = (a * pow(b, mod - 2, mod)) % mod
    return a


X, Y = map(int, input().split())
ans = 0
mod = 10 ** 9 + 7
if X > Y:
    X, Y = Y, X

if not (X + Y) % 3 == 0 or Y > 2 * X:
    ans = 0
else:
    n = (X + Y) // 3
    d = Y - X
    a = 2*n - X
    ans = comb(n, a, mod)
print(ans)

Solution (Python):

import sys

def com(n,r,m):
 f=[1,1]
 for i in range(2,n+1):
  f.append(f[i-1]*i%m)
 return f[n]*pow(f[r]*f[n-r]%m,m-2,m)%m

mod=10**9+7
x,y=map(int,raw_input().split())
z=(x+y)/3
if (x+y)%3 or abs(x-y)>z:
 print 0
 sys.exit()
print com(z,x-z,mod)

Solution (Python):
def comb(n, k, mod):
    if k > (n // 2):
        k = n - k
    a = 1
    for i in range(k):
        a *= (n - i)
        a %= mod
    for i in range(k - 1):
        a = (a * pow(k - i, mod - 2, mod)) % mod

    return a

X, Y = map(int, input().split())
ans = 0
mod = 10 ** 9 + 7
if X > Y:
    X, Y = Y, X

if not (X + Y) % 3 == 0 or Y > 2 * X:
    ans = 0
else:
    n = (X + Y) // 3
    d = Y - X
    a = 2*n - X
    ans = comb(n, a, mod)
print(ans)

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

def cmb(n, r, mod):
    from operator import mul
    from functools import reduce

    N = n + r
    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 (r<0 or r>n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod


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

else:
    a = (x + y)//3
    b = x-a

    if x-a <0 or y-a <0:
        ans = 0
    else:
        mod = 10**9+7 
        ans = cmb(a, b, mod)

print(int(ans))

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

MOD = 1000000007

def comb_mod(n, r, mod):
    if n < r:
        return 0
    elif n < 0 or r < 0:
        return 0
    else:
        fac = [1, 1]
        finv = [1, 1]
        inv = [0, 1]
        for i in range(2, n + 1):
            fac.append(fac[-1] * i % mod)
            inv.append(-inv[mod % i] * (mod // i) % mod)
            finv.append((finv[-1] * inv[-1]) % mod)

        return int(fac[n] * finv[r] * finv[n - r] % mod)

x, y = map(int, input().split())
m = int((x * 2 - y) / 3)
n = int(x - 2 * m)

if (x + y) % 3 != 0:
    print(0)
else:
    print(comb_mod(n + m, n, MOD))

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


def main():
    X, Y = map(int, input().split())
    MOD = 10 ** 9 + 7
    if (X + Y) % 3 != 0:
        print(0)
        exit()
    m = (2 * X - Y) // 3
    n = (2 * Y - X) // 3

    
    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 )
    
    ans = cmb(n + m, n, MOD, g1, g2)
    print(ans)

if __name__ == "__main__":
    main()

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

def createFacArr(n, mod):
    fac = [0] * n
    finv = [0] * n
    inv = [0] * n
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, 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
    return fac, finv, inv

def comb(n ,k, mod, fac, finv, inv):
    '''
    二項係数の計算
    '''
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n-k] % mod) % mod

def main():
    if (X + Y) % 3 != 0:
        return 0
    
    m = (2 * X - Y) // 3
    n = X - 2 * m
    fac, finv, inv = createFacArr(10**6*2, MOD)
    return comb(m+n, n, MOD, fac, finv, inv)

print(main())

Solution (Python):
import math

MOD = 10**9 + 7


def comb(n, r):
    r = min(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])

    return result
  

def comb_mod(n, r, mod):
  r = min(r, n-r)
  
  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 )

  return g1[n] * g2[r] * g2[n-r] % mod

X, Y = map(int, input().split())
  
if (X+Y)%3!=0:
  print(0)
  quit()


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

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

ans = comb_mod(n+m, n, MOD)

print(ans)

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

def main():
    X, Y = map(int, input().split())
    t = X + Y
    MOD = int(1e9) + 7
    
    if t%3 != 0:
        print(0)
        return
    
    kai = [1] * (1 + (X + Y) // 3)
    for i in range(1, len(kai)):
        kai[i] = (kai[i-1] * i) % MOD

    t //= 3
    ans = 0
    for a in range(t+1):
        if (X - a) % 2 == 0 and (X - a)//2 == Y - 2*a:
            b = Y - 2*a
            ans += kai[a+b] * pow(kai[a], MOD-2, MOD) * pow(kai[b], MOD-2, MOD)
            ans %= MOD
    print(ans)



if __name__ == "__main__":
    main()

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

def nCk(n, k, mod=10 ** 9 + 7):
    x=max(k, n-k)
    y=min(k, n-k)
    kkai=1
    for i in range(2, y+1):
        kkai=(kkai*i)%mod
    nkkai=1
    for i in range(x+1, n+1):
        nkkai=(nkkai*i)%mod
    answer=sq(kkai, mod-2, mod)*nkkai%mod
    return answer
    
import math
c    = [list(map(int, input().split())) for _ in range(1)]
aa=(2*c[0][1]-c[0][0])
bb=(2*c[0][0]-c[0][1])
k=10**9+7

if aa%3!=0 or bb%3!=0:
    print(0)
elif aa<0 or bb<0:
    print(0)
else:
    a=aa//3
    b=bb//3
    answer=nCk(a+b,a)%k
    print(answer)

Solution (Python):
from math import factorial

x, y = map(int, input().split())
fl = True
a = ((2*x - y)/3)
b = ((2*y - x)/3)
if a < 0 or b < 0 or a % 1 != 0 or b % 1 != 0:
  fl = False

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 fl:
  a = int(a)
  b = int(b)
  ans = cmb(a + b,a,mod)
  print(ans)
else:
  print(0)

Solution (Python):
MAX = 1000000
MOD = 1000000007

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


def com_init():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % 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


def resolve():
    com_init()
    X, Y = map(int, input().split())
    if (X + Y) % 3 != 0 or max(X, Y) > min(X, Y) * 2:
        print(0)
        return
    c = (X + Y) // 3
    X -= c
    Y -= c
    print(com(X + Y, X))
    
    
if __name__ == '__main__':
    resolve()

Solution (Python):
import sys

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

inf = 10**9+7

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

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

def nCr(n, r):
    r = min(r, n - r)
    if r == 0: return 1;
    if r == 1: return int(n%inf);
    numerator = [n - r + i + 1 for i in range(r)]
    denominator  = [i + 1 for i 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 = (result*numerator[k])%inf
    return int(result)

print(nCr(a+b, a))

Solution (Python):
import math

def cmb(n, r, mod):
    r = min(r, n-r)

    a = 1
    b = 1
    for i in range(r):
        a = (a * (n-i)) % mod
        b = (b * (i+1)) % mod
    
    return a * pow(b, mod-2, mod) % mod

X, Y = [int(i) for i in input().split()]
mod = 10**9+7 
x_tmp = 2*X - Y
y_tmp = 2*Y - X

if x_tmp < 0 or y_tmp < 0:
    ans = 0
elif x_tmp % 3 != 0 or y_tmp % 3 != 0:
    ans = 0
else:
    x = x_tmp // 3
    y = y_tmp // 3
    ans = cmb(x + y, y, mod)

print(ans)

Solution (Python):
X,Y = map(int,input().split())
if X>Y:
    X,Y = Y,X
if(X+Y)%3:
    print(0)
    exit()
n = (X+Y)//3
if X < n:
    print(0)
    exit()

MOD = 10**9+7
r = X-n

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

def ncr(n,r):
    if n < r: return 0
    if n < 0 or r < 0: return 0
    return fac[n] * (finv[r] * finv[n-r] % MOD) % MOD

print(ncr(n,r))

Solution (Python):
import math
import numpy as np

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:

    A = np.array([[1, 2],
                  [2, 1]])

    b = np.array([x, y])

    N = np.linalg.solve(A, b)


    if N[0].is_integer() == True and N[1].is_integer() == True and N[0] >=0 and N[1] >=0:
          
        print(cmb(int(N[0]+N[1]),int(N[0]),mod))

    else:
        print(0)

else:
    print(0)

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):

def prepare(n, MOD):
  
  f = 1
  for m in range(1, n+1):
    f *= m
    f %= MOD
  fn = f
  
  inv = pow(f, MOD-2, MOD)
  
  invs = [1]*(n+1)
  invs[n] = inv
  for m in range(n, 1, -1):
    inv *= m
    inv %= MOD
    invs[m-1] = inv
    
  return fn, invs

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

d, m = divmod(x+y, 3)
if m != 0 or x/y > 2 or y/x > 2:
  print(0)
  exit()
c = abs(d-x)
f, invs = prepare(d, MOD)
ans = f * invs[c] * invs[d-c] % MOD
print(ans)

Solution (Python):
MOD = 10 ** 9 + 7
def mod_comb(n, k, mod):
    return fact[n] * fact_inv[n-k]%mod * fact_inv[k]%mod

X, Y = map(int, input().split())
if X > Y:
    X, Y = Y, X
if (X + Y) % 3 != 0:
    print(0)
else:
    N = (X + Y) // 3
    M = Y - X
    if M > N:
        print(0)
    else:
        
        
        fact = [1]
        fact_inv = [1]
        for i in range(N):
            new_fact = fact[-1] * (i+1) % MOD
            fact.append(new_fact)
            fact_inv.append(pow(new_fact, MOD - 2, MOD))
        print(mod_comb(N, (N-M)//2, MOD))

Solution (Python):
from numpy.linalg import solve


MOD = 10**9 +7
MAX = 10**6


def COM(n, r):
    fac = [1, 1]
    finv = [1, 1]
    inv = [0, 1]
    for i in range(2, MAX):
        fac.append(fac[i - 1] * i % MOD)
        inv.append(-inv[MOD % i] * (MOD // i))
        finv.append(finv[i - 1] * inv[i] % MOD)
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD

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

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

l = solve([[2, 1], [1, 2]], [x, y])
if l[0] < 0 or l[1] < 0: 
    print(0)
    exit()

print(COM(int(l[0]+l[1]), int(l[0])))

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

def cmb(n, r):
    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])

    return result

if (X+Y)%3!=0:
    print(0)
else:
    itr=(X+Y)//3
    x_mx=itr*2
    if itr<=X and X<=x_mx and itr<=Y and Y<=x_mx and X+Y==itr+x_mx:
        c=X-itr
        f=cmb(itr, c)
        f=f%(10**9+7)
        print(f)
    else:
        print(0)

Solution (Python):
import math




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())


x = int(X-(X+Y)//3)
y = int(Y-(X+Y)//3)
if (X+Y)%3==0 and x>=0 and y>=0:
    print(cmb(x+y,x,mod))
else:
    print(0)

Solution (Python):
import math




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 = 5*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())


x = int(X-(X+Y)//3)
y = int(Y-(X+Y)//3)
if (X+Y)%3==0 and x>=0 and y>=0:
    print(cmb(x+y,x,mod))
else:
    print(0)

Solution (Python):
X,Y=map(int,input().split())
if (X+Y)%3!=0:
    print(0)
    exit()
mod=10**9+7
N=(X+Y)//3
'''
k*(1,2)+(n-k)*(2,1)=(x,y)
(k+2n-2k,2k+n-k)=(x,y)
(2n-k,n+k)=(x,y)
2y=2n+2k
x=2n-k
2y-x=3k
'''
K=(2*Y-X)//3
if not(0<=K<=N):
    print(0)
    exit()
ans=1
for i in range(1,K+1):
    ans*=(N+1-i)*pow(i,mod-2,mod)
    ans%=mod
print(ans)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

class ModTools:
    """ 階乗たくさん使う時用のテーブル準備 """

    def __init__(self, MAX, MOD):
        """ MAX：階乗に使う数値の最大以上まで作る """
        
        MAX += 1
        self.MAX = MAX
        self.MOD = MOD
        
        factorial = [1] * MAX
        factorial[0] = factorial[1] = 1
        for i in range(2, MAX):
            factorial[i] = factorial[i-1] * i % MOD
        
        inverse = [1] * MAX
        
        inverse[MAX-1] = pow(factorial[MAX-1], MOD-2, MOD)
        for i in range(MAX-2, 0, -1):
            
            inverse[i] = inverse[i+1] * (i+1) % MOD
        self.fact = factorial
        self.inv = inverse
    
    def nCr(self, n, r):
        """ 組み合わせの数 (必要な階乗と逆元のテーブルを事前に作っておく) """

        if n < r: return 0
        
        r = min(r, n-r)
        
        numerator = self.fact[n]
        
        denominator = self.inv[r] * self.inv[n-r] % self.MOD
        return numerator * denominator % self.MOD

    def nPr(self, n, r):
        """ 順列 """

        if n < r: return 0
        return self.fact[n] * self.inv[n-r] % self.MOD

    def nHr(self, n, r):
        """ 重複組み合わせ """

        
        return self.nCr(r+n-1, r)

x, y = MAP()

x, y = min(x, y), max(x, y)
sm = x + y
if sm % 3 != 0:
    print(0)
    exit()
if y - x > sm // 3:
    print(0)
    exit()


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

mt = ModTools(a+b, MOD)
print(mt.nCr(a+b, a))

Solution (Python):

import math

def mod_factorial(n):
    mod = 10**9+7

    if n < 1:
        return 1

    r = n
    for i in range(n-1):
        r = r*(i+1) % mod
    
    return r

def mod_pow(n,m):

    mod = 10**9+7
    r = 1

    while m > 0:
        if m & 1:
            r = r*n % mod
        n = n * n % mod
        m = m>>1

    return(r)



def mod_comb(n, k):
    mod = 10**9+7

    if n+k < 1:
        return 1
    
    a = mod_factorial(n)
    b = mod_pow(mod_factorial(k), mod-2)
    c = mod_pow(mod_factorial(n-k), mod-2)


    return (a * ((b * c) % mod)) % mod


a, b= map(int, input().split())

t = [a,b]

r = 0
mod = 10**9+7

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

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

    r += mod_comb(n+m,m)

print(r)

Solution (Python):

def comb(n, r, p): 
    num = den = 1 
    for i in range(r): 
        num = (num * (n - i)) % p 
        den = (den * (i + 1)) % p 
    return (num * pow(den,  p - 2, p)) % p 
  
m = 10**9 + 7
x, y = (int(x) for x in input().split())
ans = 0
if (x - 2*y) % 3 == 0:
    beta = (x - 2*y) // -3
    alpha = y - 2*beta
    ans = comb(alpha+beta, max(alpha, beta), m)
print(ans)

Solution (Python):
M = 10 ** 9 + 7
 
x, y = map(int, input().split())
 
r = 0
if (x + y) % 3 == 0:
  a, b = (2 * y - x) // 3, (2 * x - y ) // 3
  if a >= 0 <= b:
    f = [1]
    for i in range(1, a + b + 1):
      f.append(f[-1] * i % M)
    r = f[a + b] * pow(f[a] * f[b], M - 2, M) % M
print(r)

Solution (Python):
M = 10 ** 9 + 7

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

r = 0
if (x + y) % 3 == 0:
  m = (x + y ) // 3
  a, b = (2 * y - x) // 3, (2 * x - y ) // 3
  if x >= m <= y:
    f = [1]
    for i in range(1, m + 1):
      f.append(f[-1] * i % M)
    r = f[m] * pow(f[a] * f[b], M - 2, M) % M
print(r)

Solution (Python):
from math import factorial
X,Y = map(int,input().split())
if (X + Y) % 3 != 0:
  print(0)
  exit()
MOD = 10**9 + 7
i2 = (2 * X - Y) // 3
i1 = X - 2 * i2
if i1 < 0 or i2 < 0:
  print(0)
  exit()
  
def prepare(n, MOD):
 
    
    f = 1
    factorials = [1]  
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    
    inv = pow(f, MOD - 2, MOD)
    
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
     
    return factorials, invs

factorials, invs = prepare(i1+i2,MOD)
print(factorials[i1+i2] * invs[i1] % MOD * invs[i2] % MOD)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
import math

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

X, Y = map(int, input().split())
x = (-X+2*Y)/3
y = (2*X-Y)/3
if x.is_integer() and y.is_integer() and x>=0 and y>=0:
    x = int(x)
    y = int(y)
    maximam = max(x,y)
    minimam = min(x,y)
    mod = 10**9+7 
    N = x+y
    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 )
    print(cmb(x+y,minimam,mod))
else:
    print(0)

Solution (Python):
x, y = map(int, input().split())
z = x + y
mod = 10 ** 9 + 7
if z % 3 == 0:
  w = z // 3
  a, b = sorted([x - w, y - w])
  if a < 0:
    print(0)
  elif a == 0:
    print(1)
  else:
    inv_mod = [1] * (a + 1)
    num = b + 1
    den = 1
    for i in range(2, a + 1):
      num = num * (b + i) % mod
      inv_mod[i] = -inv_mod[mod % i] * (mod // i) % mod
      den = den * inv_mod[i] % mod
    print(num * den % mod)
else:
  print(0)

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

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


def comb(n, r, mod):
    if r < 0 or n < r:
        return 0
    return fact[n]*pow(fact[r], mod-2, mod) * pow(fact[n-r], mod-2, mod) % mod


if (X + Y) % 3 != 0:
    ans = 0
else:
    n = (X + Y) // 3
    r = min(X, Y)-n
    
    fact = [1, 1]
    for i in range(2, n+1):
        fact.append(fact[-1] * i % mod)
    ans = comb(n, r, mod)

print(ans)