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):
x,y = map(int,input().split())
mod = 10**9+7

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

p = (x+y)//3
if min(x,y) < p:
    print(0)
    exit()

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

x = min(x,y)

print(mod_comb_k(p,x-p,mod))

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

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 = result * int(numerator[k]) % (1e9+7)

    return result


if 2 * X < Y or X > 2 * Y or (X+Y)%3 != 0:
    print(0)
else:
    D = 1e9+7
    M = (X+Y)//3
    m = (2*X-Y)//3
    n=(2*Y-X)//3 
    res = cmb(M,m) % D
    """for i in range(M, M-m,-1):
        res *= i 
    for i in range(1,m+1):
        res //= i """
    print(int(res))

Solution (Python):
def framod(n, mod, a=1):
    for i in range(1,n+1):
        a=a * i % mod
    return a

def power(n, r, mod):
    if r == 0: return 1
    if r%2 == 0:
        return power(n*n % mod, r//2, mod) % mod
    if r%2 == 1:
        return n * power(n, r-1, mod) % mod

def comb(n, k, mod):
    a=framod(n, mod)
    b=framod(k, mod)
    c=framod(n-k, mod)
    return (a * power(b, mod-2, mod) * power(c, mod-2, mod)) % mod


x, y = map(int, input().split())
mod = 10 ** 9 + 7
ans_comb = []
for i in range(x // 2 + 1):
    j = x - 2 * i
    if 2 * i + j == x and 2 * j + i == y:
        ans_comb = [i, j]
if len(ans_comb) == 0:
    print(0)
else:
    print(comb(ans_comb[0] + ans_comb[1], ans_comb[1], 10 ** 9 + 7))

Solution (Python):
import sys
import math

MOD=10**9+7

def egcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return lastx


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

if (x+y)%3!=0: print(0),sys.exit()
p=(x+y)//3
q=min((2*y-x)//3,(2*x-y)//3)
if q<0: print(0),sys.exit()

ans=1
for i in range(q):
    ans=(ans*(p-i)*egcd(i+1,MOD))%MOD
print(ans)

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

mod = 10**9 +7

def fact(n,mod):
   t = 1
   for i in range(1,n+1):
       t = (t * i) % mod
   return t

def inv(a,mod):
    r = [1,0,a]
    w = [0,1,mod]
    while w[2]!=1:
        q = r[2]//w[2]
        r_new = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = w
        w = r_new
    x,y = w[0],w[1]    
    return (mod+x%mod)%mod




if (x+y) % 3 == 0 and y <= 2*x and 2*y >= x:
    n = (x+y)//3
    m = x-n
    ans = fact(n,mod) * inv(fact(n-m,mod),mod) * inv(fact(m,mod),mod)
    print(ans % mod)
else:
    print(0)
    exit()

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):
    x = extgcd(a,m)[0]
    return (m+x%m)%m
x,y = map(int,input().split())
kai = []
for i in range(x+1):
    if x-i == (y-2*i)*2:
        kai.append(i)
        kai.append(y-2*i)
        break
m = 10**9 + 7
if len(kai) == 0:
    print(0)
else:
    ans = 1
    b = kai[0]+kai[1]
    for i in range(kai[0]):
        
        ans = ans*(b-i)%m
        ans = ans*mod_inv(i+1,m)%m
    print(ans)

Solution (Python):
import sys; readline = sys.stdin.readline
from collections import deque


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

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

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

P = 10**9 + 7

t = n + m
s = min(n, m)

ans = 1
for i in range(t, t - s, -1):
    ans = ans * i
    ans %= P

d = 1
for i in range(s):
    d *= (i + 1)
    d %= P

def sp(x, n):
    if n == 0:
        return 1
    r = n % 2
    ret = sp(x, n//2) ** 2
    ret %= P
    if r == 1:
        ret *= x
        ret %= P
    return ret
dinv = sp(d, P - 2)

ans = ans * dinv
ans %= P

print(ans)

Solution (Python):
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def modpow(a, n, mod):
    res = 1
    while n > 0:
        if n & 1:
            res = res * a % mod
        a = a * a % mod
        n >>= 1
    return res


def modinv(a, mod):
    return modpow(a, mod - 2, mod)


def main():
    x, y = map(int, input().split())
    if (2 * y - x) % 3 != 0 or  (2 * x - y) % 3 != 0 or (2 * x - y) < 0 or (2 * y - x)  < 0:
        print(0)
        exit()
    p = (2 * y - x) // 3
    q = (2 * x - y) // 3
    
    ans = 1
    for i in range(1, p + q + 1):
        ans = (ans * i) % MOD
    
    v,z=1,1
    for i in range(1, p + 1):
        v = (v * i) % MOD
        
    for i in range(1, q + 1):
        z = (z * i) % MOD
    
    vz = (v*z)%MOD
    ans = ans*modinv(vz,MOD) % MOD
    print(ans)



if __name__ == '__main__':
    main()

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

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())
    if (2*X-Y) % 3 != 0 or (2*Y-X) % 3 != 0 or 2*X < Y or 2*Y < X:
        ans = 0
    else:
        a = (2*X-Y)//3
        b = (2*Y-X)//3
        ans = cmb(a+b, a)%M
    print(ans)


if __name__ == '__main__': main()

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

mod = 10**9+7 
N = 10**6
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 

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

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

Solution (Python):

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


mod = 10 ** 9 + 7
MAX = 10**6 + 1
fac = [0] * MAX
finv = [0] * MAX
inv = [0] * MAX

def cominit():
    fac[0] = 1
    fac[1] = 1
    finv[0] = 1
    finv[1] = 1
    inv[1] = 1
    for i in range(2,MAX):
        fac[i] = fac[i-1] * i % mod
        inv[i] = mod - inv[mod%i] * (mod//i) % mod
        finv[i] = finv[i-1] * inv[i] % mod

if (2*Y-X)%3 == 0 and (2*X-Y)%3 == 0:
    n1 = (2*Y-X)//3
    n2 = (2*X-Y)//3
    if n1 < 0 or n2 < 0:
        print(0)
    else:
        cominit()
        ans = fac[(n1+n2)]*finv[n1]*finv[n2]
        ans %= mod
        print(ans)
        
else:
    print(0)

Solution (Python):
x, y = [int(i) for i in input().split()]
if (2*x - y)%3 != 0:
    print(0)
elif (2*x - y < 0) or ((2*y - x) < 0):
    print(0)
else:
    LIM = 2*(10**6 + 5)
    fct = [1]*LIM
    inv = [1]*LIM
    MOD = 10**9 + 7
    for i in range(1, LIM):
        fct[i] = ((i%MOD)*(fct[i-1]%MOD))%MOD

    for i in range(1, LIM):
        inv[i] = (((inv[i-1])%MOD)*(pow(i, MOD-2, MOD)%MOD)%MOD)
    
    B = (2*x - y)//3
    A = x - (2*B)
    num = fct[A+B]
    den = ((inv[A]%MOD)*(inv[B])%MOD)%MOD
    print((((num)%MOD)*((den)%MOD))%MOD)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    def com(com_n, com_r):
        return fac[com_n] * inv[com_r] * inv[com_n - com_r] % md

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

    
    md = 10 ** 9 + 7
    n_max = 10 ** 6
    fac = [1]
    inv = [1] * (n_max + 1)
    k_fac_inv = 1
    for i in range(1, n_max + 1):
        k_fac_inv = k_fac_inv * i % md
        fac.append(k_fac_inv)
    k_fac_inv = pow(k_fac_inv, md - 2, md)
    for i in range(n_max, 1, -1):
        inv[i] = k_fac_inv
        k_fac_inv = k_fac_inv * i % md

    s, t = x + y, x - y
    if s % 3:
        print(0)
        exit()
    step = s // 3
    if (t + step) % 2:
        print(0)
        exit()
    n_plus = (t + step) // 2
    if not 0 <= n_plus <= step:
        print(0)
        exit()
    print(com(step, n_plus))

main()

Solution (Python):
MOD = 1000000007

x,y = map(int,input().split())
if (x+y)%3!=0:
    print(0)
    exit()
p = (x+y)//3
W = x-p+1
H = y-p+1

if W == 1 or H == 1:
    print(1)
    exit()
if W < 1 or H < 1:
    print(0)
    exit()

numer = 1

inv = [0 for _ in range(max(W,H))]
inv[0] = 1

for i in range(W+H-2):
    numer *= i+1
    numer %= MOD

for i in range(1,len(inv)):
    inv[i] = inv[i-1]*pow(i+1,MOD-2,MOD) %MOD

print(numer*inv[H-2]*inv[W-2]%MOD)

Solution (Python):
import sys
x,y=map(int,input().split())
if (x+y)%3!=0:
    print(0)
    sys.exit()
dx=x-(x+y)//3
dy=y-(x+y)//3
if dx<0 or dy<0:
    print(0)
    sys.exit()
fact=[1]*(dx+dy+1)
MOD=10**9+7
def inv_mod(a):
  return pow(a,MOD-2,MOD)

for i in range(1,dx+dy+1):
  fact[i] = (i*fact[i-1])%MOD
fact_inv = [1] * (dx+dy+1)
fact_inv[-1] = inv_mod(fact[-1])
for i in range(dx+dy,0,-1):
  fact_inv[i-1] = (i*fact_inv[i])%MOD

def comb(n,k):
  x = fact[n]
  x *= fact_inv[k]
  x %= MOD
  x *= fact_inv[n-k]
  x %= MOD
  return x
print(comb(dx+dy,dx))

Solution (Python):
def main(sample_file = ''):

    """ convenient functions
    
    
    
    
    
    """

    """convenient decorator
    
    
        
        
        
        
        
        
        
        
        
        
    """
    
    
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: 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
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter 
    
    
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)
    def make_divisors(n, reversed=False):
        """create list of divisors
        
        Args:
            number (int): number from which list of divisors is created
            reversed (bool, optional): ascending order if False. Defaults to False.
        
        Returns:
            list: list of divisors
        """
        divisors = set()
        for i in range(1, int(n**0.5)+1):
            if n % i == 0:
                divisors.add(i)
                divisors.add(n//i)
        return sorted(list(divisors),reverse=reversed)
    
    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    class UnionFindTree:
        """union find tree class
        TODO: fix this description...
        how to use (example):
        >>  uf = UnionFindTree(N) 
        >>  if uf.find_root(a) == uf.find_root(b):
        >>      do something
        >>  else:
        >>      do something
        >>      uf.unite(a, b)
        """
        def __init__(self, N):
            self.root = [-1] * (N+1)
            self.rank   = [0]  * (N+1)
            self.connected_num = [1] * (N+1)
    
        def find_root(self,x):
            root = self.root
            while root[x] != -1:
                x = root[x]
            return x
    
        def unite(self,x,y):
            root = self.root
            rank = self.rank
            connected_num = self.connected_num
            find_root = self.find_root
    
            rx = find_root(x)
            ry = find_root(y)
            if rx != ry:
                if rank[rx] < rank[ry]:
                    root[rx] = ry
                    rx,ry = ry,rx
                else:
                    if rank[rx] == rank[ry]:
                        rank[rx] += 1
                    root[ry] = rx       
                connected_num[rx] += connected_num[ry]

    
    
    
    
    

    def BellmanFord(N, M, ABC, vertex_start, vertex_end, value_if_inf = -1, find_shortest = False):
        """to calculate furthest or shortest length between vertex_start and vertex_end using BellmanFord algorithm
        
        Args:
            N (int): number of vertices
            M (int): number of edges
            ABC (list): [(ai, bi, ci) for _ in range(N)] where i-th edge is directed from vertex ai to vertex bi and the length is ci 
            vertex_start (int): start vertex. usually use 0.
            vertex_end (int): end vertex. usually use N-1.
            value_if_inf (int or string as you like, optional): value you want when the furthest (or shortest) distance is infinite (or -infinite). Defaults to -1.
            find_shortest (bool, optional): choose False to find furthest path. Defaults to False.
        
        Returns:
            int or string: normally int (but can be str if you set value_if_inf to str)
            
        Example:
                N, M, P = R()
                ABC = [R() for _ in range(M)]
                ABC = [(a-1, b-1, c-P) for a, b, c in ABC]
                print(BellmanFord(N, M, ABC, 0, N-1, value_if_inf = 'inf'))

        """

        def make_reachable_list(N, M, ABC, vertex_start, vertex_end):
            reachable_to_direct = defaultdict(list)
            reachable_from_direct = defaultdict(list)
            reachable_from_start = [False] * N
            reachable_to_end = [False] * N
            reachable_from_start[vertex_start] = True
            reachable_to_end[vertex_end] = True
            reachable_from_both_sides = [False] * N
            dfs_from_start = []
            dfs_to_end = []
            for a, b, c in ABC:
                reachable_to_direct[a].append(b)
                reachable_from_direct[b].append(a)
                if a == vertex_start:
                    dfs_from_start.append(b)
                    reachable_from_start[b] = True
                if b == vertex_end:
                    dfs_to_end.append(a)
                    reachable_to_end[a] = True
            while dfs_from_start:
                v = dfs_from_start.pop()
                for i in reachable_to_direct[v]:
                    if not reachable_from_start[i]:
                        reachable_from_start[i] = True
                        dfs_from_start.append(i)
            while dfs_to_end:
                v = dfs_to_end.pop()
                for i in reachable_from_direct[v]:
                    if not reachable_to_end[i]:
                        reachable_to_end[i] = True
                        dfs_to_end.append(i)
            for i in range(N):
                reachable_from_both_sides[i] = reachable_from_start[i] and reachable_to_end[i]
            return reachable_from_both_sides
        
        reachable_from_both_sides = make_reachable_list(N, M, ABC, vertex_start, vertex_end)
        
        if find_shortest:
            dist = [INF for i in range(N)]
        else:
            dist = [-INF for i in range(N)]
        
        dist[vertex_start] = 0
        for i in range(N):
            updated = False
            for a, b, c in ABC:
                if not reachable_from_both_sides[a]:
                    continue
                elif find_shortest:
                    update_condition = dist[a] + c < dist[b]
                else:
                    update_condition = dist[a] + c > dist[b]
                if dist[a] != INF and update_condition:
                    dist[b] = dist[a] + c
                    updated = True
                    if i == N-1:
                        return value_if_inf
            if not updated:
                break
        return dist[vertex_end]

    """ initialize variables and set inputs
    
        
        
    
        
        
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    
    if sample_file:
        sys.stdin = open(sample_file)
        
    
    

    X, Y = R()
    if (X+Y)/3 != (X+Y)//3:
        print(0)
        return
    tmp = (X+Y)//3
    if X-tmp < 0 or Y-tmp < 0:
        print(0)
        return
    X -= tmp
    Y -= tmp
    print(combinations_count(X+Y,X)%(10**9+7))
    
    
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """
    
    """memo: how to solve binary problems
    
    >>> a = 5
    >>> bin_str_a = format(a, '
    >>> print(bin_str_a)
    0b0101 
    """
    
    """memo: how to solve the problem
    creating simple test/answer
    greed
    simple dp
    graph
    """

if __name__ == '__main__':
    main()

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

from operator import mul
from functools import reduce

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)
elif (X//2)>Y or (Y//2)>X:
    print(0)
else:
    dx = X-(X+Y)//3
    dy = Y-(X+Y)//3
    print (cmb(dx+dy,dx)%int(1e9+7))

Solution (Python):


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

times = (X + Y) // 3
if (X + Y) % 3:
    print('0')
    exit()

x = X - times
y = Y - times


def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y


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


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


if y < 0 or x < 0:
    print('0')
    exit()


print(combination(times, y, mod))

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(10**8)

from math import factorial
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):
    x = extgcd(a,m)[0]
    return (m+x%m)%m
  
def solve():
  x,y = (int(i) for i in input().split())
  mod = 10**9+7
  ct = 0
  for k in range(10**6+1):
    if x < k:
      break
    else:
      
      remx = x-k
      remy = y-2*k
      if remy*2 == remx:
        times = k+remy
        res = 1
        for i in range(1,times+1):
          res = res*i%mod
        for i in range(1,remy+1):
          res = res*mod_inv(i,mod)%mod
        for i in range(1,k+1):
          res = res*mod_inv(i,mod)%mod
        ct += res
      ct %= mod
      
  print(ct)
  
solve()

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


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


X, Y = min(X, Y), max(X, Y)
ALL_MOVE = (X + Y) // 3
for m1 in range(ALL_MOVE):
    m2 = ALL_MOVE - m1

    if (m1 * 2 + m2 == X) and (m1 + m2 * 2 == Y):
        move_1_cnt = m1
        move_2_cnt = m2
        break
else:
    print(0)
    exit()


factorial = [1]
inverse = [1]
for i in range(1, ALL_MOVE + 2):
    factorial.append(factorial[-1] * i % MOD)
    inverse.append(pow(factorial[-1], MOD - 2, MOD))



def nCr(n, r):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % MOD


print(nCr(ALL_MOVE, move_1_cnt) % MOD)

Solution (Python):

import sys
import math
import numpy as np
import itertools

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

MOD = 1000000007

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

if (x > y*2) or (y > x*2):
    print(0)
    exit()


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

upper=1
lower=1

for i in range(1,a+b+1,1):
    upper *= i
    upper %= MOD

for i in range(1,a+1,1):
    lower *= i
    lower %= MOD

for i in range(1,b+1,1):
    lower *= i
    lower %= MOD



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

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 div(a, b):
    return mul(a, power(b, MOD-2))

answer = div(upper,lower)

print(answer)

Solution (Python):



def nCr(n,r):
  if n < 0 or r < 0 or n < r: return 0
  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
        
  ret = 1
  for k in range(r):
    if numerator[k] > 1: ret *= int(numerator[k])
      
  return ret


def main():
    X, Y = map(int, input().split())
    if (2*Y - X) % 3 != 0:
        print(0)
    else:
        A = (2*Y - X) // 3
        B = Y - 2*A
        print(nCr(A+B,A)%(10**9+7))


if __name__ == "__main__":
    main()

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

class CombTools(object):
    def __init__(self, cap: int, mod: int):
        self.cap = cap
        self.mod = mod
        self.inv = self._calc_inv()
        self.fac = self._calc_fac()
        self.fac_inv = self._calc_fac_inv()

    def _calc_inv(self):
        inv = [0, 1]
        for i in range(2, self.cap+1):
            inv.append((self.mod - (self.mod // i) * inv[self.mod % i]) % self.mod)

        return inv

    def _calc_fac(self):
        fac = [1]
        for i in range(1, self.cap+1):
            fac.append((i * fac[-1]) % self.mod)

        return fac

    def _calc_fac_inv(self):
        fac_inv = [1]
        for i in range(1, self.cap+1):
            fac_inv.append((self.inv[i] * fac_inv[-1]) % self.mod)

        return fac_inv

    def nCr(self, n: int, r: int):
        
        if r > n:
            raise ValueError("n must be larger than r (n={}, r={})".format(n, r))

        
        return self.fac[n] * self.fac_inv[n-r] * self.fac_inv[r] % self.mod

x, y = li()
MOD = 10**9 + 7

ct = CombTools(10**6, MOD)
n = (x+y) // 3

if (x+y)%3 != 0:
    print(0)
elif x < n or x > 2*n:
    print(0)
else:
    print(ct.nCr((x+y) // 3, x - (x+y) // 3))

Solution (Python):
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()

p,q=(2*Y-X)//3,(2*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

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

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

print(cmb(p+q,p,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 
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()))
u = [1, 2]
r = [2, 1]
flag = True
for i in range(Y//2+1):
    if i*1+2*(Y-i*2)==X:
        flag = False
        N = i+Y-i*2
        print(cmb(N, i, 10**9+7))
        
if flag:
    print(0)