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:

from collections import deque

def power(base, exp):
    """ Fast power calculation using repeated squaring """
    if exp < 0:
        return 1 / power(base, -exp)
    ans = 1
    while exp:
        if exp & 1:
            ans *= base
        exp >>= 1
        base *= base
    return ans

def parse():
  return map(int, input().split(" "))

def print_grid(grid, X, Y):
  Y = len(grid)
  X = len(grid[0])
  for y in range(Y):
    for x in range(X):
      if grid[y][x]:
        print(1, end="")
      else:
        print(0, end="")
    print("")
  print("")

def main():
  X, Y = parse()

  start_X = 2
  start_Y = 2
  end_X = X + start_X
  end_Y = Y + start_Y

  visited = {}
  # visited = [[False for x in range(X + 1 + 4)] for y in range(Y + 1 + 4)]

  # 到達不可能かどうかの判定
  if X == 0 or Y == 0:
    print(0)
    exit(0)
  elif Y > X + 2:
    print(0)
    exit(0)
  elif X > Y + 2:
    print(0)
    exit(0)

  # 枠外は移動済みとする
  for y in range(Y + 1 + 4):  # ↓
    visited["{0},{1}".format(0, y)] = None
    visited["{0},{1}".format(1, y)] = None
    visited["{0},{1}".format(end_X + 1, y)] = None
    visited["{0},{1}".format(end_X + 2, y)] = None
  for x in range(X + 1 + 4):  # →
    visited["{0},{1}".format(x, 0)] = None
    visited["{0},{1}".format(x, 1)] = None
    visited["{0},{1}".format(x, end_Y + 1)] = None
    visited["{0},{1}".format(x, end_Y + 2)] = None

  # print("visited")

  # for y in range(Y + 1 + 4):
  #   for x in range(X + 1 + 4):
  #     if y < start_Y or x < start_X or x > end_X or y > end_Y:
  #       visited["{0},{1}".format(x, y)] = None
        # visited[y][x] = True
  # print_grid(visited)
  # exit(0)

  # 深さ優先探索
  ans = 0
  stack = deque([(start_X, start_Y)])
  while len(stack) > 0:
    x, y = stack.pop()
    if visited.get("{0},{1}".format(x, y), 0) == 0:
    # if not visited[y][x]:
      visited["{0},{1}".format(x, y)] = None
      # visited[y][x] = True
      stack.append((x + 1, y + 2))
      stack.append((x + 2, y + 1))
    
    if y == end_Y and x == end_X:
      ans += 1

    # print_grid(visited)
  print(ans % (power(10, 9) + 7))

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:

from collections import deque

def power(base, exp):
    """ Fast power calculation using repeated squaring """
    if exp < 0:
        return 1 / power(base, -exp)
    ans = 1
    while exp:
        if exp & 1:
            ans *= base
        exp >>= 1
        base *= base
    return ans

def parse():
  return map(int, input().split(" "))

def print_grid(grid, X, Y):
  Y = len(grid)
  X = len(grid[0])
  for y in range(Y):
    for x in range(X):
      if grid[y][x]:
        print(1, end="")
      else:
        print(0, end="")
    print("")
  print("")

def main():
  X, Y = parse()

  start_X = 2
  start_Y = 2
  end_X = X + start_X
  end_Y = Y + start_Y

  visited = {}
  # visited = [[False for x in range(X + 1 + 4)] for y in range(Y + 1 + 4)]

  # 到達不可能かどうかの判定
  if X == 0 or Y == 0:
    print(0)
    exit(0)
  elif Y >= X + 2:
    print(0)
    exit(0)
  elif X >= Y + 2:
    print(0)
    exit(0)

  # 枠外は移動済みとする
  for y in range(Y + 1 + 4):  # ↓
    visited["{0},{1}".format(0, y)] = None
    visited["{0},{1}".format(1, y)] = None
    visited["{0},{1}".format(end_X + 1, y)] = None
    visited["{0},{1}".format(end_X + 2, y)] = None
  for x in range(X + 1 + 4):  # →
    visited["{0},{1}".format(x, 0)] = None
    visited["{0},{1}".format(x, 1)] = None
    visited["{0},{1}".format(x, end_Y + 1)] = None
    visited["{0},{1}".format(x, end_Y + 2)] = None

  # print("visited")

  # for y in range(Y + 1 + 4):
  #   for x in range(X + 1 + 4):
  #     if y < start_Y or x < start_X or x > end_X or y > end_Y:
  #       visited["{0},{1}".format(x, y)] = None
        # visited[y][x] = True
  # print_grid(visited)
  # exit(0)

  # 深さ優先探索
  ans = 0
  stack = deque([(start_X, start_Y)])
  while len(stack) > 0:
    x, y = stack.pop()
    if visited.get("{0},{1}".format(x, y), 0) == 0:
    # if not visited[y][x]:
      visited["{0},{1}".format(x, y)] = None
      # visited[y][x] = True
      stack.append((x + 1, y + 2))
      stack.append((x + 2, y + 1))
    
    if y == end_Y and x == end_X:
      ans += 1

    # print_grid(visited)
  print(ans % (power(10, 9) + 7))

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
# 21:50まで

# (X, Y)に移動させる移動方法は何通り？
# 移動方法は2パターン
#   (i + 1, j + 2) か (i + 2, j + 1)
#  移動させる組み合わせは、上記2種のパターン組み合わせ個数による

# 全パターン探索する場合
# 深さ優先探索 計算量ヤバめ

# (0, 0) -> (X, Y)
# (X, Y) に行くためには、
# (X-1, Y-2)
# (X-2, Y-1)
# 両方からいける
# の3通り

from collections import deque


def main():
    X, Y = map(int, input().rstrip().rsplit())

    if Y > 2*X or Y < X / 2:
        print(0)
        return

    if Y == 2*X or Y == X / 2:
        print(1)

    stack = deque([(0, 0)])

    cnt = 0
    while len(stack) > 0:
        x, y = stack.pop()

        if x == X and y == Y:
            cnt += 1
            continue

        if x > X or y > Y:
            continue

        stack.append((x+1, y+2))
        stack.append((x+2, y+1))

    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from scipy.misc import comb
x, y = map(int, input().split())
if (x + y) % 3 != 0:
    print(0)
    exit()
a, b = y - (x + y) // 3, x - (x + y) // 3
print(comb(a + b, a))

Submission status: Wrong Answer

Solution code:
MOD = 10**9+7

class ModInt:
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    __repr__ = __str__

    def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

    def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

    def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

    def __truediv__(self, other):
        return (
            ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD))
        )

    def __pow__(self, other):
        return (
            ModInt(pow(self.x, other.x, MOD)) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        )

    __radd__ = __add__

    def __rsub__(self, other):
        return (
            ModInt(other.x - self.x) if isinstance(other, ModInt) else
            ModInt(other - self.x)
        )

    __rmul__ = __mul__

    def __rtruediv__(self, other):
        return (
            ModInt(
                other.x * pow(self.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(other * pow(self.x, MOD - 2, MOD))
        )

    def __rpow__(self, other):
        return (
            ModInt(pow(other.x, self.x, MOD)) if isinstance(other, ModInt) else
            ModInt(pow(other, self.x, MOD))
        )


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

if (2*X-Y)%3!=0 and (2*Y-X)%3!=0:
    print(0)
else:
    a = (2*X-Y)//3
    b = (2*Y-X)//3
    factb = [1]
    for n in range(1,b+1):
        factb.append(factb[-1]*n%MOD)
    temp = cmb(a+b,a,MOD)
    print(ModInt(temp)/ModInt(factb[-1]))

Submission status: Wrong Answer

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

MOD = 10**9 + 7

def comb(n, r):
    n += 1
    over = 1
    under = 1
    for i in range(1,r + 1):
      over = over * (n - i) % MOD
      under = under * i % MOD
    return over * pow(under,MOD - 2,MOD) % MOD

if (X + Y) % 3 != 0:
    print(0)
else:
    a = (2 * X - Y) // 3
    b = (2 * Y - X) // 3
    print(comb(a+b, a))

Submission status: Wrong Answer

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

MOD = 10**9 + 7

def comb(n, r):
    n += 1
    over = 1
    under = 1
    for i in range(1,r + 1):
      over = over * (n - i) % MOD
      under = under * i % MOD
    return over * pow(under,MOD - 2,MOD) % MOD

if (X + Y) % 3 != 0:
    print(0)
else:
    diff = Y - X
    X -= diff
    one = X // 3 + diff
    two = X // 3
    print(comb(one+two, one))

Submission status: Wrong Answer

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

MD = pow(10, 9) + 7

def choose(n, a):
    x, y = 1, 1
    for i in range(a):
        x = x * (n - i) % MD
        y = y * (i + 1) % MD
    return x * pow(y, MD - 2, MD) % MD

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

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

print(choose(a+b, min(a,b)))

Submission status: Wrong Answer

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

MD = pow(10, 9) + 7


def myfactorial(n):
    ans = 1
    for i in range(n):
        ans = ans * (n-i) % MD
    return ans


def mycombination(n, r):
    ans = 0
    if n < 0 or r < 0:
        # print(n, r)
        return ans
    # r = min(r, n-r)
    ans = myfactorial(n)//(myfactorial(n-r) * myfactorial(r))
    return ans


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

a = (2*Y-X)//3
b = (Y-2*X)//-3
# if a == 0 and b == 0:
#     print(0)
#     import sys
#     sys.exit()

print(mycombination((a+b), a))

# from math import factorial
# print(factorial(a+b)//factorial(a)*factorial(b)%(10e9+7))
# print(factorial(a+b)//(factorial(a+b-a)*factorial(a))

# dp = [[0 for _ in range(X+1)] for __ in range(Y+1)]
# dp[1][2] = 1
# dp[2][1] = 1

# for xi in range(X + 1):
#     for yi in range(Y + 1):
#         dp[xi][yi] = dp[xi - 1][yi - 2] + dp[xi - 2][yi - 1]


# print(dp[X][Y])
# import sys
# sys.setrecursionlimit(100000)  # RecursionError対策

# def help(x, y):
#     if x < 0 or y < 0:
#         return 0
#     if (x == 1 and y == 2) or (x == 2 and y == 1):
#         return 1
#     else:
#         return (help(x - 1, y - 2) + help(x - 2, y - 1))%(10e9+7)

# print(help(X, Y))

Submission status: Wrong Answer

Solution code:
X, Y = map(int, input().split())
a = 2*Y-X
b = 2*X-Y

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

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

if a%3==0 and b%3==0 and a>0 and b>0:
    a //= 3 
    b //= 3
    print(combination(a+b, a))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
X, Y = map(int, input().split())
a = 2*Y-X
b = 2*X-Y

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

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

if a%3==0 and b%3==0:
    a //= 3 
    b //= 3
    print(combination(a+b, a))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
X, Y = map(int, input().split())
a = (2*Y-X)/3
b = (2*X-Y)/3

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

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

if a.is_integer() and b.is_integer():
    a = int(a)
    b = int(b)
    print(combination(a+b, a))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
N=10**9+7
x , y = map(int, input().split())
a=(2*x-y)//3
b=(2*y-x)//3

if 2*a+b!=x:
    print(0)
    exit()

n=a+b
r=a

def fac(n,r,N):
    ans=1
    for i in range(r):
        ans=ans*(n-i)%N
    return ans

def combi(n,r,N):
    if n<r:
        ans = 0
        return ans
    r= min(r, n-r)
    ans = fac(n,r,N)*pow(fac(r,r,N),N-2,N)%N
    return ans

ans = combi(n,r,N)
print(ans)

Submission status: Wrong Answer

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

factorial = [1]
inverse = [1]

n=a+b
r=a

def fac(n,r,N):
    ans=1
    for i in range(r):
        ans=ans*(n-i)%N
    return ans

def combi(n,r,N):
    r= min(r, n-r)
    ans = fac(n,r,N)*pow(fac(r,r,N),N-2,N)%N
    return ans

ans = combi(n,r,N)
print(ans)

Submission status: Wrong Answer

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

def modinv(a):
    return pow(a,MOD-2,MOD)

x=(2*Y-X)//3
y=(2*X-Y)//3

if x%3!=0 or y%3!=0 or x<0 or y<0:
	print(0)
else:
	r=1
	n=x+y
	k=min(x,y)
	for i in range(k):
		r*=(n-i)
		r*=modinv(i+1)
		r%=MOD
	print(r)

Submission status: Wrong Answer

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

def modinv(a):
    return pow(a,MOD-2,MOD)

if (2*X-Y)%3!=0 or (2*Y-X)%3!=0:
    print(0)
else:
    x=(2*Y-X)//3
    y=(2*X-Y)//3
    r=1
    n=x+y
    k=min(x,y)
    for i in range(k):
        r*=(n-i)
        r*=modinv(i+1)
        r%=MOD
    print(r)

Submission status: Wrong Answer

Solution code:
x,y=map(int,input().split())
if (x+y)%3:print(0)
else:
    sab=(x+y)//3
    a=y-sab
    b=sab-a
    if a<0 or b<0:print(0)
    mod=10**9+7
    fact=[1]*(sab+1)
    inv=[1]*(sab+1)
    for i in range(2,sab+1):
        fact[i]=i*fact[i-1]%mod
    inv[-1]=pow(fact[-1],mod-2,mod)
    for i in range(sab,1,-1):
        inv[i-1]=inv[i]*i%mod
    def comb(x,y):return fact[x]*inv[y]%mod*inv[x-y]%mod if x>=y>=0 else 0
    print(comb(sab,a))

Submission status: Wrong Answer

Solution code:
import math

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

def calc_combination(n, r):

    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 )

    return cmb(n,r,mod)


if x == y:
    if x % 3 == 0:
        move01 = x // 3
        answer = calc_combination(move01+move01, move01)
    else:
        answer = 0
elif x // y == 2 or y // x == 2:
    answer = 1
elif x // y > 2 or y // x > 2:
    answer = 0
else:
    if x < y:
        move0 = min(x, y // 2)
        
        judge_x = x - move0
        judge_y = y - move0*2

        if judge_x % 2 and (judge_x // 2 == judge_y):
            move1 = judge_y
            answer = calc_combination(move0+move1, move1)
        else:
            answer = 0
    if x > y:
        move1 = min(x // 2, y)
        
        judge_x = x - move1*2
        judge_y = y - move1

        if judge_y % 2 and (judge_y // 2 == judge_x):
            move0 = judge_x
            answer = calc_combination(move0+move1, move0)
        else:
            answer = 0

print(answer)

Submission status: Wrong Answer

Solution code:
import math

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

def calc_combination(n, r):

    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 )

    return cmb(n,r,mod)


if x == y:
    if x % 3 == 0:
        move01 = x // 3
        answer = calc_combination(move01+move01, move01)
    else:
        answer = 0
elif x // y == 2 or y // x == 2:
    answer = 1
elif x // y > 2 or y // x > 2:
    answer = 0
else:
    if x < y:
        move0 = min(x, y // 2)
        
        judge_x = x - move0
        judge_y = y - move0*2

        if judge_x % 2 and (judge_x // 2 == judge_y):
            move1 = judge_y
            answer = calc_combination(move0+move1, move1)
        else:
            answer = 0
    if x > y:
        move1 = min(x // 2, y)
        
        judge_x = x - move1*2
        judge_y = y - move1

        if judge_y % 2 and (judge_y // 2 == judge_x):
            move0 = judge_x
            answer = calc_combination(move0+move1, move1)
        else:
            answer = 0

print(answer)

Submission status: Wrong Answer

Solution code:
def main():
    MOD = 10 ** 9 + 7

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

    if (X + Y) % 3:
        print(0)
        return

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

    def choose(n, a, mod):
        x, y = 1, 1
        for i in range(a):
            x = x * (n - i) % mod
            y = y * (i + 1) % mod
        return x * pow(y, mod - 2, mod) % mod

    ans = choose(a + b, min(a, b), mod=MOD)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

num_A, num_B = 0, 0

num_A_r = int(X/3)*2
num_A_d = int(Y/3)
num_B_r = int(X/3)
num_B_d = int(Y/3)*2

num_A = num_A_d
num_B = num_B_r
if num_A_r+num_B_r == X and num_A_d+num_B_d == Y:
  num_all = num_A + num_B
  num_min = min(num_A, num_B)
  total = 1
  for i in range(num_all, num_all-num_min, -1):
    total *= i
  for i in range(num_min, 0, -1):
    total /= i
  print(int(total))
else:
  print(0)

Submission status: Wrong Answer

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

mod = 10**9+7

x_surplus = x%3
y_surplus = y%3
division = (x+y)//3

if [x_surplus,y_surplus] in [[1,1],[2,2]]:
    print(0)
    exit()

surplus = min(x_surplus,y_surplus)
r = division//2-surplus

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 )

ans = cmb(division,r,mod)

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
D - Knight
https://atcoder.jp/contests/abc145/tasks/abc145_d

"""
import sys


def modinv(a, m):
    b, u, v = m, 1, 0
    while b:
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    u %= m
    return u if u >= 0 else u + m


def solve(X, Y):
    MOD = 10**9 + 7
    if (X + Y) % 3:
        return 0
    n = (X - 2 * Y) / -3.0
    m = (Y - 2 * X) / -3.0
    if not n.is_integer() or not m.is_integer():
        return 0
    n, m = int(n), int(m)
    x1 = 1
    for i in range(2, n + m + 1):
        x1 = (x1 * i) % MOD
    x2 = 1
    for i in range(2, n+1):
        x2 = (x2 * i) % MOD
    for i in range(2, m+1):
        x2 = (x2 * i) % MOD
    ans = x1 * modinv(x2, MOD) % MOD
    return ans


def main(args):
    X, Y = map(int, input().split())
    ans = solve(X, Y)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

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

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

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


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


print(cmb(n+m, n))

Submission status: Wrong Answer

Solution code:
import math

I=list(map(int,input().split()))
X=(2*I[1]-I[0])//3
Y=(2*I[0]-I[1])//3
n=X+Y
r=X

if (X<0 or Y<0):
    print(0)
    exit()

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

p = 10 ** 9 + 7
N = 10 ** 6
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
 
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

print(cmb(n, r, p))

Submission status: Wrong Answer

Solution code:
import math

I=list(map(int,input().split()))
X=(2*I[1]-I[0])//3
Y=(2*I[0]-I[1])//3
n=X+Y
r=X

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

p = 10 ** 9 + 7
N = 10 ** 6
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
 
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

print(cmb(n, r, p))

Submission status: Wrong Answer

Solution code:
from math import factorial


x,y = map(int,input().split())
n = (x+y)/3
sa = abs(x-y)
c = (n-sa)//2


try:
    a = factorial(n) / factorial(r) / factorial(n - r)
    print(a%(10**9+7))
except:
    print(0)

Submission status: Wrong Answer

Solution code:
x,y=map(int,input().split())
MOD=10**9+7
if (2*y-x)%3!=0 or 2*y-x<0 or 2*x-y<0:
  print(0)
a=(2*x-y)//3
if (a+1)*3<=(2*x-y):
  a+=1
a=(2*x-y)//3
if (a+1)*3<=(2*x-y):
  a+=1
b=(2*y-x)//3
if (b+1)*3<=(2*y-x):
  b+=1
p=1
for i in range(1,a+b+1):
  p=(p*i)%MOD
q=1
for i in range(1,a+1):
  q=(q*i)%MOD
q=pow(q,MOD-2,MOD)
r=1
for i in range(1,b+1):
  r=(r*i)%MOD
r=pow(r,MOD-2,MOD)
print((p*q*r)%MOD)

Submission status: Wrong Answer

Solution code:
def nCr(n,r):
  dividend,divisor = 1,1
  for i in range(r):
    dividend *= n-i
    divisor *= 1+i
    dividend %= MOD
    divisor %= MOD

  return (dividend * pow(divisor, MOD-2, MOD)) % MOD

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

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

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

if n<0 or m<0:
  print(0)
  exit()

print(nCr(n+m, n))

Submission status: Wrong Answer

Solution code:
X,Y = map(int,input().split())
M = max(X,Y)
m = min(X,Y)
mod = 10 ** 9 + 7

if (X + Y) % 3 != 0:
  print(0)
  
else:
  con = (X + Y) // 3
  dif = M - m
  n = (con - dif) // 2
  
  def comb(n, r):
    n += 1
    over = 1
    under = 1
    for i in range(1,r + 1):
      over = over * (n - i) % mod
      under = under * i % mod
    #powでunder ** (mod - 2) % modを実現、逆元を求めている
    return over * pow(under,mod - 2,mod) % mod
  
  ans = comb(con,n)
  
  print(ans)

Submission status: Wrong Answer

Solution code:
def cmb(n, r, mod):
  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 )

import math
X,Y=list(map(int,input().split()))
X=min(X,Y)
Y=max(X,Y)
N=Y-X
Y=Y-2*N
X=X-N
N2=X//3
print(cmb(N2*2+N,N2,mod))

Submission status: Wrong Answer

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

if X % 3 == 0 and Y % 3 == 0 and X == Y:
    n = X // 3
    nCr = 1

    for i in range(n):
        nCr *= 2 * n - i
        nCr *= pow(i + 1, mod - 2, mod)
        nCr %= mod    
    print(nCr) 

elif (X + Y) % 3 == 0:
    if (X >= 1 and Y >= 2) or (X >= 2 and Y >= 1):
        if max(X, Y) / min(X, Y) < 2:
            night = [X, Y]
            n = (X + Y) // 3
            nCr = 1
            r = (night[int(X < Y)] - 1) // 2
            
            for i in range(r):
                nCr *= n - i
                nCr *= pow(i + 1, mod - 2, mod)
                nCr %= mod    
            print(nCr)
        
        else:
            print(0)

else:
    print(0)

Submission status: Wrong Answer

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

if X % 3 == 0 and Y % 3 == 0 and X == Y:
    n = X // 3
    nCr = 1

    for i in range(n):
        nCr *= 2 * n - i
        nCr *= pow(i + 1, mod - 2, mod)
        nCr %= mod    
    print(nCr) 

elif (X + Y) % 3 == 0:
    if (X >= 1 and Y >= 2) or (X >= 2 and Y >= 1):
        night = [X, Y]
        n = (X + Y) // 3
        nCr = 1
        r = (night[int(X < Y)] - 1) // 2
        
        for i in range(r):
            nCr *= n - i
            nCr *= pow(i + 1, mod - 2, mod)
            nCr %= mod    
        print(nCr)

else:
    print(0)

Submission status: Wrong Answer

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

if X % 3 == 0 and Y % 3 == 0 and X == Y:
    n = X // 3
    nCr = 1

    for i in range(n):
        nCr *= 2 * n - i
        nCr *= pow(i + 1, mod - 2, mod)
        nCr %= mod    
    print(nCr) 

elif (X + Y) % 3 == 0:
    if (X >= 1 and Y >= 2) or (X >= 2 and Y >= 1):
        night = [X, Y]
        n = (X + Y) // 3
        nCr = 1
        r = (night[int(X < Y)] - 1) // 2
        print(r)
        for i in range(r):
            nCr *= n - i
            nCr *= pow(i + 1, mod - 2, mod)
            nCr %= mod    
        print(nCr)
        
else:
    print(0)

Submission status: Wrong Answer

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

def comb(n,k):
    if n<k:
        return 0
    if n-k<k:
        k=n-k
    comb=1
    for x in range(n-k+1,n+1):
        comb=(comb*x)%mod
    d=1
    for x in range(1,k+1):
        d =(d*x)%mod
    comb*=pow(d,mod-2,mod)
    return comb%mod

ans=0
if (x+y)%3==0:
    n=int(x/3+y/3)
    r=int(2*x/3-y/3)
    if n-r>=0 and r>=0:
        ans=comb(n,r)%mod
print(ans)

Submission status: Wrong Answer

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

def comb(n,k):
    if n<k:
        return 0
    if n-k<k:
        k=n-k
    comb=1
    for x in range(n-k+1,n+1):
        comb=(comb*x)%mod
    d=1
    for x in range(1,k+1):
        d =(d*x)%mod
    comb*=pow(d,mod-2,mod)
    return comb%mod

if (x+y)%3==0:
    n=int(x/3+y/3)
    r=int(2*x/3-y/3)
    if n-r>=0 and r>=0:
        print(comb(n,r)%mod)
    else:
        print(0)
else:
    print(0)

Submission status: Wrong Answer

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

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

def comb_speed(n,x):
    rec=1
    for i in range(n-x+1,n+1):
        rec=rec*i%mod
    rec2=1
    for i in range(1,x+1):
        rec2=rec2*i%mod
    rec2=pow_speed(rec2,mod-2)
    return rec*rec2%mod

if (x+y)%3==0:
    n=int(x/3+y/3)
    r=int(2*x/3-y/3)
    if n-r>=0 and r>=0:
        print(comb_speed(n,r)%mod)
    else:
        print(0)
else:
    print(0)

Submission status: Wrong Answer

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

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

def comb_speed(n,x):
    rec=1
    for i in range(n-x+1,n+1):
        rec=rec*i%mod
    rec2=1
    for i in range(1,x+1):
        rec2=rec2*i%mod
    rec2=pow_speed(rec2,mod-2)
    return rec*rec2%mod

if (x+y)%3==0:
    n=int(x/3+y/3)
    r=int(2*x/3-y/3)
    print(comb_speed(n,r)%mod)
else:
    print(0)

Submission status: Wrong Answer

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

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

def comb_speed(n,x):
    rec=1
    for i in range(n-x+1,n+1):
        rec=rec*i%mod
    rec2=1
    for i in range(1,x+1):
        rec2=rec2*i%mod
    rec2=pow_speed(rec2,mod-2)
    return rec*rec2%mod

if x%3==0 and y%3==0:
    n=x//3+y//3
    r=2*x//3-y//3
    print(comb_speed(n,r)%mod)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7

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

def getComb(n, k, MOD):
    if n < k:
        return 0
    if n-k < k:
        k = n-k
    comb = 1
    for x in range(n-k+1, n+1):
        comb = (comb * x) % MOD
    d = 1
    for x in range(1, k+1):
        d = (d * x) % MOD
    comb *= pow(d, MOD-2, MOD)
    return comb % MOD

if (X+Y)%3:
    ans = 0
else:
    N = (X+Y)//3
    k = (2*Y-X)//3
    ans = getComb(N, k, MOD)

print(ans)

Submission status: Wrong Answer

Solution code:

from collections import deque
import numpy as np

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

if x >= y:
    if not (2 * y - x) % 3:  
        ch2 = int((2 * y - x) / 3)
        ch1 = ch2 + x - y
    else:
        print(0)
        exit()

elif x < y:
    if not (2 * x - y) % 3:  
        ch1 = int((2 * x - y) / 3)
        ch2 = ch1 + y - x 
    else:
        print(0)
        exit()

def cmb(s, t, r):
    x = 1
    y = 1
    for i in range(t):
        x = (x*(s-i))%r
        y = (y*(i+1))%r
 
    y_inverse = pow(y, r-2, r)
    return (x*y_inverse)%r

mod = 10 ** 9 + 7

print(cmb(ch1+ch2,ch1,mod))

Submission status: Wrong Answer

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
x,y=n1()

def comb(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

if (x+y)%3!=0:
    print(0)
else:
    n=(x+y)//3
    ans=comb(n,x-n)
    print(ans%(10**9+7))