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
input = lambda: sys.stdin.readline().rstrip() 

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

    def combMod(n, r, p):
        numer = 1
        denom = 1
        for i in range(1, r+1):
            numer = numer * (n-r+i) % p
            denom = denom * i % p
        return numer * pow(denom, p-2, p) % p

    if (X+Y)%3==0 and Y<=X*2 and X<=Y*2:
        n = (X+Y)//3
        r = X-(X+Y)//3
        print(combMod(n, r, p))
    else:
        print('0')

Submission status: Wrong Answer

Solution code:
# 組み合わせ計算
# 約分してから計算するアルゴリズム
# 10^9 + 7 で割ったあまりを返すバージョン
MOD = 1000000007

def combination(n, r):
    # n >= 0, r >= 0, r <= n を仮定
    if n < 0 or r < 0 or n < r:
        return -1

    # r <= n - r を仮定
    if n - r < r:
        r = n - r

    # r < 2 の場合
    if r == 0:
        return 1
    if r == 1:
        return n

    # nCr の分子
    # [n - r + 1, n - r + 2, ... , n - 1, n]
    numerator = [n - r + 1 + i for i in range(r)]


    # nCr の分母
    # [1, 2, ... , r - 1, r]
    denominator = [i + 1 for i in range(r)]

    # nCr を約分する
    for i in range(r):
        # i番目の分母で約分する
        # 0番目は1なので省略

        # 元の（今までの約分を行う前の）i番目の分母の値
        original_denominator = i + 1

        # 今までの約分を行った後の、i番目の分母の値
        # 今回の約分で割る数
        divisor = denominator[i]

        # divisorが1（全て約分済み）の場合は無視する
        if divisor == 1:
            continue

        # 約分する相手の分子を決める
        # 0～i番目の分子（約分前）の中で、
        # original_denominatorで割り切れる数
        # （一つしかない）を選ぶ
        # remainder = i番目の分子 % original_denominator
        # とするとき、
        # (i - remainder)番目の分子 = i番目の分子 - remainder
        # (i - remainder)番目の分子 % original_denominator
        #  = (i番目の分子 - remainder) % original_denominator
        #  = 0
        # なので、(i - remainder)番目の分子を選べばよい。

        # remainder = i番目の分子 % original_denominator
        #           = (n - r + i + 1) % (i + 1)
        #           = (n - r) % (i + 1)
        remainder = (n - r) % original_denominator

        # divisor で約分できるペアをすべて約分する
        for j in range(i, r, original_denominator):
            numerator[j - remainder] = numerator[j - remainder] // divisor
            denominator[j] = denominator[j] // divisor

    ans = 1
    for i in numerator:
        ans = (ans * i) % MOD
    return ans

X, Y = map(int, input().split())
if X < Y:
    d = Y - X
    n = X
else:
    d = X - Y
    n = Y

# (n, n + d)
if (n - d) % 3 != 0:
    print(0)
else:
    print(combination((2*n + d) // 3, (n - d) // 3))

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)


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, readline().split())
if (x + y) % 3 != 0 or not (x <= 2 * y) and (y <= 2 * x):
    print(0)
else:
    n = (x + y) // 3
    print(combination(n, y - n))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
#import bisect
#from collections import deque
import math

def input():
    return sys.stdin.readline().rstrip('\n')


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

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

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

M = max((2*X-Y)//3, (2*Y-X)//3)
N = min((2*X-Y)//3, (2*Y-X)//3)
mod = pow(10,9) + 7

#from scipy.special import comb
#print(comb(M+N,N) % (pow(10,9) + 7))
#print(comb(M+N,N, exact=True)% (pow(10,9) + 7))  causes "RE" at atcoder
#from math import factorial
#print((factorial(M+N)//factorial(M)//factorial(N)))

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

print(ohmycomb(M+N, N, mod))

Submission status: Wrong Answer

Solution code:
X,Y = map(int,input().split())
dp = [[0 for i in range(Y+10)] for _ in range(X+10)]
dp[2][1] = 1
dp[1][2] = 1

for i in range(2,X+1):
    for j in range(max(2,i//2),min(Y+1,i*2+1)):
        dp[i][j] = (dp[i-2][j-1] + dp[i-1][j-2]) % (10**9+7)
        print(dp[i][j],i,j)

print(dp[X][Y])

Submission status: Wrong Answer

Solution code:
import numpy as np

def nCr(n,r):
    if r*2>n:
        return nCr(n,n-r)
    mod = 10 ** 9 + 7
    a = b = 1
    for i in range(r):
        a = (a * (n - i)) % mod
        b = (b * (r - i)) % mod
    return (a * pow(b,mod-2,mod))%mod

X,Y = map(int,input().split())
if (X+Y)%3 != 0:
    print(0)
else:
    left = np.array([[1,2],[2,1]])
    right = np.array([X,Y])
    n = int(np.linalg.solve(left,right)[0])
    m = int(np.linalg.solve(left,right)[1])

    print(nCr(n+m,n))

Submission status: Wrong Answer

Solution code:
mod = 10**9+7
n = 10**6

def comb(n, r):
    if 2 * r > n: return comb(n, n-r)
    return fac[n] * inv[r] * inv[n-r] % mod

x, y = map(int, input().split())
if (x+y) % 3 != 0:
  print(0)
  exit()

fac = [1] * (n+2)
inv = [1] * (n+2)
for i in range(2, n+1):
    fac[i] = fac[i-1] * i % mod
inv[n] = pow(fac[n], mod - 2, mod)
for i in range(n-1, 1, -1):
    inv[i] = inv[i + 1] * (i + 1) % mod
     
a = -(x-2*y)//3
b = (2*x-y)//3  
print(comb(a+b, a) % mod)

Submission status: Wrong Answer

Solution code:
mod = 10**9+7
n = 10**6

def comb(n, r):
    if 2 * r > n: return comb(n, n-r)
    return fac[n] * inv[r] * inv[n-r] % mod

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

fac = [1] * (n+2)
inv = [1] * (n+2)
for i in range(2, n+1):
    fac[i] = fac[i-1] * i % mod
inv[n] = pow(fac[n], mod - 2, mod)
for i in range(n-1, 1, -1):
    inv[i] = inv[i + 1] * (i + 1) % mod
     
a = -(x-2*y)//3
b = (2*x-y)//3  
print(comb(a+b, a) % mod)

Submission status: Wrong Answer

Solution code:
i, j = map(int, input().split())

if (i + j) % 3 != 0 or (max(i, j) / min(i, j) >= 2):
  print(0)
else:
  TW = (i + j) // 3
  W = (TW - (i-j)) // 2

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

  print(mod_cmb(TW, W))

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
x, y = map(int, input().split())
a = (2 * y - x) // 3
b = (2 * x - y) // 3
if (x + y) % 3 != 0 or a < 0 or b < 0:
    print(0)
    exit()
n = a + b
r = min(a, b)
lim = 10 ** 6
g1 = [1, 1]
g2 = [1, 1]
inv = [0, 1]
for i in range(2, lim + 1):
    g1.append((g1[-1] * i) % mod)  # 元テーブル
    inv.append((inv[mod % i] * (mod // i)) % mod)  # 逆元テーブル
    g2.append((g2[-1] * inv[-1]) % mod)  # 逆元テーブル計算用テーブル


def comb(n, r, mod):
    if r < 0 or r > n:
        return 0
    else:
        return g1[n] * g2[r] * g2[n - r] % mod


ans = comb(n, r, mod)
print(ans)

Submission status: Wrong Answer

Solution code:
def mod_pow(n, m, mod):
    if m == 0:
        return 1
    if m == 1:
        return n
    res = 1
    if m % 2 == 1:
        res = n

    return ((mod_pow((n * n) % mod, m // 2, mod) % mod) * res) % mod


def mod_frac(n, mod):
    res = 1

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

    return res


def mod_comb(n, r, mod):
    mul1 = mod_frac(n, mod)
    mul2 = mod_pow(mod_frac(r, mod), mod - 2, mod)
    mul3 = mod_pow(mod_frac(n - r, mod), mod - 2, mod)

    return (((mul1 * mul2) % mod) * mul3) % mod


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

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

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

mod = 10 ** 9 + 7

print(mod_comb(n + m, n, mod))

Submission status: Wrong Answer

Solution code:
def mod_pow(n, m, mod):
    if m == 0:
        return 1
    if m == 1:
        return n
    res = 1
    if m % 2 == 1:
        res = n

    return ((mod_pow((n * n) % mod, m // 2, mod) % mod) * res) % mod


def mod_frac(n, mod):
    res = 1

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

    return res


def mod_comb(n, r, mod):
    mul1 = mod_frac(n, mod)
    mul2 = mod_pow(mod_frac(r, mod), mod - 2, mod)
    mul3 = mod_pow(mod_frac(n - r, mod), mod - 2, mod)

    return (((mul1 * mul2) % mod) * mul3) % mod


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

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

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

mod = 10 ** 9 + 7

print(mod_comb(n + m, n, mod))

Submission status: Wrong Answer

Solution code:
def gho1(x):
  g=1
  for i in range(1,x+1):
    g*=i
  return g
def gho2(x):
  g=1
  for i in range(1,x+1):
    g*=i;g=g%(10**9+7)
  return g

q,w=map(int,input().split())
e,d=divmod((2*w-q),3)
r,f=divmod((2*q-w),3)

if d!=0 or f!=0:print(0)
else:
  print((gho2(e+r)//(gho2(e)*gho2(r)))%(10**9+7))

Submission status: Wrong Answer

Solution code:
def gho1(x):
  g=1
  for i in range(1,x+1):
    g*=i
  return g
def gho2(x):
  g=1
  for i in range(1,x+1):
    g*=i;g=g%(10**9+7)
  return g

q,w=map(int,input().split())
e,d=divmod((2*w-q),3)
r,f=divmod((2*q-w),3)

if d!=0 or f!=0:print(0)
else:
  print((gho2(e+r)//(gho1(e)*gho1(r)))%(10**9+7))

Submission status: Wrong Answer

Solution code:
def gho1(x):
  g=1
  for i in range(1,x+1):
    g*=i
  return g
def gho2(x):
  g=1
  for i in range(1,x+1):
    g*=i;g=g%(10**9+7)
  return g

q,w=map(int,input().split())
e,d=divmod((2*w-q),3)
r,f=divmod((2*q-w),3)

if d!=0 or f!=0:print(0)
else:
  print((gho2(e+r)//(gho1(e)*gho1(r)))%(10*9+7))

Submission status: Wrong Answer

Solution code:
def gho(x):
  g=1
  for i in range(1,x+1):
    g*=i
  return g

q,w=map(int,input().split())
e,d=divmod((2*w-q),3)
r,f=divmod((2*q-w),3)

if d!=0 or f!=0:print(0)
else:
  print((gho(e+r)%(10**9+7)//(gho(e)*gho(r)))%(10**9+7))

Submission status: Wrong Answer

Solution code:
x,y = map(int,input().split())
flag = True
for j in range(1,x//2+1):
    nup = j*2
    nright = (x-nup)
    if( j+2*nright == y)&(nright >= 0):
        flag = False
        break
        
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 flag == False:
    a = cmb((j+nright),j ,mod)
    print(a)
    
else:
    print(0)

Submission status: Wrong Answer

Solution code:
x,y = map(int,input().split())
flag = True
for j in range(1,x//2+1):
    nup = j*2
    nright = (x-nup)
    if j+2*nright == y:
        flag = False
        break
        
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 flag == False:
    a = cmb((j+nright),j ,mod)
    print(a)
    
else:
    print(0)

Submission status: Wrong Answer

Solution code:
x,y=map(int,input().split())
if (x+y)%3!=0:
    print(0)
    exit()

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

if x%2!=0 or x%3==0:
    num=min(x,y)//3
    print(com(2*num+1,num) if x!=y else com(2*num,num))
else:
    print(1)

Submission status: Wrong Answer

Solution code:
x,y=map(int,input().split())
if (x+y)%3!=0:
    print(0)
    exit()

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

if x%3==0:
    num=min(x,y)//3
    print(com(2*num+1,num) if x!=y else com(2*num,num))
else:
    print(1)

Submission status: Wrong Answer

Solution code:
x,y=map(int,input().split())
if (x+y)%3!=0:
    print(0)
    exit()

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

n=min(x,y)//3
print(com(2*n+1,n))

Submission status: Wrong Answer

Solution code:
x,y=map(int,input().split())
if (x+y)%3!=0:
    print(0)
    exit()

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

n=x//3
print(com(2*n+1,n))

Submission status: Wrong Answer

Solution code:
import sys
X,Y=map(int,input().split())
if (X+Y)%3!=0:
    print(0)
    sys.exit()
mod=10**9+7
A=(X+Y)//3
B=min(X,Y)-A

ans=1
for i in range(B):
    den=i+1
    num=A-i
    ans=ans*num*pow(den,mod-2,mod)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
X,Y=map(int,input().split())
if (X+Y)%3!=0:
    print(0)
    sys.exit()
mod=10**9+7
A=(X+Y)//3
B=min(X,Y)-A

comb=[1]
for i in range(B):
    den=i+1
    num=A-i
    ans=comb[i]*num*pow(den,mod-2,mod)%mod
    comb.append(ans)
print(comb[-1])

Submission status: Wrong Answer

Solution code:
import sys

x,y=map(int,input().split())
d=x-y
if x>=y:
    x-=2*d
    y-=d
else:
    x-=d
    y-=2*d
if x%3!=0 or x<0:
    print(0)
    sys.exit()
cnt=x//3

mod=10**9+7
N=7*10**5+10
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 cmb(n,r):
    if (r<0 or r>n):
        return 0
    r=min(r,n-r)
    return g1[n]*g2[r]*g2[n-r]%mod

print(cmb(2*cnt+d,cnt))

Submission status: Wrong Answer

Solution code:
X, Y = map(int, input().split())
x, y = (X*2-Y)//3, (Y*2-X)//3
mod = 10**9+7
n = min(x, y)

factorial = [1]
for i in range(1, n+1):
    factorial.append((factorial[i-1]*i)%mod)

p_list = [(x+y)%mod]
for i in range(1, n+1):
    p_list.append((p_list[i-1]*(x+y-i))%mod)
    
if (X+Y)%3 != 0:
    print (0)
elif X*2-Y < 0 or Y*2-X < 0:
    print (0)
else:
    print (p_list[n-1]*pow(factorial[n], 10**9+5, mod)%mod)

Submission status: Wrong Answer

Solution code:
X, Y = map(int, input().split())
x, y = (X*2-Y)//3, (Y*2-X)//3
mod = 10**9+7
n = min(x, y)

factorial = [1]
for i in range(1, n+1):
    factorial.append((factorial[i-1]*i)%mod)

p_list = [(x+y)%mod]
for i in range(1, n+1):
    p_list.append((p_list[i-1]*(x+y-i))%mod)
    
if (X+Y)%3 != 0:
    print (0)
elif x < 0 or y < 0:
    print (0)
else:
    print (p_list[n-1]*pow(factorial[n], 10**9+5, mod)%mod)

Submission status: Wrong Answer

Solution code:



def factorial(x, modp):
    curr = 1
    for i in range(1, x + 1):
        curr *= i
        curr %= modp
    return curr


def power(x, n, modp):
    if n == 0:
        return 1

    k = 1
    while n > 1:
        if n % 2:
            k *= x
            k %= modp
        x *= x
        x %= modp
        n //= 2
    
    return k * x % modp


def submit():
    x, y = map(int, input().split())
    
    # (i, j) -> (i + 1, j + 2)の移動方法をs回
    # (i, j) -> (i + 2, j + 1)の移動方法をt回行なったとする
    # t = Y - 2s, s = (2Y - X) / 3となる
    s = 2*y - x
    if s % 3 != 0: # 移動方法なし
        print(0)
        return
    s //= 3

    t = y - 2*s
    if t < 0: # 移動方法なし
        print(0)
        return

    # 全移動n = s + t中のs移動のパターンを数え上げれば良い
    # n_C_sを求めれば良い
    # n! / s! (n - s)! ≡ n! (s!)^modp-2 ((n - s)!)^modp-2
    n = s + t
    modp = 10**9 + 7
    ans = factorial(n, modp) * power((factorial(s, modp)), modp - 2, modp) * power(factorial(n - s, modp), modp - 2, modp)
    ans %= modp
    print(ans)
    
    
if __name__ == "__main__":
    submit()

Submission status: Wrong Answer

Solution code:
import sys

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(x) for x in input().split())

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

if (2*Y-X) % 3 == 0:
  n = (2*Y-X) // 3  
else:
  n = -1

if (2*X-Y) % 3 == 0:
  m = (2*X-Y) // 3
else:
  m = -1

if n < 0 or m < 0:
  print(0)
  sys.exit()
print(int(cmb(n+m,n)))

Submission status: Wrong Answer

Solution code:
import sys

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(x) for x in input().split())

if (X+Y) % 3 != 0:
  print(0)
  sys.exit()
  
n = (2*Y-X) // 3 if (2*Y-X) % 3 == 0 else -1
m = (2*X-Y) // 3 if (2*X-Y) % 3 == 0 else -1
if n < 0 or m < 0:
  print(0)
  sys.exit()
print(int(cmb(n+m,n)))

Submission status: Wrong Answer

Solution code:
import sys

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(x) for x in input().split())

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

Submission status: Wrong Answer

Solution code:
import sys

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(x) for x in input().split())

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

print(int(cmb(n+m,n)))

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from collections import defaultdict
from copy import deepcopy

mod = 10 ** 9 + 7
t = time.time()

def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

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

def main():
    X, Y = iip()
    aa = (int(-X) + int(2*Y)) // 3
    bb = (int(-Y) + int(2*X)) // 3
    if (int(-X) + int(2*Y)) % 3 != 0 or  (int(-Y) + int(2*X)) % 3 != 0:
        print(0)
        exit()

    if X > 2*Y or Y > 2*X:
        print(0)
        exit()

    a = min(aa, bb)
    b = max(aa, bb)

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

    #print(npr)

    bunbo = 1
    for i in range(1, a+1):
        #print(f"i:{i}")
        bunbo *= i
        bunbo = bunbo % mod

    #print(bunbo)

    ans = npr * power(bunbo, mod-2)
    ans = ans % mod
    print(ans)





main()

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from collections import defaultdict
from copy import deepcopy

mod = 10 ** 9 + 7
t = time.time()

def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

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

def main():
    X, Y = iip()
    aa = (int(-X) + int(2*Y)) // 3
    bb = (int(-Y) + int(2*X)) // 3

    if X > 2*Y or Y > 2*X or (aa, bb) == (0, 0):
        print(0)
        exit()

    a = min(aa, bb)
    b = max(aa, bb)

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

    #print(npr)

    bunbo = 1
    for i in range(1, a+1):
        #print(f"i:{i}")
        bunbo *= i
        bunbo = bunbo % mod

    #print(bunbo)

    ans = npr * power(bunbo, mod-2)
    ans = ans % mod
    print(ans)





main()

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from collections import defaultdict
from copy import deepcopy

mod = 10 ** 9 + 7
t = time.time()

def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

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

def main():
    X, Y = iip()
    aa = (int(-X) + int(2*Y)) // 3
    bb = (int(-Y) + int(2*X)) // 3

    if X > 2*Y or Y > 2*X:
        print(0)
        exit()

    a = min(aa, bb)
    b = max(aa, bb)

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

    #print(npr)

    bunbo = 1
    for i in range(1, a+1):
        #print(f"i:{i}")
        bunbo *= i
        bunbo = bunbo % mod

    #print(bunbo)

    ans = npr * power(bunbo, mod-2)
    ans = ans % mod
    print(ans)





main()

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from collections import defaultdict
from copy import deepcopy

mod = 10 ** 9 + 7
t = time.time()

def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

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

def main():
    X, Y = iip()
    aa = (int(-X) + int(2*Y)) // 3
    bb = (int(-Y) + int(2*X)) // 3

    if X > 2*Y or Y > 2*X:
        print(0)
        exit()

    a = min(aa, bb)
    b = max(aa, bb)

    if a == 0:
        print(1)
        exit()
    #print(a)
    #print(b)

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

    #print(npr)

    bunbo = 1
    for i in range(1, a+1):
        #print(f"i:{i}")
        bunbo *= i
        bunbo = bunbo % mod

    #print(bunbo)

    ans = npr * power(bunbo, mod-2)
    ans = ans % mod
    print(ans)





main()

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from collections import defaultdict
from copy import deepcopy

mod = 10 ** 9 + 7
t = time.time()

def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

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

def main():
    X, Y = iip()
    aa = (int(-X) + int(2*Y)) // 3
    bb = (int(-Y) + int(2*X)) // 3

    if X > 2*Y or Y > 2*X:
        print(0)
        exit()

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

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


    a = min(aa, bb)
    b = max(aa, bb)

    if a == 0:
        print(1)
        exit()
    #print(a)
    #print(b)

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

    #print(npr)

    bunbo = 1
    for i in range(1, a+1):
        #print(f"i:{i}")
        bunbo *= i
        bunbo = bunbo % mod

    #print(bunbo)

    ans = npr * power(bunbo, mod-2)
    ans = ans % mod
    print(ans)





main()

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from collections import defaultdict
from copy import deepcopy

mod = 10 ** 9 + 7
t = time.time()

def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

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

def main():
    X, Y = iip()
    aa = (int(-X) + int(2*Y)) // 3
    bb = (int(-Y) + int(2*X)) // 3

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

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


    a = min(aa, bb)
    b = max(aa, bb)

    if a == 0:
        print(1)
        exit()
    #print(a)
    #print(b)

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

    #print(npr)

    bunbo = 1
    for i in range(1, a+1):
        #print(f"i:{i}")
        bunbo *= i
        bunbo = bunbo % mod

    #print(bunbo)

    ans = npr * power(bunbo, mod-2)
    ans = ans % mod
    print(ans)





main()

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from collections import defaultdict
from copy import deepcopy

mod = 10 ** 9 + 7
t = time.time()

def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

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

def main():
    X, Y = iip()
    aa = (-X + 2*Y) // 3
    bb = (-Y + 2*X) // 3

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


    a = min(aa, bb)
    b = max(aa, bb)

    if a == 0:
        print(1)
        exit()
    #print(a)
    #print(b)

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

    #print(npr)

    bunbo = 1
    for i in range(1, a+1):
        #print(f"i:{i}")
        bunbo *= i
        bunbo = bunbo % mod

    #print(bunbo)

    ans = npr * power(bunbo, mod-2)
    ans = ans % mod
    print(ans)





main()

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from collections import defaultdict
from copy import deepcopy

mod = 10 ** 9 + 7
t = time.time()

def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

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

def main():
    X, Y = iip()
    aa = (-X + 2*Y) // 3
    bb = (-Y + 2*X) // 3

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


    a = min(aa, bb)
    b = max(aa, bb)

    if a == 0:
        print(0)
        exit()
    #print(a)
    #print(b)

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

    #print(npr)

    bunbo = 1
    for i in range(1, a+1):
        #print(f"i:{i}")
        bunbo *= i
        bunbo = bunbo % mod

    #print(bunbo)

    ans = npr * power(bunbo, mod-2)
    ans = ans % mod
    print(ans)





main()

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from collections import defaultdict
from copy import deepcopy

mod = 10 ** 9 + 7
t = time.time()

def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

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

def main():
    X, Y = iip()
    aa = (-X + 2*Y) // 3
    bb = (-Y + 2*X) // 3

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

    a = min(aa, bb)
    b = max(aa, bb)

    #print(a)
    #print(b)

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

    #print(npr)

    bunbo = 1
    for i in range(1, a+1):
        #print(f"i:{i}")
        bunbo *= i
        bunbo = bunbo % mod

    #print(bunbo)

    ans = npr * power(bunbo, mod-2)
    ans = ans % mod
    print(ans)





main()

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from collections import defaultdict
from copy import deepcopy

mod = 10 ** 9 + 7
t = time.time()

def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

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

def main():
    X, Y = iip()
    aa = (-X + 2*Y) // 3
    bb = (-Y + 2*X) // 3

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

    a = min(aa, bb)
    b = max(aa, bb)

    #print(a)
    #print(b)

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

    #print(npr)

    bunbo = 1
    for i in range(1, a+1):
        #print(f"i:{i}")
        bunbo *= i
        bunbo = bunbo % mod

    #print(bunbo)

    ans = npr * power(bunbo, mod-2)
    ans = ans % mod
    print(ans)





main()

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

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


def combination(n, r, mod):
    r = min(r, n-r)

    numer = 1
    denom = 1

    for i in range(r):
        numer = numer * (n-i) % mod
        denom = denom * (r-i) % mod

    return numer * pow(denom, mod-2, mod) % mod

m_n = (X+Y) // 3
m = X - m_n
n = Y - m_n

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

print(combination(m_n, m, mod))