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
sys.setrecursionlimit(10**7)
import math

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

z = 10**9+7

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)
    print(cmb(x+y,minimam)%z)
else:
    print(0)

Solution (Python):
import math
import sys
sys.setrecursionlimit(10000)

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

if (X + Y) % 3 != 0:
    print(0)
else:
    N_r = int((2*X-Y) / 3)
    N_u = int((2*Y-X) / 3)

    if N_u < 0 or N_r < 0:
        print(0)
    elif N_u == 0 or N_r == 0:
        print(1)
    else:

        mod = 1000000007
        ans = 0

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

        def mul(a, b):
            return ((a % mod) * (b % mod)) % mod

        def div(a, b):
            return mul(a, power(b, mod-2))


        factorial = [1]
        for n in range(N_r + N_u-1):
            factorial.append(factorial[n]*(n+2)%mod)

        inverse_factorial = [0] * (N_u + N_r)
        inverse_factorial[-1] = power(factorial[-1], mod-2)

        for n in range(N_u+N_r-2, -1, -1):
            inverse_factorial[n] = (inverse_factorial[n+1] * (n+2)) % mod

        def combi(n, m):
            return factorial[n-1] * inverse_factorial[m-1] * inverse_factorial[n-m-1] % mod

        print(int(combi(N_u+N_r, N_u)))

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

_x, _y = map(int, input().split())


def nCk(n, k, m=10 ** 9 + 7):
    a = b = 1
    k = min(k, n - k)
    for i in range(k):
        a = (a * (n - i)) % m
        b = (b * (i + 1)) % m

    return (a * pow(b, m - 2, m)) % m


def chker(x, y):
    if (x + y) % 3 != 0 or x > 2 * y or y > 2 * x:
        print(0)
        return

    if x == y:
        b = x // 3
        print(nCk(2 * b, b))
        return

    x, y = (x, y) if x < y else (y, x)

    b = 2 * x - y
    if b % 3 != 0:
        print(0)
        return

    d1, d2 = b // 3, x - 2 * (b // 3)
    u = d1 + d2

    print(nCk(u, d1))


chker(_x, _y)

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().strip().split())
if (X + Y) % 3 != 0:
    print(0)
    exit(0)

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

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

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

Solution (Python):
MOD = 10**9 + 7
def comb(n, k, MOD):
    if n < k or n < 0 or k < 0:
        return 0
    
    if k == 0:
        return 1
    iinv = [1] * (k + 1)
    ans = n
    for i in range(2, k + 1):
        iinv[i] = MOD - iinv[MOD % i] * (MOD // i) % MOD
        ans *= (n + 1 - i) * iinv[i] % MOD
        ans %= MOD
    return ans
X, Y = map(int,input().split())
if (X+Y) % 3 != 0:
    print(0)
    exit()
n = (2*Y-X)//3
m = (2*X-Y)//3
print(comb(n+m,m,MOD))

Solution (Python):
def comb(n, k, MOD):
    if n < k or n < 0 or k < 0:
        return 0
    k = min(k, n - k)
    if k == 0:
        return 1
    iinv = [1] * (k + 1)
    ans = n
    for i in range(2, k + 1):
        iinv[i] = MOD - iinv[MOD % i] * (MOD // i) % MOD
        ans *= (n + 1 - i) * iinv[i] % MOD
        ans %= MOD
    return ans

mm = 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, beta, mm)
print(ans)

Solution (Python):
import math
tmp = input().split()
X = int(tmp[0])
Y = int(tmp[1])

mod = 10 ** 9 + 7

if (X + Y) % 3 != 0:
    print(0)
else:
    n = int((2 * Y - X) / 3)
    m = int(Y - 2 * n)
    if n < 0 or m < 0:
        print(0)
        exit()
    n %= mod
    m %= mod

    r = n
    n = n+m
    
    
    
    
    
    

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

    
    print(result % mod)

Solution (Python):
INF = 1000000000+7

x,y = map(int, input().split())
a = 2*x-y
b = -x + 2*y
if a%3!=0 or b%3!=0:
    print(0)
    exit()

n = a//3
m = b//3
nm = n+m

def cmb(n,r):
    if r > n:
        return 0
    if n - r < r:
         r = n - r
    if r > n:
        return 0
    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 n<0 or m<0:
    print(0)
    exit()
ans = cmb(nm,n)
print(ans%INF)

Solution (Python):
import sys 
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10**9 + 7

def calc_combination(n, k, MOD):
    if k > n - k:
        k = n - k
    ans = 1
    for i in range(1, k + 1):
        ans *= n + 1 - i
        ans *= pow(i, MOD - 2, MOD)
        ans %= MOD
    return ans
 
 
x, y = map(int, input().split())
m = 2 * y - x
n = 2 * x - y
 
if m < 0 or n < 0:
    ans = 0
elif m % 3 != 0 or n % 3 != 0:
    ans = 0
else:
    m //= 3
    n //= 3
    ans = calc_combination(m + n, n, MOD)

print(ans)

Solution (Python):
MOD = 10**9 + 7
def comb(n, k, MOD):
    if n < k or n < 0 or k < 0:
        return 0
    k = min(k, n - k)
    if k == 0:
        return 1
    iinv = [1] * (k + 1)
    ans = n
    for i in range(2, k + 1):
        iinv[i] = MOD - iinv[MOD % i] * (MOD // i) % MOD
        ans *= (n + 1 - i) * iinv[i] % MOD
        ans %= MOD
    return ans
X, Y = map(int,input().split())
if (X+Y) % 3 != 0:
    print(0)
    exit()
n = (2*Y-X)//3
m = (2*X-Y)//3
print(comb(n+m,m,MOD))

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


def comb(n, k, MOD):
    if n < k or n < 0 or k < 0:
        return 0
    k = min(k, n - k)
    if k == 0:
        return 1
    iinv = [1] * (k + 1)
    ans = n
    for i in range(2, k + 1):
        iinv[i] = MOD - iinv[MOD % i] * (MOD // i) % MOD
        ans *= (n + 1 - i) * iinv[i] % MOD
        ans %= MOD
    return ans


x, y = map(int, input().split())
a = 2 * y - x
b = 2 * x - y

if a < 0 or b < 0:
    ans = 0
elif a % 3 != 0 or b % 3 != 0:
    ans = 0
else:
    a //= 3
    b //= 3
    
    ans = comb(a + b, a, MOD)

print(ans)

Solution (Python):

X,Y = map(int,input().split())
b = (2*X -Y) / 3
if b != int(b):
    print(0)
    quit()
b = int(b)
a = X - 2 * b
if a < 0 or b < 0:
    print(0)
    quit()


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

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


ans = combination(a+b,a)

print(ans)

Solution (Python):
x,y=map(int,input().split())
MOD=10**9+7
ans=0
if y>x:
    x,y=y,x
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 = MOD 
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 )

for a in range(1,x//2+1):
    b=x-2*a
    if 2*b+a==y:
        tmp=cmb(b+a,a,mod)%MOD
        ans+=tmp
        ans%=MOD
print(ans)

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

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

MOD = 10 ** 9 + 7

import functools
import math

def mpow(x, n):
    result = 1
    while n > 0:
        if (n & 1):
            result = (result * x) % MOD
        x = (x * x) % MOD
        n >>= 1
    return result


def solver_not_good():
    factorial = [0] * (10 ** 6 + 1)
    inverse = [0] * (10 ** 6 + 1)
    factorial[0] = 1
    inverse[0] = 1
    for i in range(1, 10 ** 6 + 1):
        factorial[i] = (i * factorial[i-1]) % MOD
        inverse[i] = mpow(factorial[i], MOD - 2) % MOD

    total = 0
    twopath_maxnum = x // 2
    for twopath_num in range(twopath_maxnum + 1):
        onepath_num = x - twopath_num * 2
        if (onepath_num * 2 + twopath_num * 1) == y:
            a = factorial[onepath_num + twopath_num]
            b = inverse[onepath_num]
            c = inverse[twopath_num]
            total += (a * b * c) % MOD
    return total

def solver_better():
    
    factorial = [0] * (10 ** 6 + 1)
    factorial[0] = 1
    for i in range(1, 10 ** 6 + 1):
        factorial[i] = (i * factorial[i-1]) % MOD

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

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

    
    if a < 0 or b < 0:
        return 0

    return (factorial[a+b] * (mpow(factorial[a], MOD - 2) % MOD) * (mpow(factorial[b], MOD -2) % MOD)) % MOD


solver = solver_better
print(solver())

Solution (Python):
def nCk(n, k, m=10**9+7):
    a = b = 1
    for i in range(k):
        a = (a * (n-i)) % m
        b = (b * (i+1)) % m
    return (a * pow(b, m-2, m)) % m

x,y = map(int,input().split())
if (x+y)%3 != 0 or min(x,y)*2 < max(x,y):
    ans = 0
else:

    n = (x+y)//3 + 1
    k = x - (n-1) + 1
    ans = nCk(n-1,k-1)
print(ans)

Solution (Python):
p = 10 ** 9 + 7
 
def mod_inverse(a, p):
    mod = 1
    p_bin = bin(p-2)
    for i in range(len(p_bin) - 1, 1, -1):
        if i == len(p_bin) - 1:
            mul = a
            mul %= p
            mod *= (mul ** int(p_bin[i]))
            mod %= p
        else:
            mul = mul ** 2
            mul %= p
            mod *= (mul ** int(p_bin[i]))
            mod %= p
    return mod
 
def combination(n, k, p):
    a = 1
    for i in range(n, n - k, -1):
        a *= i
        a %= p
    b = 1
    for i in range(k):
        b *= (i + 1)
        b %= p
    return (a * mod_inverse(b, p)) % p

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

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

Solution (Python):
x,y=[int(j) for j in input().split()]
U = 2*10**6
MOD = 10**9+7
 
fact = [1]*(U+1)
fact_inv = [1]*(U+1)
 
for i in range(1,U+1):
  fact[i] = (fact[i-1]*i)%MOD
fact_inv[U] = pow(fact[U],MOD-2,MOD)
 
for i in range(U,0,-1):
	fact_inv[i-1] = (fact_inv[i]*i)%MOD
 
def comb(n,k):
  if k < 0 or k > n:
    return 0
  x = fact[n]
  x *= fact_inv[k]
  x %= MOD
  x *= fact_inv[n-k]
  x %= MOD
  return x

if (2*y-x)//3==(2*y-x)/3 and (2*x-y)//3==(2*x-y)/3:
    i=(2*y-x)//3
    j=(2*x-y)//3
    print(comb(i+j,min(i,j)))
else:
    print(0)

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

sys.setrecursionlimit(10 ** 7)

MOD = 10**9 + 7

def calc_combination(n, k, MOD):
    if k > n - k:
        k = n - k
    ans = 1
    for i in range(1, k + 1):
        ans *= n + 1 - i
        ans *= pow(i, MOD - 2, MOD)
        ans %= MOD
    return ans
 
 
x, y = map(int, input().split())
m = 2 * y - x
n = 2 * x - y
 
if m < 0 or n < 0:
    ans = 0
elif m % 3 != 0 or n % 3 != 0:
    ans = 0
else:
    m //= 3
    n //= 3
    ans = calc_combination(m + n, n, MOD)

print(ans)

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



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



def mod_pow(a, p, mod):
    if p == 0:
        return 1
    elif p % 2 == 1:
        return a * mod_pow(a, p - 1, mod)
    else:
        return (mod_pow(a, p // 2, mod) % mod) ** 2 % mod



def mod_inverse(a, mod):
    return mod_pow(a, (mod - 2), mod)


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(mod_cmb(n + m, n, mod))

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

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

MOD = 10 ** 9 + 7

import functools
import math

def mpow(x, n):
    result = 1
    while n > 0:
        if (n & 1):
            result = (result * x) % MOD
        x = (x * x) % MOD
        n >>= 1
    return result


def solver_not_good():
    factorial = [0] * (10 ** 6 + 1)
    inverse = [0] * (10 ** 6 + 1)
    factorial[0] = 1
    inverse[0] = 1
    for i in range(1, 10 ** 6 + 1):
        factorial[i] = (i * factorial[i-1]) % MOD
        inverse[i] = mpow(factorial[i], MOD - 2) % MOD

    total = 0
    twopath_maxnum = x // 2
    for twopath_num in range(twopath_maxnum + 1):
        onepath_num = x - twopath_num * 2
        if (onepath_num * 2 + twopath_num * 1) == y:
            a = factorial[onepath_num + twopath_num]
            b = inverse[onepath_num]
            c = inverse[twopath_num]
            total += (a * b * c) % MOD
    return total

def solver_better():
    
    factorial = [0] * (10 ** 6 + 1)
    factorial[0] = 1
    for i in range(1, 10 ** 6 + 1):
        factorial[i] = (i * factorial[i-1]) % MOD

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

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

    
    if a < 0 or b < 0:
        return 0

    return (factorial[a+b] * (mpow(factorial[a], MOD - 2) % MOD) * (mpow(factorial[b], MOD -2) % MOD)) % MOD

solver = solver_not_good

print(solver())

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

def cumprod(arr,MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq)
    for n in range(1,Lsq):
        arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD
    for n in range(1,Lsq):
        arr[n] *= arr[n-1,-1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U,MOD):
    x = np.arange(U,dtype=np.int64); x[0] = 1
    fact = cumprod(x,MOD)
    x = np.arange(U,0,-1,dtype=np.int64); x[0] = pow(int(fact[-1]),MOD-2,MOD)
    fact_inv = cumprod(x,MOD)[::-1]
    return fact,fact_inv



U = 10**6
fact, fact_inv = make_fact(U,MOD)

X, Y = map(int, input().split())
if (X + Y) % 3 != 0:
    print(0)
    exit()
time = (X+Y) // 3
diff = X - Y
if abs(diff) > time:
    print(0)
    exit()
x_time = (time+diff) // 2

answer = fact[time] * fact_inv[x_time] % MOD * fact_inv[time-x_time] % MOD
print(answer)

Solution (Python):

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

def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a//b, b, a%b
        x0, x1 = x1, x0 -q*x1
        y0, y1 = y1, y0 -q*y1
    return a, x0, y0

def modinv(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x %m

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

    divider = 10**9 + 7

    m = 2 * X - Y
    n = 2 * Y - X

    if m%3 != 0 or n%3 != 0 or n < 0 or m < 0:
        print(0)
        return
    m //= 3
    n //= 3

    if m < n:
        m, n = n, m

    ans = 1
    for i in range(m+n, m, -1):
        ans *= i
        ans %= divider

    for i in range(n, 0, -1):
        ans *= modinv(i, divider)
        ans %= divider

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):


import sys
from pprint import pprint

import math

def nCr(n, r):
    v = 10**9 + 7
    return math.factorial(n) // math.factorial(r) // math.factorial(n - r)

from operator import mul
from functools import reduce

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

def cmb_mod(n, r, mod):
    r = min(n - r, r)
    if r == 0:
        return 1
    else:
        denominator = 1
        for i in range(n, n - r, -1):
            denominator = (denominator * i) % mod
        numerator = 1
        for i in range(1, r + 1):
            numerator = (numerator * i) % mod
        return denominator * pow(numerator, mod - 2, mod) % mod 

x, y = map(int, sys.stdin.readline().strip().split(" "))

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

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

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



print(cmb_mod(n + m, n, 10**9 + 7))

Solution (Python):
from operator import mul
from functools import reduce

X,Y = map(int,input().split()[:2])

if (X + Y) % 3 or not (X <= 2*Y and Y <= 2*X):
    print(0)
    exit()

x_tmp=X+1
y_tmp=Y+2

x_count=0
y_count=0

while x_tmp*2 != y_tmp:
    x_tmp-=2
    y_tmp-=1
    x_count+=1

y_count = y_tmp//2 - 1

sum = x_count + y_count

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 
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 

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

ret = cmb(sum,x_count,mod)
print(ret)

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):
    if a%m!=0:
      x = extgcd(a,m)[0]
      return (m+x%m)%m
      


def c_mod(n,k,m=10**9+7):
  res = 1
  for i in range(1,n+1):
      if (i%m)!=0:
        res = res*i%m
      else:
        res=res/i%m
  for i in range(1,k+1):
      if (i%m)!=0:
        res = res*mod_inv(i,m)%m
      else:
        res=res/i%m
  for i in range(1,n-k+1):
      if (i%m)!=0:
        res = res*mod_inv(i,m)%m
      else:
        res=res/i%m
  return res


x,y=map(int,input().split())
if (2*x-y)%3==0 and (2*y-x)%3==0 and (2*y-x)>=0 and (2*x-y)>=0 :
  l=int((2*x-y)/3)
  k=int((2*y-x)/3)
  ans=c_mod(l+k,k)

else:
  ans=0
print(ans)

Solution (Python):
def c_mod(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 (2*x-y)%3==0 and (2*y-x)%3==0 and (2*x-y)>=0 and (2*y-x)>=0 :
  l=int((2*x-y)/3)
  k=int((2*y-x)/3)
  ans=c_mod(l+k,k)

else:
  ans=0
print(ans)

Solution (Python):
from operator import mul
from functools import reduce
import sys
x,y = map(float,input().split())
m = (2*x - y)/3
n = (y - m)/2
if not (m.is_integer() and n.is_integer()):
  print(0)
  sys.exit()
if n < 0 or m < 0:
  print(0)
  sys.exit()
m = int(m)
n = int(n)
mod = 10**9 + 7
def cmb(h, r):
    if h - r < r: r = h - r
    if r == 0: return 1
    if r == 1: return h
    r = int(r)

    humerator = [h - r + k + 1 for k in range(r)]
    dehominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = dehominator[p - 1]
        if pivot > 1:
            offset = (h - r) % p
            for k in range(p-1,r,p):
                k = int(k)
                offset = int(offset)
                humerator[k - offset] /= pivot
                dehominator[k] /= pivot

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

    return result
if (x + y) % 3 != 0:
  print(0)
else:
  print(cmb(n + m,n)%mod)

Solution (Python):
X,Y = list(map(int,input().split()))
def nCk(n, k, mod=10 ** 9 + 7):
    if n < k:
        return 0
    k = min(k, n - k)
    numer = 1
    for x in range(n - k + 1, n + 1):
        numer = (numer * x) % mod
    denom = 1
    for x in range(1, k + 1):
        denom = (denom * x) % mod
    return numer * pow(denom, mod - 2, mod) % mod
if (X+Y)%3!=0:
    print(0)
else:
    n = (2*Y - X)//3
    m = (2*X - Y)//3
    if n<0 or m<0:
        print(0)
    else:
        print(nCk(n+m, m)%(7+10**9))

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

else:
  c2 = Y-2*c1
  c3 = c1+c2+1
  mod = 10**9+7
  c4 = min(c1,c2)
  x = 1
  y = 1
  for i in range(1,c4+1):
    x = x * (c3-i) % mod
    y = y * i % mod
    
  result = x * pow(y,mod-2,mod) % mod
 
  print(result)

Solution (Python):
from functools import reduce

def main():
    mod = 10 ** 9 + 7

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

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

    if (X + Y) % 3 != 0 or a < 0 or b < 0:
        print(0)
        quit(0)

    n, k = a + b, a
    k = min(k, n - k)
    mod_mul = lambda a, b: a * b % mod
    numer = reduce(mod_mul, range(n - k + 1, n + 1), 1)
    denom = reduce(mod_mul, range(1, k + 1), 1)

    print(numer * pow(denom, mod - 2, mod) % mod)

main()

Solution (Python):
from functools import reduce

def main():
    def nCk(n, k, mod=10 ** 9 + 7):
        if n < k:
            return 0
        k = min(k, n - k)
        mod_mul = lambda a, b: a * b % mod
        numer = reduce(mod_mul, range(n - k + 1, n + 1), 1)
        denom = reduce(mod_mul, range(1, k + 1), 1)
        return numer * pow(denom, mod - 2, mod) % mod

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

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

    if (X + Y) % 3 != 0 or a < 0 or b < 0:
        print(0)
    else:
        print(nCk(a + b, a))

main()

Solution (Python):
from functools import reduce

def nCk(n, k, mod=10 ** 9 + 7):
    if n < k:
        return 0
    k = min(k, n - k)
    mod_mul = lambda a, b: a * b % mod
    numer = reduce(mod_mul, range(n - k + 1, n + 1), 1)
    denom = reduce(mod_mul, range(1, k + 1), 1)
    return numer * pow(denom, mod - 2, mod) % mod

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

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

if (X + Y) % 3 != 0 or a < 0 or b < 0:
    print(0)
else:
    print(nCk(a + b, a))

Solution (Python):

import numpy as np
import math

def cmb(n, k, mod):
    numerator = 1
    for i in range(k):
        numerator = (numerator * (n-i)) % mod
 
    denominator = 1
    for i in range(1, k+1):
        denominator = (denominator * i) % mod
 
    return (numerator * pow(denominator, mod-2, mod)) % mod
    
    return int((n_frac * n_r_ifrac * r_ifrac) % mod)

mod = 10**9+7
x,y = map(int, input().split()) 
is_ok = True
b = (2 * x - y) / 3
a = x - 2 * b
if not (a.is_integer() and b.is_integer()):
    ans = 0
    is_ok = False
    
a = int(a)
b = int(b)
if a < 0 or b < 0:
    ans = 0
    is_ok = False
if is_ok:    
    ans = cmb(a+b, a, mod)
print(ans)

Solution (Python):
def nCk(n, k, mod=10 ** 9 + 7):
    if n < k:
        return 0
    k = min(k, n - k)
    numer = 1
    for x in range(n - k + 1, n + 1):
        numer = (numer * x) % mod
    denom = 1
    for x in range(1, k + 1):
        denom = (denom * x) % mod
    return numer * pow(denom, mod - 2, mod) % mod

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

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

if (X + Y) % 3 != 0 or a < 0 or b < 0:
    print(0)
else:
    print(nCk(a + b, a))

Solution (Python):


from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

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

def calc(x,y):
    if (x*2-y)%3 != 0 or (y*2-x)%3 != 0: return 0
    m = (x*2-y)//3
    n = (y*2-x)//3
    if m < 0 or n < 0:return 0
    
    return cmb(m+n,m,mod)

DEBUG = 0

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 = list(map(int, input().split()))

if DEBUG:print(X,Y)

print(calc(X,Y))

Solution (Python):
from numpy.linalg import solve


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


MOD = 10 ** 9 + 7
MAX = 10**6
x, y = map(int, input().split())
if (x + y) % 3 != 0:
    print(0)
    exit(0)
l = solve([[2, 1], [1, 2]], [x, y])
if l[0] < 0 or l[1] < 0:
    print(0)
    exit(0)
print(COM(int(l[0]+l[1]), int(l[0])))

Solution (Python):
import numpy as np
from operator import mul
from functools import reduce

MOD = 10**9+7
def calc(a,b=MOD-2):
    if b==0: return 1
    elif b%2==0: return (calc(a,b/2)**2)%MOD
    else: return (a*calc(a,b-1))%MOD

X,Y = list(map(int,input().split()))
i, j = (X-2*Y), (-2*X+Y)

if i%3==0 and i<=0 and j%3==0 and j<=0:
    i,j = -i//3,-j//3
    i,j = min(i,j), max(i,j)
    n,m = 1,1
    for k in range(i): n = (n * (i+j-k))%MOD
    m = calc(np.math.factorial(i))%MOD
    print((n*m)%MOD)
else: print(0)

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)

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

if not float.is_integer(a) or not float.is_integer(b):
    print(0)
    exit()

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

Solution (Python):
import math

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

def cmb(n, k,MOD):
    if k > n - k:
        k = n - k
    ans = 1
    for i in range(1, k + 1):
        ans *= n + 1 - i
        ans *= pow(i, MOD - 2, MOD)
        ans %= MOD
    return ans

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

Solution (Python):
def main():
    def nCk(n, k, mod=10 ** 9 + 7):
        def xgcd(a, b):
            if b == 0:
                return (1, 0)
            x, y = xgcd(b, a % b)
            return (y, x - (a // b) * y)
        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 p * (xgcd(q, mod)[0] % mod) % mod


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

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

    if (X + Y) % 3 != 0 or a < 0 or b < 0:
        print(0)
    else:
        print(nCk(a + b, a))

main()

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

def fact(n):
    ans = 1
    for i in range(2,n+1):
        ans *= i
        ans %= mod
    return ans
    
X,Y = list(map(int,input().split()))







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

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


print(fact(int(a+b)) * pow(fact(int(a)),mod-2,mod) * pow(fact(int(b)),mod-2,mod) % mod)

Solution (Python):

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

aa=int(0)
x=max([a, b])
y=min([a, b])

if (x+y)%3 ==0 and x <= y*2:
    aa=int(1)

n=int((x+y)/3)
k=int((n-(x-y))/2)


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

if aa==1:
    print(comb(n,k))
else:
    print(aa)

Solution (Python):
def nCk(n, k, mod=10 ** 9 + 7):
    def xgcd(a, b):
        if b == 0:
            return (a, 1, 0)
        g, x, y = xgcd(b, a % b)
        return (g, y, x - (a // b) * y)
    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 p * (xgcd(q, mod)[1] % mod) % mod


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

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

if (X + Y) % 3 != 0 or a < 0 or b < 0:
    print(0)
else:
    print(nCk(a + b, a))

Solution (Python):



import math
import itertools


def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
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

def main():

    x,y = map(int,input().split())
    a=max(x,y)
    b=min(x,y)

    t=a//2
    if b<t:
       ans=0
    elif (b-t)%3==0 and a%2==0:
       aa=a//2
       bb=(b-t)//3*2
       aa-=(b-t)//3
       ans=1
       ans=cmb(aa+bb,bb)

    elif (b-t+1)%3==0 and a%2!=0:
       aa=a//2
       bb=(b-t)//3*2+1
       aa -= (b - t) // 3
       ans=1
       ans=cmb(aa+bb,bb)

    else:
        ans=0



    print(ans%(10**9+7))
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


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

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

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

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

n = a + b
r = min(a, b)

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

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

print(cmb(n, r, mod))