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.



Sample input:
3 3


Sample output:
2

Submission status: Runtime Error

Solution code:
import math
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:
    a = x-(x+y)//3
    b = y-(x+y)//3
    print(cmb(a+b,a,inf))

Submission status: Runtime Error

Solution code:
from scipy.special import comb
import math
x, y = map(int, input().split())
if x * 2 < y or y * 2 < x:
    print("0")
elif (x + y) % 3 != 0:
    print("0")
else:
    x1 = x - (x + y)/3
    y1 = y - (x + y)/3
    root = comb(x1+y1, x1)
    answer = root % (10**9 + 7)
    print(answer)

Submission status: Runtime Error

Solution code:
import numpy as np
mod = 1000000007
X,Y = map(int, input().split())

def comb(N,R):
    if R == 1:
        return N
    else:
        return N/R * comb(N-1,R-1)

if (X + Y) % 3:
    print(0)
else:
    left = [[1,2],
            [2,1]]
    right = [X,Y]
    n,m = np.linalg.solve(left,right)
    print(n)
    print(m)
    n = int(n)
    m = int(m)
    ans = comb(n+m,n) % mod
    print(ans)

Submission status: Runtime Error

Solution code:
import math
x,y = map(int,input().split())
sum_xy = x+y

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 sum_xy%3!=0:
    print(0)
else:
    ab_sum = sum_xy//3
    a = x-ab_sum
    b = y-ab_sum
    print(cmb(ab_sum,a,inf))

Submission status: Runtime Error

Solution code:
from scipy.special import comb
x,y = map(float,input().split())
m = (2*x - y)/3
n = (y - m)/2
if (x + y) % 3 != 0:
  print(0)
else:
  print(comb(n + m,n,exact=True)%(10**9+7))

Submission status: Runtime Error

Solution code:
import math
mod = (10**9) + 7

def kaijo(n):
    result = 1
    for i in reversed(range(1,n+1)):
        result = (result * i) % mod
    return result
def combinations_count(n, r):
    return (kaijo(n)) //(kaijo(n - r) * kaijo(r))

def nCk(n, k):
    ret = 1
    mo = 10**9 + 7
    for i in range(k):
        ret = ret * (n - i) * pow(i + 1, mo - 2, mo) % mo
    return ret


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

if (X+Y)%3 == 0:
    flag = False
    if X > Y:
        diff = X - Y
        all_num = (X+Y)//3
        count_2 = (X+Y)//2 - diff//2 - all_num
        if X > Y*2:
            flag = True
    elif Y > X:
        diff = Y - X
        all_num = (X+Y)//3
        count_2 = (X+Y)//2 - diff//2　- all_num
        if Y > X*2:
            flag = True
    else:
        all_num = (X+Y)//3
        count_2 = all_num//2
    if flag:
        print(0)
    else:
        print(nCk(all_num,count_2))
else:
    print(0)

Submission status: Runtime Error

Solution code:
from math import factorial
import math
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
mod = 10**9+7 #出力の制限

X,Y = map(int,input().split())
n = (2*Y-X)//3
m = (2*X-Y)//3
if (X+Y) % 3 == 0　and n>0 and m >0:
    ans = cmb(n+m,n)%mod
else:
    ans = 0
print(ans)

Submission status: Runtime Error

Solution code:
import sys

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

MOD = 10**9 + 7

i = x
j = y
if (2*max(x,y) - min(x,y)) % 3 != 0:
	print(0)
	sys.exit()

c1 = (2*max(x,y) - min(x,y)) // 3
c2 = max(x,y) - 2*c1

c1 %= MOD
c2 %= MOD

ans = cmb(c1 + c2 , c1,MOD)
print(ans%MOD)

Submission status: Runtime Error

Solution code:
from math import factorial
import math
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
mod = 10**9+7 #出力の制限

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

Submission status: Runtime Error

Solution code:
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



X, Y = (int(i) for i in input().split())
if (X+Y) % 3 == 0 and max(X,Y) - min(X,Y) <= min(X,Y):
    print(cmb(X+Y/3, abs(X-Y)) % (10**9+7))
else:
    print(0)

Submission status: Runtime Error

Solution code:
from numpy.linalg import solve
def cmb(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:
  print(0)
else:
  left = [[1, 2],
          [2, 1]]
  right = [x, y]
  n, m = map(int, solve(left, right))
  c = cmb(n+m, n)
print(c)

Submission status: Runtime Error

Solution code:
from sympy import Symbol, solve
x = Symbol('x')
y = Symbol('y')

tmp1 = 2*x + 3*y - 6
tmp2 = 3*x + 2*y - 12

ans = solve((tmp1, tmp2))
print(ans[x])
print(x)

Submission status: Runtime Error

Solution code:
import itertools

X, Y = (int(i) for i in input().split())
if (X+Y) % 3 == 0 and max(X,Y) - min(X,Y) <= min(X,Y):
    print(len(list(itertools.combinations(range((X+Y)/3), abs(X-Y)))) % (10**9+7))
else:
    print(0)

Submission status: Runtime Error

Solution code:
from math import factorial
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

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

if m.is_integer() and n.is_integer() and m >= 0 and n >= 0:
    m = int(m)
    n = int(n)
    if m >= n:
        a = int(cmb(m+n,m+1)/factorial(n))
    else:
        a = int(cmb(m+n,n+1)/factorial(m))
    print(a%(10**9+7))
    
else:print(0)

Submission status: Runtime Error

Solution code:
from functools import reduce
from operator import mul
import sys
from scipy import special


input = sys.stdin.readline

# 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


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

BIG_NUM = 1000000007


if (X + Y) % 3 != 0:
    print(0)
else:
    n = (X - Y * 2) / (-3)
    m = ((2 * X - Y) / 3)
    if n < 0 or m < 0:
        print(0)
    else:
        # print("n:{0},m:{1}".format(n,m))
        print(scipy.special.comb(n + m, n, exact=False) % BIG_NUM)

Submission status: Runtime Error

Solution code:
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


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

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

if(  n < 0 or m < 0  ):
    print(0)
else:
    print(cmb(i+j, i) % 1000000007)

Submission status: Runtime Error

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

if X > Y:
    ma = X
    mi = Y
else:
    ma = Y
    mi = X

for i in range(mi):
    a = X - 3*i
    b = Y - 3*i
    if a == 2 * b or b == 2 * a:
        import math
        pattern = math.factorial(2*i + ((ma - 3*i)//2)) // (i * (i + ((ma - 3*i)//2)))
        print(pattern % (10**9 + 7))
        break
        
if a != 2 * b and b != 2 * a:
    print(0)

Submission status: Runtime Error

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

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

f=[1]
for i in range(1,a+b+100):
    f.append(f[-1]*i%mod)

if a+2*b==x and 2*a+b==y:
    print(f[a+b]*pow(f[a]*f[b],mod-2,mod)%mod)
else:
    print(0)

Submission status: Runtime Error

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

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

f=[1]
for i in range(1,a+b+1):
    f.append(f[-1]*i%mod)

if a+2*b==x and 2*a+b==y:
    print(f[a+b]*pow(f[a]*f[b],mod-2,mod)%mod)
else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys
G = list(map(int, input().split()))
total = G[0] + G[1]
if total % 3 != 0:
  print(0)
  sys.exit(0)

total //= 3
dis = G[0] - total

mod = 10 ** 9 + 7
fact = [1]
fact_inv = [1]
for i in range(10**6+4):
    new_fact = fact[-1]*(i+1)%mod
    fact.append(new_fact)
    fact_inv.append(pow(new_fact,mod-2,mod))

def mod_comb_k(n,k,mod):
    return fact[n] * fact_inv[k] % mod * fact_inv[n-k] %mod

print(comb_mod(total, min(G)-total, mod))

Submission status: Runtime Error

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

if X > Y:
    ma = X
    mi = Y
else:
    ma = Y
    mi = X

for i in range(mi):
    a = X - 3*i
    b = Y - 3*i
    if a == 2 * b or b == 2 * a:
        import math
        pattern = math.factorial(2*i + ((ma - 3*i)//2)) // (i * (i + ((ma - 3*i)//2)))
        print(pattern % (10**9 + 7))
        break

print((ma - 3*i)/2)
print((ma - 3*i)//2)

        
        
if a != 2 * b and b != 2 * a:
    print(0)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import math
x, y = map(int, input().split())
if (x + y) % 3 != 0:
    print(0)
else:
    m = (2 * x - y) // 3
    n = (2 * y - x) // 3
    print((math.factorial(m+n)/math.factorial(m)*math.factorial(n))%(10**9+7))

Submission status: Runtime Error

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

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

f=[1]
for i in range(1,a+b):
    f.append(f[-1]*i%mod)

if a+2*b==x and 2*a+b==y:
    print(f[a+b]*pow(f[a]*f[b],mod-2,mod)%mod)
else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys
G = list(map(int, input().split()))
total = G[0] + G[1]
if total % 3 != 0:
  print(0)
  sys.exit(0)

total //= 3
dis = G[0] - total

mod = 10 ** 9 + 7
fact = [1]
fact_inv = [1]
for i in range(10**6+4):
    new_fact = fact[-1]*(i+1)%mod
    fact.append(new_fact)
    fact_inv.append(pow(new_fact,mod-2,mod))

def mod_comb_k(n,k,mod):
    return fact[n] * fact_inv[k] % mod * fact_inv[n-k] %mod

print(comb_mod(total, min(G)-total))

Submission status: Runtime Error

Solution code:
import math

# 有理数型をインポート

# 組合せの総数
def calc(a, b, p):
    if (b == 0):
        return 1
    elif (b % 2 == 0):
        d = calc(a, b // 2, p)
        return (d * d) % p
    else:
        return (a * calc(a, b - 1, p)) % p

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

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

if(  ((2*X-Y)%3)!=0 or (2*Y-X)%3!=0  ):
    print(0)
else:
    print(calc(i+j, i, 1000000007))

Submission status: Runtime Error

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


X,Y = map(int,input().split())
s = (2*Y-X)//3
t = (2*X-Y)//3

if (2*Y-X)%3==0 and (2*X-Y)%3==0 :
    ans =  combination(s+t,t,mod)
    print(ans)
else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

INPUT = lambda: sys.stdin.readline().rstrip()
MAP = lambda: map(int, INPUT().split())

sys.setrecursionlimit(10 ** 9)
mod = 10 ** 9 + 7


def main():
    X, Y = MAP()
    if (X + Y) % 3 != 0:
        print(0)
        sys.exit()

    s = (2*X - Y)/3
    t = (2*Y - X)/3

    if s != int(s) or t != int(t) or s < 0 or t < 0:
        print(0)
        sys.exit()

    s = int(s)
    t = int(t)
    MAX_NUM = max(s, t)
    dp = np.zeros((s+1, t+1), np.int)
    for i in range(s+1):
        if i == 0:
            for j in range(t+1):
                if j == 0: dp[i][j] = 0
                else: dp[i][j] = 1
        else:
            for j in range(t+1):
                if j == 0: dp[i][j] = 1
                else: dp[i][j] = dp[i-1][j] + dp[i][j-1]

    print(dp[s][t] % mod)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import math
input = sys.stdin.readline


def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

BIG_NUM = 1000000007


if (X + Y) % 3 != 0:
    print(0)
else:
    n = (X - Y * 2) / (-3)
    m = ((2 * X - Y) / 3)
    # print("n:{0},m:{1}".format(n,m))
    print(combinations_count(n + m, n) % BIG_NUM)

Submission status: Runtime Error

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
#mint
class ModInt:
    def __init__(self, x):
        self.x = x % mod
    
    def __str__(self):
        return str(self.x)
    
    __repr__ = __str__

    def __add__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x + other.x)
        else:
            return ModInt(self.x + other)

    __radd__ = __add__
        
    def __sub__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x - other.x)
        else:
            return ModInt(self.x - other)

    def __rsub__(self, other):
        if isinstance(other, ModInt):
            return ModInt(other.x - self.x)
        else:
            return ModInt(other - self.x)

    def __mul__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * other.x)
        else:
            return ModInt(self.x * other)

    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * pow(other.x, mod-2,mod))
        else:
            return ModInt(self.x * pow(other, mod - 2, mod))
            
    def __rtruediv(self, other):
        if isinstance(other, self):
            return ModInt(other * pow(self.x, mod - 2, mod))
        else:
            return ModInt(other.x * pow(self.x, mod - 2, mod))
            

    def __pow__(self, other):
        if isinstance(other, ModInt):
            return ModInt(pow(self.x, other.x, mod))
        else:
            return ModInt(pow(self.x, other, mod))
            

    def __rpow__(self, other):
        if isinstance(other, ModInt):
            return ModInt(pow(other.x, self.x, mod))
        else:
            return ModInt(pow(other, self.x, mod))
class BigCombination(object):

    def __init__(self, mod: int=10**9+7, max_n: int=5*(10**6)):
        fac, inv=[1], []
        fac_append, inv_append=fac.append, inv.append

        for i in range(1, max_n+1):
            fac_append(fac[-1] * i % mod)

        inv_append(pow(fac[-1], mod-2, mod))

        for i in range(max_n, 0, -1):
            inv_append(inv[-1] * i % mod)

        self.mod, self.factorial, self.inverse=mod, fac, inv[::-1]

    def get_combination(self, n, r):
        if n < r:
            return 0
        return self.factorial[n] * self.inverse[r] * self.inverse[n-r] % self.mod

    def get_permutation(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0

        if n==r:
            return 1
        return self.factorial[n] * self.inverse[n-r] % self.mod
        

def main():
    
    X,Y = MI()
    if (X+Y)%3 != 0:
        print(0)
        return 
    S = X+Y
    X-=S//3
    Y-=S//3
    B = BigCombination()
    ans = B.get_combination(X+Y,X,mod)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math

x, y = (int(l) for l in input().split())
if (x+y) % 3 != 0:
    print(0)
else:
    a = (x+y)//3
    n = math.factorial(a)
    
    k = a // 2 + (abs(x-y) - (a+1) % 2 )
    ka = math.factorial(k)
    nk = math.factorial(a-k)
    print((n//(ka*nk) % 1000000007))

Submission status: Runtime Error

Solution code:
import math

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

if (X+Y) % 3 != 0:
    print(0)
else:
    m = (2*X - Y) / 3
    n = (2*Y - X) / 3
    if m < 0 or n < 0:
        print(0)
    else:
        ans = math.factorial(m+n)% (1e+7 + 7) / (math.factorial(m) * math.factorial(n))
        print(int(ans))

Submission status: Runtime Error

Solution code:
import math
x,y = map(int,input().split())
sum_xy = x+y

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 = 8*(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 sum_xy%3!=0:
    print(0)
else:
    ab_sum = sum_xy//3
    a = x-ab_sum
    b = y-ab_sum
    if a<0 or b<0:
        print(0)
    else:
        print(cmb(ab_sum,a,inf))

Submission status: Runtime Error

Solution code:
from scipy.special import comb
X,Y = map(int,input().split())
def comb(n,k,p):
  from math import factorial
  if n<0 or k<0 or n<k: return 0
  if n==0 or k==0: return 1
  a=factorial(n) %p
  b=factorial(k) %p
  c=factorial(n-k) %p
  return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p
def power_func(a,b,p):
  if b==0: return 1
  if b%2==0:
    d=power_func(a,b//2,p)
    return d*d %p
  if b%2==1:
    return (a*power_func(a,b-1,p ))%p
if (X+Y)%3 == 0:
    N = (X+Y)//3
    print(comb(N,X-N,10**9+7))
else:
    print(0)

Submission status: Runtime Error

Solution code:
import math

x, y = (int(l) for l in input().split())
if (x+y) % 3 != 0:
    print(0)
else:
    a = (x+y)//3
    n = int(math.factorial(a))
    
    k = a // 2 - (abs(x-y) - (a+1) % 2 )
    ka = int(math.factorial(k))
    nk = int(math.factorial(a-k))
    print((n//(ka*nk) % 1000000007))

Submission status: Runtime Error

Solution code:
import sys

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

if (-X + 2*Y) % 3 or (2*X - Y) % 3 or (-X + 2*Y) < 0 or (2*X - Y) < 0:
    print(0)
    sys.exit()

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

n=a+b

p1=1
p2=1
p3=1

for i in range(n):
    p1=p1*(i+1)%mod

for i in range(max(a,b)):
    p2=p2*(i+1)%mod

    if i==b-1:
        p3=p2

if a+2*b==x and 2*a+b==y:
    print(p1*pow(p2,mod-2,mod)*pow(p3,mod-2,mod)%mod)
else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

INPUT = lambda: sys.stdin.readline().rstrip()
MAP = lambda: map(int, INPUT().split())

sys.setrecursionlimit(10 ** 9)
mod = 10 ** 9 + 7


def main():
    X, Y = MAP()
    if (X + Y) % 3 != 0:
        print(0)
        sys.exit()

    s = (2*X - Y)/3
    t = (2*Y - X)/3
    if s != int(s) or t != int(t) or s < 0 or t < 0:
        print(0)
        sys.exit()

    s = int(s)
    t = int(t)
    MAX_NUM = max(s, t)
    dp = np.zeros((s+1, t+1), np.int)
    for i in range(MAX_NUM):
        if i == 0:
            for j in range(len(dp[0])):
                if j == 0: dp[i][j] = 0
                else: dp[i][j] = 1
        else:
            for j in range(len(dp[0]-1)):
                if j == 0: dp[i][j] = 1
                else: dp[i][j] = dp[i-1][j] + dp[i][j-1]

    print(dp[s][t] % mod)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math
x,y = map(int,input().split())
sum_xy = x+y

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 sum_xy%3!=0:
    print(0)
else:
    ab_sum = sum_xy//3
    a = x-ab_sum
    b = y-ab_sum
    if a<0 or b<0:
        print(0)
    else:
        print(cmb(ab_sum,a,inf))

Submission status: Runtime Error

Solution code:
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()
else:
    s = (x+y)//3
 
t = min((x-s),(y-s))

print(combination(s,t))