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
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import lru_cache
import bisect
import re
import queue
import decimal


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True

    @staticmethod
    def fact(N):
        res = {}
        tmp = N
        for i in range(2, int(N ** 0.5 + 1) + 1):
            cnt = 0
            while tmp % i == 0:
                cnt += 1
                tmp //= i
            if cnt > 0:
                res[i] = cnt
        if tmp != 1:
            res[tmp] = 1
        if res == {}:
            res[N] = 1
        return res


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def modinv(a):
    b = MOD
    u = 1
    v = 0
    while b:
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u

    u %= MOD
    if u < 0:
        u += MOD
    return u


def factorial(N):
    if N == 0 or N == 1:
        return 1
    res = N
    for i in range(N - 1, 1, -1):
        res *= i
        res %= MOD
    return res


def solve():
    X, Y = Scanner.map_int()
    if (X + Y) % 3 != 0:
        print(0)
        return
    B = (2 * Y - X) // 3
    A = (2 * X - Y) // 3
    if A < 0 or B < 0:
        print(0)
        return
    n = factorial(A + B)
    m = factorial(A)
    l = factorial(B)
    ans = n * modinv(m * l % MOD) % MOD
    print(ans)


def main():
    
    
    
    
    
    
    solve()


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    x, y = map(int, readline().split())
    ans = 0
    tmp = 1

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

    def com(n, r, mod):
        r = min(r, n - r)
        if r == 0:
            return 1
        res = ilist[n] * iinvlist[n - r] * iinvlist[r] % mod
        return res

    ilist = [0]
    iinvlist = [1]
    tmp = 1


    for i in range(1, x + y + 1):
        tmp *= i
        tmp %= mod
        ilist.append(tmp)
        iinvlist.append(modinv(tmp, mod))


    for b in range(x//2+1):
        a = x - 2 * b
        if 2*a + b == y:
            ans += ilist[a+b] * iinvlist[a] * iinvlist[b] % mod
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
x, y = map(int,input().split())
p = 10 ** 9 + 7
if (x + y) % 3 != 0 or 2*x < y or 2*y < x:
    print(0)
    exit()
a = (2 * y -x) // 3
b = (2 * x -y) // 3

n = a + b
fac = [0] * (n + 1)
fac[0] = 1
for i in range(n):
    fac[i + 1] = fac[i] * (i + 1) % p
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

print(mcomb(n,a))

Solution (Python):
mod=10**9+7
def fact(n):
    ans=1
    for i in range(1,n+1):
        ans*=i
        ans%=mod
    return ans

x,y=map(int,input().split())
if (x+y)%3!=0: print(0)
else:
    temp=(x+y)//3
    if x<temp or x>2*temp: print(0)
    else:
        dx=x-temp
        fc_temp=fact(temp)
        fc_dx=fact(dx)
        fc_temp_dx=fact(temp-dx)
        fc_dx=pow(fc_dx,mod-2,mod)
        fc_temp_dx=pow(fc_temp_dx,mod-2,mod)
        ans=fc_temp*fc_dx%mod*fc_temp_dx%mod
        print(ans)

Solution (Python):

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

m = 1000000007
def mcomb(n, k):
    a = 1
    b = 1
    for i in range(k):
        a *= n - i
        a %= m
        b *= i + 1
        b %= m
    return a * pow(b, m - 2, m) % m
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
print(mcomb(n, a))

Solution (Python):

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

m = 1000000007


def make_factorial_table(n):
    result = [0] * (n + 1)
    result[0] = 1
    for i in range(1, n + 1):
        result[i] = result[i - 1] * i % m
    return result


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], m - 2, m) * pow(fac[k], m - 2, m) % m


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
fac = make_factorial_table(n)
print(mcomb(n, a))

Solution (Python):
def nCr(n, r, mod):
    x, y = 1, 1
    for r_ in range(1, r+1):
        x = x*(n+1-r_)%mod
        y = y*r_%mod
    return x*pow(y, mod-2, mod)%mod

x, y = map(int, input().split())
mod = 10**9+7
if (x+y)%3 or 2*x<y or 2*y<x:
    print(0)
else:
    print(nCr((x+y)//3,(2*x-y)//3, mod))

Solution (Python):
def cmb(n, r, mod):
    if (r < 0 or r > n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod
 
mod = 10**9+7  
nums = 10**6 
g1, g2, inverse = [1, 1] , [1, 1], [0, 1]
 
for num in range(2, nums + 1):
    g1.append((g1[-1] * num) % mod)
    inverse.append((-inverse[mod % num] * (mod//num)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)
    
x, y = map(int, input().split())
if (x+y)%3 or 2*x<y or 2*y<x:
    print(0)
else:
    print(cmb((x+y)//3,(2*x-y)//3, mod))

Solution (Python):
def cmbp(n,k):
    nCk = 1
    MOD = 10**9+7

    for i in range(n-k+1, n+1):
        nCk *= i
        nCk %= MOD

    for i in range(1,k+1):
        nCk *= pow(i,MOD-2,MOD)
        nCk %= MOD
    return nCk
x,y=map(int,input().split())
if (x+y)%3==0:
  n=int((2*y-x)/3)
  m=int((2*x-y)/3)
  if n>=0 and m>=0:
    print(cmbp(m+n,n))
  else:
    print(0)
else:
  print(0)

Solution (Python):
mod=10**9+7
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
from itertools import combinations
import bisect
def mi() : return map(int,sys.stdin.readline().split())
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()

a,b=mi()
if (a+b)%3!=0:
  print(0)
  sys.exit()
s=(a+b)//3
a-=s
b-=s
if a<0 or b<0:
  print(0)
  sys.exit()
fac=[1]*700000
for i in range(1,700000):
  fac[i]=(fac[i-1]*i)%mod
m=a+b
n=min(a,b)
c=1
for j in range(m,m-n,-1):
  c*=j
  c=c%mod
print((c*pow(fac[n],mod-2,mod))%mod)

Solution (Python):

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

m = 1000000007

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
fac = [0] * (n + 1)
fac[0] = 1
for i in range(n):
    fac[i + 1] = fac[i] * (i + 1) % m


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], m - 2, m) * pow(fac[k], m - 2, m) % m


print(mcomb(n, a))

Solution (Python):
def comb_mod(n,r,m):
    ans = 1
    for i in range(1,r+1):
        ans *= (n-i+1) % m
        ans *= pow(i,m-2,m) % m
        ans = ans % m
    return ans

x,y = map(int,input().split())
m = 10**9+7
if x > 2*y or 2*x < y or (x+y)%3 != 0:
    ans = 0
else:
    n = (x+y)//3
    r = x-n
    ans = comb_mod(n,r,m)
print(ans)

Solution (Python):
x,y=map(int,input().split())
if (x+y)%3!=0:
    print(0)
    exit()
if x<y:
    x,y=y,x
if 2*y<x:
    print(0)
    exit()
mod=10**9+7
a=(2*y-x)//3
b=(2*x-y)//3
mx=10**6
k=[1]*(mx+1)
def inv(n):
    return pow(n,mod-2,mod)
for i in range(mx):
    k[i+1]=k[i]*(i+1)%mod
ans=(k[a+b]*inv(k[a])*inv(k[b]))%mod
print(ans)

Solution (Python):


import sys
input=sys.stdin.readline
MOD=10**9+7

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

def modinv(x):
    return pow(x,MOD-2,MOD)
    
def comb(n,r):
    return (factorial[n]*modinv(factorial[n-r])*modinv(factorial[r]))%MOD

def main():
    X,Y=map(int,input().split())
    a=(2*Y-X)
    b=(2*X-Y)
    A=a//3
    B=b//3
    ans=0
    if a%3==0 and b%3==0 and a>=0 and b>=0:
        ans=comb(A+B,A)
    print(ans)
    
    
    
if __name__=="__main__":
    main()

Solution (Python):
def ncr(n, r):
    num, den = 1, 1
    if n - r < r:
        r = n - r
    for i in range(1, r + 1):
        num *= n - i + 1
        num %= MOD
        den *= i
        den %= MOD
    return num * pow(den, MOD - 2, MOD) % MOD


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

if Z % 3 or Y > 2 * X or Y < X // 2:
    print(0)
else:
    print(ncr(Z // 3, X - Z // 3))

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

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

n = (X + Y) // 3
r = X - (X + Y) // 3
num, den = 1, 1
if n - r < r:
    r = n - r
for i in range(1, r + 1):
    num *= n - i + 1
    num %= MOD
    den *= i
    den %= MOD

print((num * pow(den, MOD - 2, MOD)) % MOD)

Solution (Python):
def ncr(n, r):
    return fac[n] * facinv[r] * facinv[n - r] % MOD


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

N = 10 ** 6
fac = [1, 1]
facinv = [1, 1]
inv = [0, 1]
for i in range(2, N + 1):
    fac.append(fac[i - 1] * i % MOD)
    inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
    facinv.append((facinv[-1] * inv[-1]) % MOD)

if (X + Y) % 3 or Y > 2 * X or Y < X // 2:
    print(0)
    exit()
d = (X + Y) // 3
print(ncr(d, X - (X + Y) // 3))

Solution (Python):
import sys
from io import StringIO
import unittest
import os




sys.setrecursionlimit(999999999)

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

def prepare_simple(n, mod=pow(10, 9) + 7):
    
    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


def prepare(n, mod=pow(10, 9) + 7):
    
    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



def resolve(test_def_name=""):
    x, y = map(int, input().split())

    all_move_count = (x + y) // 3

    x_move_count = all_move_count
    y_move_count = 0

    x_now = all_move_count * 2
    y_now = all_move_count

    canMake= False
    for i in range(all_move_count+1):
        if x_now == x and y_now == y:
            canMake = True
            break

        x_move_count -= 1
        y_move_count += 1
        x_now -= 1
        y_now += 1

    if not canMake:
        print(0)
        return


    ans = cmb(all_move_count,x_move_count)

    print(ans)



class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """3 3"""
        output = """2"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """2 2"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """999999 999999"""
        output = """151840682"""
        self.assertIO(test_input, output)

    
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

Solution (Python):
X,Y=map(int,input().split())
mod=10**9+7
if (X+Y)%3!=0:
  print(0)
else:
  N=int((2*Y-X)/3)
  M=int((2*X-Y)/3)
  if N<0 or M<0:
    print(0)
  if N==0 or M==0:
    print(1)
  if N>0 and M>0:
    List=[0 for i in range(N+M)]
    List[0]=1
    for i in range(N+M-1):
      List[i+1]=List[i]*(i+2)%mod
    print(List[N+M-1]*pow(List[N-1],mod-2,mod)*pow(List[M-1],mod-2,mod)%mod)

Solution (Python):
import sys
from io import StringIO
import unittest
import os


sys.setrecursionlimit(999999999)


def prepare_simple(n, mod=pow(10, 9) + 7):
    
    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


def prepare(n, mod=pow(10, 9) + 7):
    
    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



def resolve(test_def_name=""):
    x, y = map(int, input().split())

    all_move_count = (x + y) // 3

    x_move_count = all_move_count
    y_move_count = 0

    x_now = all_move_count * 2
    y_now = all_move_count

    canMake= False
    for i in range(all_move_count+1):
        if x_now == x and y_now == y:
            canMake = True
            break

        x_move_count -= 1
        y_move_count += 1
        x_now -= 1
        y_now += 1

    if not canMake:
        print(0)
        return

    fns, invs = prepare(all_move_count)
    ans = (fns[all_move_count] * invs[x_move_count] * invs[all_move_count - x_move_count]) % (pow(10, 9) + 7)

    print(ans)
    
    
    



class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """3 3"""
        output = """2"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """2 2"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """999999 999999"""
        output = """151840682"""
        self.assertIO(test_input, output)

    
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

Solution (Python):
import sys
import itertools

import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

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

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

def comInit(mod):
    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, r, mod):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod
    
comInit(MOD)

if X < Y:
    X, Y = Y, X
b = (2 * X - Y) // 3
a = X - 2 * b
print(com(a + b, b, MOD))

Solution (Python):
import sys
from io import StringIO
import unittest
import os


sys.setrecursionlimit(999999999)


def prepareSimple(n, mod=pow(10, 9) + 7):
    
    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


def prepare(n, mod=pow(10, 9) + 7):
    
    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



def resolve(test_def_name=""):
    x, y = map(int, input().split())

    all_move_count = (x + y) // 3

    x_move_count = all_move_count
    y_move_count = 0

    x_now = all_move_count * 2
    y_now = all_move_count

    canMake= False
    for i in range(all_move_count+1):
        if x_now == x and y_now == y:
            canMake = True
            break

        x_move_count -= 1
        y_move_count += 1
        x_now -= 1
        y_now += 1

    if not canMake:
        print(0)
        return

    fn, invs = prepareSimple(all_move_count)
    ans = (fn * invs[x_move_count] * invs[all_move_count - x_move_count]) % (pow(10, 9) + 7)

    print(ans)
    
    
    



class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """3 3"""
        output = """2"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """2 2"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """999999 999999"""
        output = """151840682"""
        self.assertIO(test_input, output)

    
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

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

def fac(n):
    ans = 1
    for i in range(1, n + 1):
        ans = ans * i % MOD
    return ans

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

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

Solution (Python):
from math import factorial

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

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

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

MOD = 10**9 + 7
class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv
    
comb = Combination(1000000)
print(comb(SUM, n)%MOD)

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

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

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

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


mod = 10**9+7
N = 10**6  
fact = [1, 1]  
factinv = [1, 1]  
inv = [0, 1]  

for i in range(2, N+1):
	fact.append((fact[-1]*i)%mod)
	inv.append((-inv[mod%i]*(mod//i))%mod)
	factinv.append((factinv[-1]*inv[-1])%mod)

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

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

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

if 2 * min(X, Y) < max(X, Y):
    print(0)
    sys.exit()

def f(n, A):
    if A == 0:
        return 1
    bunsi = reduce(lambda x, y: x*y%mod, range(n, n-A, -1))
    bunbo = reduce(lambda x, y: x*y%mod, range(1, A+1))
    return bunsi * pow(bunbo, mod-2, mod) % mod

ans = 0
r, d = 0, 0
if X == Y:
    r, d = X//3, Y//3
    ans = f(r+d, d)%mod
else:
    r = abs(X-Y) + ((X+Y) - 3*abs(X-Y))//6
    d = ((X+Y) - 3*abs(X-Y))//6
    ans = f(r+d, d)%mod

print(ans)

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

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

if 2 * min(X, Y) < max(X, Y):
    print(0)
    sys.exit()

def f(n, A):
    bunsi = reduce(lambda x, y: x*y%mod, range(n, n-A, -1))
    bunbo = reduce(lambda x, y: x*y%mod, range(1, A+1))
    return bunsi * pow(bunbo, mod-2, mod) % mod

ans = 0
r, d = 0, 0
if X == Y:
    r, d = X//3, Y//3
    ans = f(r+d, d)%mod
else:
    r = abs(X-Y) + ((X+Y) - 3*abs(X-Y))//6
    d = ((X+Y) - 3*abs(X-Y))//6
    if d == 0:
        print(1)
        sys.exit()
    ans = f(r+d, d)%mod

print(ans)

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

def factorial(n):
    res = 1
    for i in range(2, n+1):
        res *= i
        res %= P
    return res

if (2*X-Y)%3 == 0 and (-X+2*Y)%3 == 0 and 2*X-Y >= 0 and -X+2*Y >= 0:
    a = (-X+2*Y)//3
    b = (2*X-Y)//3
    print((factorial(a+b)*pow(factorial(a), P-2, P)*pow(factorial(b), P-2, P))%P)
else:
    print(0)

Solution (Python):
mod = 10**9 + 7
def cmb(n,r,mod):
	a=1
	b=1
	r = min(r,n-r)
	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 = map(int,input().split())
if (X+Y)%3 != 0:
  ans = 0
else:
  n = (2*X-Y)//3
  m = (2*Y-X)//3
  if n < 0 or m < 0:
    ans = 0
  else:
    ans = cmb(n+m,m,mod)
print(ans)

Solution (Python):




move1 = [1,2]
move2 = [2,1]
X,Y = map(int,input().split())

max_move1 = int(Y/2)
move2_num = -1
move1_num = -1
for i in range(max_move1+1):
    rest = [X-i,Y-2*i]
    if (X-i) == 2*(Y-2*i):
        move1_num = i
        move2_num = (Y-2*i)
if move1_num == -1:
    print(0)
    exit()



large = 10**9 + 7

ans = 1
for i in range(move1_num):
    ans *= (move1_num+move2_num - i)
    ans %= large
for j in range(2,move1_num+1):
    ans *= pow(j,large-2,large)
    ans %= large
print(ans)

Solution (Python):
def comb(n, k):
    ret = 1
    for i in range(1, k + 1):
        ret = ret * (n - i + 1) % MOD        
        ret = ret * pow(i, MOD - 2, MOD) % MOD

    return ret


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

MOD = 10 ** 9 + 7

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

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

print(comb(a + b, a))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10**9+7
X,Y = map(int,readline().split())
if X > Y:
    X,Y = Y,X
Z = X+Y
if Z % 3 == 0 and X <= Y <= 2*X:
    N = Z//3
    r = (N-(Y-X)) // 2
else:
    print(0)
    sys.exit()
f = [1]*(N+1)
for i in range(1,N+1):
    f[i] = i * f[i-1] % mod
a = pow(f[r],mod-2,mod)
b = pow(f[N-r],mod-2,mod)
ans = f[N] * a * b % mod
print(ans)

Solution (Python):
def main():
    from functools import reduce
    class comb_mod:
        mod = 1000000007
        MAX = pow(10, 6)
        fac = [0 for i in range(MAX)]
        finv = [0 for i in range(MAX)]
        inv = [0 for i in range(MAX)]

        def __init__(self):
            self.fac[0], self.fac[1] = 1, 1
            self.finv[0], self.finv[1] = 1, 1
            self.inv[1] = 1
            for i in range(2, self.MAX):
                self.fac[i] = self.fac[i - 1] * i % self.mod
                self.inv[i] = self.mod - self.inv[self.mod % i] * (self.mod // i) % self.mod
                self.finv[i] = self.finv[i - 1] * self.inv[i] % self.mod
        def combination(self, n, r):
            if n < r:
                return 0
            if n < 0 or r < 0:
                return 0
            return self.fac[n] * (self.finv[r] * self.finv[n - r] % self.mod) % self.mod
        def combination_mul(self, N, R):
            return reduce(lambda x, y: x * self.combination(y[0], y[1]) % self.mod, zip(N, R), 1)
    x, y = list(map(int, input().split()))
    ans = 0
    C = comb_mod()
    for i in range(x, -1, -2):
        j = (x - i) // 2
        if 2 * i + j == y:
            ans += C.combination(i + j, i)
    print(ans)




if __name__ == '__main__':
    main()

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

def comb(n, r, mod):
    n1 = n+1
    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 
    
    
    
    

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

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


def conb(n, r):
    x = 1
    y = 1
    for i in range(n - r + 1, n + 1):
        x = (x * i % P)
    for i in range(1, r + 1):
        y = (y * i % P)
    y_inv = power(y, P - 2)
    return (x * y_inv)%P


def power(x, n):
    ans = 1
    while (n > 0):
        if (bin(n & 1) == bin(1)):
            ans = (ans * x) % P
        x = (x * x) % P
        n = n >> 1  
    return ans


if (2 * X - Y) % 3 == 0 and (2 * Y - X) % 3 == 0:
    u = (2 * Y - X) // 3
    r = (2 * X - Y) // 3
    if u < 0 or r < 0:
        print(0)
    else:
        print(conb(u + r, r))


else:
    print(0)

Solution (Python):
def nck(n, k):
    numer, denom = 1, 1
    for i in range(k):
        numer = numer * (n-i) % MOD
        denom = denom * (k-i) % MOD
    return numer * pow(denom, MOD-2, MOD) % MOD

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

i = 2 * y - x
j = 2 * x - y
if i % 3 != 0 or j % 3 != 0 or i < 0 or j < 0:
    print(0)
else:
    i, j = i // 3, j // 3
    print(nck(i+j, min(i, j)))

Solution (Python):
def pre_combi1(n, p):
    fact = [1]*(n+1)  
    factinv = [1]*(n+1)  
    inv = [0]*(n+1)  
    inv[1] = 1
    
    for i in range(2, n + 1):
        fact[i]= fact[i-1] * i % p
        inv[i]= -inv[p % i] * (p // i) % p
        factinv[i]= factinv[i-1] * inv[i] % p
    return fact, factinv

def combi1(n, r, p, fact, factinv):
    if r < 0 or n < r:
        return 0
    r = min(r, n-r)
    return fact[n] * factinv[r] * factinv[n-r] % p

import sys
x,y=map(int,input().split())
if (x+y)%3 > 0:
  print(0)
  sys.exit()
n=(x+y)//3
r=x-n
f,inv=pre_combi1(n,1000000007)
print(combi1(n,r,1000000007,f,inv))

Solution (Python):
def pre_combi1(n, p):
    fact = [1]*(n+1)  
    factinv = [1]*(n+1)  
    inv = [0]*(n+1)  
    inv[1] = 1
    
    for i in range(2, n + 1):
        fact[i]= fact[i-1] * i % p
        inv[i]= -inv[p % i] * (p // i) % p
        factinv[i]= factinv[i-1] * inv[i] % p
    return fact, factinv

def combi1(n, r, p, fact, factinv):
    if r < 0 or n < r:
        return 0
    r = min(r, n-r)
    return fact[n] * factinv[r] * factinv[n-r] % p

import sys
x,y=map(int,input().split())
if (x+y)%3 > 0:
  print(0)
  sys.exit()
n=(x+y)//3
r=x-n
f,inv=pre_combi1(n,1000000007)
print(combi1(n,r,1000000007,f,inv))

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

def comb(n,r,mod):
    n1 = n+1
    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

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

Solution (Python):
def mod_inverse(x, mod):
    return pow(x, mod - 2, mod)




def mod_comb(n, k, mod):
    numer, denom = 1, 1
    for i in range(k):
        numer = numer * ((n - i) % mod) % mod
        denom = denom * ((i + 1) % mod) % mod

    return numer * mod_inverse(denom, mod) % mod


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

    t1, t2 = 2 * Y - X, 2 * X - Y
    if t1 % 3 != 0 or t2 % 3 != 0 or t1 < 0 or t2 < 0:
        print(0)
        return

    a, b = t1 // 3, t2 // 3
    print(mod_comb(a + b, a, mod))


main()