Problem: 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.

Submission attempts (in order):

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


MOD = 10**9 + 7

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

pascal_depth = int((X + Y) / 3)  
x, y = int((X + Y) * 2 / 3), (X + Y) / 3
pascal_k = x - X  

if pascal_k > pascal_depth / 2:
    pascal_k = pascal_depth - pascal_k


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 = pascal_depth
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)


def mod_combination(n, k, mod):
    # nCk (mod m)
    def mod_permutation(n, k, mod):
        if n <= k:
            return 1
        else:
            return (n * mod_permutation(n - 1, k, mod)) % mod

    def mod_inv_permutation(k, mod):
        k, mod = int(k), int(mod)
        if k <= 1:
            return 1
        else:
            return (pow(k, mod - 2, mod) * mod_inv_permutation(k - 1, mod)) % mod

    return (mod_permutation(n, n - k, mod) * mod_inv_permutation(k, mod)) % mod


a = mod_combination(pascal_depth, pascal_k, MOD)
print(a)



import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


MOD = 10**9 + 7

X, Y = read_ints()
if (X + Y) % 3 != 0 or Y / X > 2 or Y / X < 1 / 2:  # !=0
    print(0)
    exit()

pascal_depth = int((X + Y) / 3)  
x = ((X + Y) * 2) // 3
pascal_k = x - X  


def combination(n, r, mod=MOD):
    r = min(r, n - r)
    nf = rf = 1
    for i in range(r):
        nf = nf * (n - i) % mod
        rf = rf * (i + 1) % mod
    return nf * pow(rf, mod - 2, mod) % mod




#         return 1

#     mod = 10**9 + 7
#     inv = [0, 1]
#     for c in range(2, k + 1):
#         inv.append((-(mod // c) * inv[mod % c]) % mod)

#     return s


a = combination(pascal_depth, pascal_k, MOD)
print(a)


Verdicts: Runtime Error

Accepted