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: Wrong Answer

Solution code:
import sys
X,Y = map(int,input().split())
if (X+Y)%3 != 0:
    print(0)
    sys.exit()
A = Y - (X+Y)//3
B = X - (X+Y)//3
N = A+B
def cmb(n, r, mod):
    inv = [0,1]
    for i in range(2, N + 1):
        inv.append((-inv[mod % i] * (mod // i)) % mod)
    cmd = 1
    for i in range(1,min(r,n-r)+1):
        cmd = (cmd*(N-i+1)*inv[i])%mod
    return cmd
    
a = cmb(N,A,10**9+7)
print(a)

Submission status: Wrong Answer

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

mod = 10**9 + 7

if (x+y)%3 != 0:
    print(0)
    
else:
    
    n=(x+y)//3
    
    r = min(x-n,y-n)
    
    p=1
    q=1
    for i in range(r):
       
        p *= n-i
        p %= mod
        

        q *= i+1
        q %= mod
            
    ans = p * pow(q,mod-2,mod)
    ans %= mod
        
    print(ans)

Submission status: Wrong Answer

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

mod = 10**9 + 7

if (x+y)%3 != 0:
    print(0)
    
else:
    
    n=(x+y)//3
    
    r = min(x-n,y-n)
    
    p=1
    q=1
    for i in range(r):
       
        p *= n-i
        

        q *= (i+1)
            
    ans = p * pow(q,mod-2,mod)
    ans %= mod
        
    print(ans)

Submission status: Wrong Answer

Solution code:
#D
X,Y=map(int,input().split())
mod=10**9+7
N=10**6

fact=[1 for i in range(N+1)]
for i in range(1,N):
    fact[i+1]=(fact[i]*(i+1))%mod

def nCk(n,k):
    return fact[n]*pow(fact[n-k]*fact[k],mod-2,mod)

ans=0
if (2*Y-X)%3==0 and (2*X-Y)%3==0 and (2*Y-X)>0 and (2*X-Y)>0:
    cntx=(2*Y-X)//3
    cnty=(2*X-Y)//3
    
    ans=nCk(cntx+cnty,cntx)%mod

print(ans)

Submission status: Wrong Answer

Solution code:
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  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
 
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)
    
n,m = map(int, input().split(" "))

if (n+m)%3 != 0 or max(n,m) // min(n,m) > 2:
  print(0)
else:
  sa = max(n,m)-min(n,m)
  times = [((n+m)//3+sa), ((n+m)//3-sa)//2]
  print(cmb(times[0], times[1], p))

Submission status: Wrong Answer

Solution code:
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  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
 
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)
    
n,m = map(int, input().split(" "))

if (n+m)%3 != 0:
  print(0)
else:
  sa = max(n,m)-min(n,m)
  times = [((n+m)//3+sa), ((n+m)//3-sa)//2]
  print(cmb(times[0], times[1], p))

Submission status: Wrong Answer

Solution code:
def comb(n,k,p):
  """power_funcを用いて(nCk) mod 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):
  """a^b mod 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
n,m = map(int, input().split(" "))
mod = 10 ** 9 + 7

if (n+m)%3 != 0:
  print(0)
else:
  sa = max(n,m)-min(n,m)
  times = [((n+m)//3+sa), ((n+m)//3-sa)//2]
  print(comb(times[0], times[1], mod))

Submission status: Wrong Answer

Solution code:
p = 1000000007
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], p - 2, p) * pow(fac[k], p - 2, p) % p

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

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

if a.is_integer()==False or b.is_integer()==False or a<0 or b<0:
    print(0)
else:
    a=int(a)
    b=int(b)
    n = a + b
    fac = [0] * (n + 1)
    fac[0] = 1
    for i in range(n):
        fac[i + 1] = fac[i] * (i + 1) % p
        print(mcomb(n, a))
#他者の解答を参考にした

Submission status: Wrong Answer

Solution code:
p = 1000000007
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], p - 2, p) * pow(fac[k], p - 2, p) % p

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

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

if a.is_integer()==False or b.is_integer()==False:
    print(0)
else:
    a=int(a)
    b=int(b)
    n = a + b
    fac = [0] * (n + 1)
    fac[0] = 1
    for i in range(n):
        fac[i + 1] = fac[i] * (i + 1) % p
        print(mcomb(n, a))
#他者の解答を参考にした

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

# nCkを高速に求める
# https://drken1215.hatenablog.com/entry/2018/06/08/210000


class COM:
    def __init__(self, n, MOD):
        self.n = n
        self.MOD = MOD
        self.fac = [0] * (n+1)
        self.finv = [0] * (n+1)
        self.inv = [0] * (n+1)
        self.fac[0] = self.fac[1] = 1
        self.finv[0] = self.finv[1] = 1
        self.inv[1] = 1
        for i in range(2, n+1):
            self.fac[i] = self.fac[i-1] * i % MOD
            self.inv[i] = MOD - self.inv[MOD % i] * (MOD // i) % MOD
            self.finv[i] = self.finv[i-1] * self.inv[i] % MOD

    
    def calc(self, k):
        if k < 0:
            return 0
        return self.fac[self.n] * (self.finv[k] * self.finv[self.n-k] % self.MOD) % self.MOD


def main():
    MOD = 10**9+7
    X, Y = map(int,input().split())
    if (X+Y) % 3 == 0:
        z = (X+Y) // 3
        a_x = X-z
        a_y = Y-z
        if a_x > 0 and a_y > 0:
            com = COM(a_x+a_y, MOD)
            print(com.calc(a_x))
        else:
            print(0)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math
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
X, Y = map(int,input().split())
a=(-X+2*Y)/3
b=(2*X-Y)/3
if a.is_integer()==False or b.is_integer()==False:
    print(0)
else:
    print(combinations_count(int(a+b),int(b))%(pow(10,9)+7))

Submission status: Wrong Answer

Solution code:
import math
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
X, Y = map(int,input().split())
a=(-X+2*Y)/3
b=(2*X-Y)/3
if a.is_integer()==False or b.is_integer()==False:
    print(0)
else:
    print(combinations_count(int(a+b),int(b)))

Submission status: Wrong Answer

Solution code:
def comb(n, r, mod):
    if n < 0 or r < 0 or r > n: return 0
    nu = 1
    de = 1
    for i in range(r):
        nu = nu * (n-i) % mod
        de = de * (i+1) % mod
    return nu * inv(de, mod) % mod
 
def inv(a, mod):
    b = mod
    p, q = 1, 0
    while b > 0:
        c = a//b
        d = a; a = b; b = d % b
        d = p; p = q; q = d-c*q
    return p+mod if p < 0 else p

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

m = (2 * x -y) // 3
n = (2 * y -x) // 3
mod = 10**9+7
if n >= 0 and m >= 0:
  ans = comb(n+m, m, mod)
  print(ans)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
# import itertools
# import math
import sys
# sys.setrecursionlimit(500*500)
# import numpy as np
# import heapq
# from collections import deque

# N = int(input())
# S = input()
# n, *a = map(int, open(0))
X, Y = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

# 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

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

cnt = (X + Y) // 3

diff = abs(X - Y)



def comb(n, m, mod = 10 ** 9 + 7):
    numerator = 1
    denominator = 1
    for i in range(1, m + 1):
        numerator = numerator * (n - i + 1) % mod
        denominator = denominator * i % mod
    d_inv = pow(denominator, mod - 2, mod)
    return numerator * d_inv % mod

print(comb(cnt, (2 * Y - X) // 3) % (10 ** 9 + 7))

Submission status: Wrong Answer

Solution code:
# import itertools
# import math
import sys
# sys.setrecursionlimit(500*500)
# import numpy as np
# import heapq
# from collections import deque

# N = int(input())
# S = input()
# n, *a = map(int, open(0))
X, Y = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

# 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

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

cnt = (X + Y) // 3

diff = abs(X - Y)



def comb(n, m, mod = 10 ** 9 + 7):
    numerator = 1
    denominator = 1
    for i in range(1, m + 1):
        numerator = numerator * (n - i + 1) % mod
        denominator = denominator * i % mod
    d_inv = pow(denominator, mod - 2, mod)
    return numerator * d_inv % mod

print(comb(cnt, (cnt - diff) // 2) % (10 ** 9 + 7))

Submission status: Wrong Answer

Solution code:
# import itertools
# import math
import sys
# sys.setrecursionlimit(500*500)
# import numpy as np
# import heapq
# from collections import deque

# N = int(input())
# S = input()
# n, *a = map(int, open(0))
X, Y = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

# 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

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

cnt = (X + Y) // 3

diff = abs(X - Y)



def comb(n, m, mod = 10 ** 9 + 7):
    numerator = 1
    denominator = 1
    for i in range(1, m + 1):
        numerator = numerator * (n - i + 1) % mod
        denominator = denominator * i % mod
    d_inv = pow(denominator, mod - 2, mod)
    return numerator * d_inv % mod

print(comb(cnt, (cnt - diff) // 2) % (10 ** 9 + 7))

Submission status: Wrong Answer

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

def mod_pow(n, m):
  res = 1
  while m > 0:
    if m & 1:
      res = (res*n)%MOD
    n = (n*n)%MOD
    m >>= 1
  return res
  
if (2*Y-X)%3 != 0 or (2*X-Y)%3 != 0:
  print(0)
else:
  A = (2*X-Y)//3
  B = (2*Y-X)//3
  m = 1
  n = 1
  for i in range(A):
    m = (m*(A+B-i))%MOD
    n = (n*(A-i))%MOD
  inverse_n = mod_pow(n, MOD-2)
  print((m*inverse_n)%MOD)

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
x, y = [int(i) for i in input().split()]
if (2*x - y) % 3 != 0 or (2*y - x) % 3 != 0:
  print(0)
a = (2*x - y) // 3
b = (2*y - x) // 3
def mygcd(a_b, b, mod):
    b = min(a_b, b)
    cnt = 1
    for i in range(b):
        cnt = cnt * (a_b - i) * pow(i+1, mod-2, mod) % mod
    return cnt
print(mygcd(a+b, b, mod))

Submission status: Wrong Answer

Solution code:
import sys
x, y = map(int,input().split())

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

n = y - (x+y)//3
m = y - (x+y)//3

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

Submission status: Wrong Answer

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

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

N=a+b

ans=0

#print(a,b,N)


if (X+Y)%3!=0:
    ans=0
elif a==0 or b==0:
    ans=1
else:
    NN=1
    aa=1
    for i in range(1,a+1):
        NN*=N+1-i
        aa*=i
    ans=NN//aa

print(ans%(10**9+7))

Submission status: Wrong Answer

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

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

N=a+b

ans=0

#print(a,b,N)


if (X+Y)%3!=0:
    ans=0
elif a==0 or b==0:
    ans=1
else:
    NN=1
    aa=1
    for i in range(1,a+1):
        NN*=N+1-i
        aa*=i
    ans=N//a

print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
# ABC145 D

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

a,b=0,0
if not (2*X-Y)%3:
    b=(2*X-Y)//3
if not (2*Y-X)%3:
    a=(2*Y-X)//3
    



N=10**6
p=10**9+7
f,finv,inv=[0]*N,[0]*N,[0]*N

def nCr_init(L,p):
    for i in range(L+1):
        if i<=1:
            f[i],finv[i],inv[i]=1,1,1
        else:
            f[i]=(f[i-1]*i)%p
            inv[i]=(p-inv[p%i]*(p//i))%p
            finv[i]=(finv[i-1]*inv[i])%p

def nCr(n,r,p):
    if 0<=r<=n and 0<=n:
        return (f[n]*finv[n-r]*finv[r])%p
    else:
        return 0
    
nCr_init(a+b+1,p)
if a*b>0:
    print(nCr(a+b,b,p))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math
N = [ int(i) for i in input().split() ]  # [3,3]
N = np.sort( N )[::-1]
N = np.array(N)
number = 1
m1 = np.max(N)//2+1
m2 = 0
v1 = np.array( [2,1] )
v2 = np.array( [1,2] )
m = v1*m1 + v2*m2

while m[1] <= m[0]:
  if np.sum(N*N) == np.sum(m*m):
    break
  m1 = m1 -1
  m2 = m2 +1
  m = v1*m1 + v2*m2
else:
  number = 0
  
if number != 0:
  number = math.factorial(m1+m2) / math.factorial(m1) / math.factorial(m2)
  print(number)
else:
  print(number)

Submission status: Wrong Answer

Solution code:
def nCr(n, r):
    num, den = 1, 1
    for i in range(n, n-r, -1):
        num = num * i % M       ##;print(num)
    for i in range(1, r+1):
        den = den * i % M       ##;print(den)
    den_inv = pow(den, -1, M)   #;print(num, den, den_inv) 
    return num * den_inv % M

def is_comb():
    a = 1/3 * (2*X - Y)
    b = 1/3 * (-X + 2*Y)
    if a.is_integer() and b.is_integer():
        return int(a), int(b)
    return 0, 0

M = 10**9 + 7
X, Y  = map(int, input().split())
a, b = is_comb()
if a==b==0:
    print(0)
else:
    n, r  = a+b, a
    print(nCr(n, r))

Submission status: Wrong Answer

Solution code:
import math

X,Y= list(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)
    exit()

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


#print(s,t)

ans = cmb(s+t, t)
ans = ans % (10**9+7)

print(ans)

Submission status: Wrong Answer

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

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

xc = (2*y-x)//3
yc = (2*x-y)//3
p = 1
k = 1
for i in range(1, xc+1):
    p *= (yc+i)
    k *= i
    p %= mod
    k %= mod

ans = p*pow(k, mod-2, mod)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[8]:


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


# In[1]:


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


# In[11]:


mod = 10**9+7
if (x+y)%3 != 0:
    ans = 0
else:
    right = x - (x+y)//3
    up = y - (x+y)//3
    if right < 0 or up < 0:
        ans = 0
    else:
        ans = nCk(right+up,right,mod)
print(ans)


# In[ ]:

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[8]:


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


# In[1]:


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


# In[10]:


mod = 10**9+7
if (x+y)%3 != 0:
    ans = 0
else:
    right = x - (x+y)//3
    up = y - (x+y)//3
    ans = nCk(right+up,right,mod)
print(ans)


# In[ ]:

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    X, Y = MI()
    
    exist = False
    for x1 in range(1, X+1):
        y1 = Y - 2 * x1
        if x1 + 2 * y1 == X:
            exist = True
            break
    if not exist:
        print(0)
        sys.exit()

    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 = 1000000007
    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 = cmb(x1+y1,y1,mod)
    print(a)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    X, Y = MI()
    
    exist = False
    for x1 in range(1, X+1):
        y1 = Y - 2 * x1
        if x1 + 2 * y1 == X:
            exist = True
            break
    if not exist:
        print(0)
        exit()
        
    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 = cmb(x1+y1,y1,mod)
    print(a)
    
if __name__ == "__main__":
    main()