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:
class Com:
    def __init__(self, MAX = 1000000, MOD = 1000000007):
        self.MAX = MAX
        self.MOD = MOD
        self._fac = [0]*MAX
        self._finv = [0]*MAX
        self._inv = [0]*MAX
        self._fac[0], self._fac[1] = 1, 1
        self._finv[0], self._finv[1] = 1, 1
        self._inv[1] = 1
        for i in range(2,self.MAX):
            self._fac[i] = self._fac[i - 1] * i % self.MOD
            self._inv[i] = self.MOD - self._inv[self.MOD%i] * (self.MOD // i) % self.MOD
            self._finv[i] = self._finv[i - 1] * self._inv[i] % self.MOD
    
    def com(self, n, k):
        if (n < k):
            return 0
        if (n < 0 or k < 0):
            return 0
        return self._fac[n] * (self._finv[k] * self._finv[n - k] % self.MOD) % self.MOD
X,Y = list(map(int,input().split()))
ij = min(X//3,Y//3)
if ij == 0:
  print(0)
  exit()
X = X%(3*ij)
Y = Y%(3*ij)
if X%2 == 0 and X//2 == Y:
    com = Com()
    print(com.com(2*ij+X//2, ij+X//2))
elif Y%2 == 0 and Y//2 == X:
    com = Com()
    print(com.com(2*ij+Y//2, ij+Y//2))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import math
mod=10**9+7
def mul(a, b):
    return ((a % mod) * (b % mod)) % mod
def div(a, b):
    return mul(a, pow(b, mod-2,mod))
x,y=map(int,input().split())
x,y=min(x,y),max(x,y)
s=y-x
if 3*((x-s)//3)+2*s!=y:
  print(0)
else:
  a=(x-s)//3
  b=(y-2*s)//3+s
  A=1
  B=1
  C=1
  for i in range(1,a+b+1):
    A*=i
    A%=mod
  for i in range(1,a+1):
    B*=i
    B%=mod
  for i in range(1,b+1):
    C*=i
    C%=mod
  print(div(A,mul(B,C)))

Submission status: Wrong Answer

Solution code:
import math
mod=10**9+7
def mul(a, b):
    return ((a % mod) * (b % mod)) % mod
def div(a, b):
    return mul(a, pow(b, mod-2,mod))
x,y=map(int,input().split())
x,y=min(x,y),max(x,y)
s=y-x
if (x+y)%3!=0:
  print(0)
else:
  a=x//3
  b=x//3+s
  A=1
  B=1
  C=1
  for i in range(1,a+b+1):
    A*=i
    A%=mod
  for i in range(1,a+1):
    B*=i
    B%=mod
  for i in range(1,b+1):
    C*=i
    C%=mod
  print(div(A,mul(B,C)))

Submission status: Wrong Answer

Solution code:
import math
mod=10**9+7
def mul(a, b):
    return ((a % mod) * (b % mod)) % mod
def div(a, b):
    return mul(a, pow(b, mod-2,mod))
x,y=map(int,input().split())
x,y=min(x,y),max(x,y)
s=y-x

if 3*(x//3)+2*s!=y:
  print(0)
else:
  a=x//3
  b=x//3+s
  A=1
  B=1
  C=1
  for i in range(1,a+b+1):
    A*=i
    A%=mod
  for i in range(1,a+1):
    B*=i
    B%=mod
  for i in range(1,b+1):
    C*=i
    C%=mod
  print(div(A,mul(B,C)))

Submission status: Wrong Answer

Solution code:
from scipy.special import comb

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

# 足した時の値が3の倍数でない時は除去
if not (X+Y) % 3 == 0:
    print(0)

# min / max が1/2以下の時も除去
elif min([X,Y]) / max([X, Y]) < 0.5:
    print(0)

else:
    move_count = 0
    diff = abs(int(Y - X))
    num_3_3 = int(int(X - diff) / 3)

    move_count = diff + num_3_3 * 2

    ans = comb(move_count, num_3_3+diff, exact=True)

    print(ans)

Submission status: Wrong Answer

Solution code:
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=map(int,input().split(" "))
g=0
if x%2==0:
  for i in range(int(x/2)):
    a=2*i
    xh=x-a 
    yh=y-2*a
    if xh<0 or yh<0:
      break
    elif xh/2==yh:
      b=yh
      if b<0:
        break
      else:
        g=g+cmb(a+b,a)
        break
    
else:
  for i in range(int(x/2)):
    a=2*i+1
    xh=x-a
    yh=y-2*a
    if xh<0 or yh<0:
      break
    elif xh/2==yh:
      b=yh
      if b<0:
        break
      else:
        g=g+cmb(a+b,a)
        break
      
print(g%(10**9+7))

Submission status: Wrong Answer

Solution code:
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=map(int,input().split(" "))
g=0
if x%2==0:
  for i in range(int(x/2)):
    a=2*i
    xh=x-a 
    yh=y-2*a
    if xh<0 or yh<0:
      break
    elif xh/2==yh:
      b=yh
      g=g+cmb(a+b,a)
      break
    
else:
  for i in range(int(x/2)):
    a=2*i+1
    xh=x-a
    yh=y-2*a
    if xh<0 or yh<0:
      break
    elif xh/2==yh:
      b=yh
      g=g+cmb(a+b,a)
      break
      
print(g%(10**9+7))

Submission status: Wrong Answer

Solution code:
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=map(int,input().split(" "))
g=0
if x%2==0:
  for i in range(int(x/2)):
    a=2*i
    xh=x-a 
    yh=y-2*a
    if xh/2==yh:
      b=yh
      g=g+cmb(a+b,a)
      break
    if xh<0 or yh<0:
      break
else:
  for i in range(int(x/2)):
    a=2*i+1
    xh=x-a
    yh=y-2*a
    if xh/2==yh:
      b=yh
      g=g+cmb(a+b,a)
      break
    if xh<0 or yh<0:
      break
print(g%(10**9+7))

Submission status: Wrong Answer

Solution code:
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=map(int,input().split(" "))
g=0
if (x+y)%3==0:
  if x%2==0:
    for i in range(int(x/2)):
      a=2*i
      xh=x-a
      yh=y-2*a
      if xh/2==yh:
        b=yh
        g=g+cmb(a+b,a)
        break
      if xh<0 or yh<0:
        break
  else:
    for i in range(int(x/2)):
      a=2*i+1
      xh=x-a
      yh=y-2*a
      if xh/2==yh:
        b=yh
        g=g+cmb(a+b,a)
        break
      if xh<0 or yh<0:
        break

print(g%(10**9+7))

Submission status: Wrong Answer

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

ans = 0
x = 0
y = 0
if (X + Y) % 3 == 0:
  x = (2*Y-X) // 3
  y = (2*X-Y) // 3
      
def cmb(n, r, mod):
  if (r<0) or (n<r):
    return 0
  r = min(r, n-r)
  return (fac[n] * finv[r] * finv[n-r]) % mod

mod = 10**9+7
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]
for i in range(2, x+y+1):
  fac.append(fac[-1] * i % mod)
  inv.append(-inv[mod % i] * (mod // i) % mod)
  finv.append(finv[-1] * inv[-1] % mod)

print(cmb(x+y, x, mod))

Submission status: Wrong Answer

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

m, r = divmod(2*Y-X, 3)
if r != 0:
    print(0)
    exit()

n = Y - 2*m

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

print(combination(n+m, n))

Submission status: Wrong Answer

Solution code:
x,y = map(int,input().split())
mod = 10**9+7
def comb(a,b):
  comb = 1
  chld = 1
  for i in range(b):
    comb = comb*(a-i)%mod
    chld = chld*(b-i)%mod
  return (comb*pow(chld,mod-2,mod))%mod
if (x+y)%3!=0:
  print(0)
else:
  nm = (x+y)//3
  n = y-nm
  m = x-nm
  if n>0 and m>0:
    print(comb(n+m,m))
  else:
    print(0)

Submission status: Wrong Answer

Solution code:
x,y = map(int,input().split())
mod = 10**9+7
def comb(a,b):
  comb = 1
  chld = 1
  for i in range(b):
    comb = comb*(a-i)%mod
    chld = chld*(b-i)%mod
  return (comb*pow(chld,mod-2,mod))%mod
if (x+y)%3!=0:
  print(0)
else:
  nm = (x+y)//3
  n = y-nm
  m = x-nm
  print(comb(n+m,m))

Submission status: Wrong Answer

Solution code:
x,y = map(int,input().split())
mod = 10**9+7
def comb(a,b):
  comb = 1
  chld = 1
  for i in range(b):
    comb = comb*(a-i)%mod
    chld *= (b-i)%mod
  return (comb*pow(chld,mod-2,mod))%mod
if (x+y)%3!=0:
  print(0)
else:
  nm = (x+y)//3
  n = y-nm
  m = x-nm
  print(comb(n+m,m))

Submission status: Wrong Answer

Solution code:
x, y = list(map(int, input().split(' ')))
# K馬(右上のみ) -> パスカルの三角形っぽい
mmm = 1000000000 + 7
sss = x + y
if sss % 3 != 0:
    print(0)
    exit()
hoge = sss // 3
# 上にp, 右にq
# 2p + q = y
# 2q + p = x
# p + q = hoge
# p = y - hoge
# q = x - hoge
p =  y - hoge
q = x - hoge
# print(hoge, p, q)
# 二項係数 mod [検索]
fac = []
inv = []
inv_fac = []
def init(n):
    fac.append(1)
    fac.append(1)
    inv.append(0)
    inv.append(1)
    inv_fac.append(1)
    inv_fac.append(1)
    for i in range(2, n):
        fac.append(fac[-1] * i % mmm)
        inv.append(mmm - inv[mmm%i] * (mmm // i) % mmm)
        inv_fac.append(inv_fac[-1] * inv[-1] % mmm)

def choice(a, b):
    if a < b:
        return 0
    return fac[a] * (inv_fac[b] * inv_fac[a-b] % mmm) % mmm

init(hoge * 3 + 1)
print(choice(hoge, p) % mmm)

# nCk - TLE でした
# def choice(a, b):
#     if a == b or b == 0:
#         return 0
#     if a < b:
#         return choice(b, a)
#     bunshi = a
#     t = min(b, (a-b))
#     bunbo = t
#     for i in range(1, t):
#         bunshi *= (a-i) % mmm
#         bunbo *= (t-i) % mmm
#     return bunshi // bunbo

Submission status: Wrong Answer

Solution code:
x, y = list(map(int, input().split(' ')))
# K馬(右上のみ) -> パスカルの三角形っぽい
mmm = 1000000000 + 7
sss = x + y
if sss % 3 != 0:
    print(0)
    exit()
hoge = sss // 3
# 上にp, 右にq
# 2p + q = y
# 2q + p = x
# p + q = hoge
# p = y - hoge
# q = x - hoge
p =  y - hoge
q = x - hoge
# print(hoge, p, q)
# 二項係数 mod [検索]
fac = []
inv = []
inv_fac = []
def init(n):
    fac.append(1)
    fac.append(1)
    inv.append(0)
    inv.append(1)
    inv_fac.append(1)
    inv_fac.append(1)
    for i in range(2, n):
        fac.append(fac[-1] * i % mmm)
        inv.append(mmm - inv[mmm%i] * (mmm // i) % mmm)
        inv_fac.append(inv_fac[-1] * inv[-1] % mmm)

def choice(a, b):
    if a < b:
        return 0
    return fac[a] * (inv_fac[b] * inv_fac[a-b] % mmm) % mmm

init(hoge * 2 + 1)
print(choice(hoge, p) % mmm)

# nCk - TLE でした
# def choice(a, b):
#     if a == b or b == 0:
#         return 0
#     if a < b:
#         return choice(b, a)
#     bunshi = a
#     t = min(b, (a-b))
#     bunbo = t
#     for i in range(1, t):
#         bunshi *= (a-i) % mmm
#         bunbo *= (t-i) % mmm
#     return bunshi // bunbo

Submission status: Wrong Answer

Solution code:
x, y = list(map(int, input().split(' ')))
# K馬(右上のみ) -> パスカルの三角形っぽい
mmm = 1000000000 + 7
sss = x + y
if sss % 3 != 0:
    print(0)
    exit()
hoge = sss // 3
# 上にp, 右にq
# 2p + q = y
# 2q + p = x
# p + q = hoge
# p = y - hoge
# q = x - hoge
p =  y - hoge
q = x - hoge
#print(hoge, p, q)
# 二項係数 mod [検索]
fac = []
inv = []
inv_fac = []
def init(n):
    fac.append(1)
    fac.append(1)
    inv.append(0)
    inv.append(1)
    inv_fac.append(1)
    inv_fac.append(1)
    for i in range(2, n):
        fac.append(fac[-1] * i % mmm)
        inv.append(mmm - inv[mmm%i] * (mmm // i) % mmm)
        inv_fac.append(inv_fac[-1] * inv[-1] % mmm)

def choice(a, b):
    if a <= b:
        return 0
    return fac[a] * (inv_fac[b] * inv_fac[a-b] % mmm) % mmm

init(hoge * 2 + 1)
print(choice(hoge, p) % mmm)

# nCk - TLE でした
# def choice(a, b):
#     if a == b or b == 0:
#         return 0
#     if a < b:
#         return choice(b, a)
#     bunshi = a
#     t = min(b, (a-b))
#     bunbo = t
#     for i in range(1, t):
#         bunshi *= (a-i) % mmm
#         bunbo *= (t-i) % mmm
#     return bunshi // bunbo

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import lru_cache
import bisect
import re
import queue
import decimal


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

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

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

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

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

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


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

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

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

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

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

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


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


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


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

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


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


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


def main():
    # sys.setrecursionlimit(1000000)
    # sys.stdin = open("sample.txt")
    # T = Scanner.int()
    # for _ in range(T):
    #     solve()
    # print('YNeos'[not solve()::2])
    solve()


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import lru_cache
import bisect
import re
import queue
import decimal


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

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

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

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

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

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


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

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

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

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

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

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


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


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


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

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


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


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

    n = factorial(A + B)
    m = factorial(A)
    l = factorial(B)
    ans = n * modinv(m * l) % MOD
    print(ans)


def main():
    # sys.setrecursionlimit(1000000)
    # sys.stdin = open("sample.txt")
    # T = Scanner.int()
    # for _ in range(T):
    #     solve()
    # print('YNeos'[not solve()::2])
    solve()


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

i = (2 * y - x) // 3
j = (2 * x - y) // 3
print(nck(i+j, min(i, j)))

Submission status: Wrong Answer

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

A = (2*Y - X)/3
B = (2*X - Y)/3
if (2*Y - X) % 3 != 0 or (2*X - Y) % 3 != 0:
    ans = 0
    print(ans)
else:
    A = int(A)
    B = int(B)
    p = 10**9 + 7
    def comb(n, r, p):
        num1, num2 = 1, 1
        for i in range(r):
            num1 *= n-i
            num2 *= i+1
            num1 %= p
            num2 %= p
        return int(num1 * pow(num2, p-2, p))
    ans = comb(A+B, A, p)
    print(ans % p)

Submission status: Wrong Answer

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

A = (2*Y - X)/3
B = (2*X - Y)/3
if X % 3 != 0 or Y % 3 != 0:
    ans = 0
    print(ans)
else:
    A = int(A)
    B = int(B)
    p = 10**9 + 7
    def comb(n, r, p):
        num1, num2 = 1, 1
        for i in range(r):
            num1 *= n-i
            num2 *= i+1
            num1 %= p
            num2 %= p
        return int(num1 * pow(num2, p-2, p))
    ans = comb(A+B, A, p)
    print(ans % p)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc145/tasks/abc145_d


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

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

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

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

n = x + y
r = x


mod = 10**9 + 7
f = 1
for i in range(1, n + 1):
    f = f*i % mod
fac = f
f = pow(f, mod-2, mod)
facinv = [f]
for i in range(n, 0, -1):
    f = f*i % mod
    facinv.append(f)
facinv.append(1)

print(fac * facinv[r] * facinv[n - r] % mod)

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def combinations_mod(n, r, mod=1000000007):
    """Returns nCr in mod."""
    r = min(r, n - r)
    combs = 1
    for i, j in zip(range(n - r + 1, n + 1), range(1, r + 1)):
        combs *= (i % mod) * pow(j, mod - 2, mod)
        combs %= mod
    return combs


def main():
    X, Y = NMI()
    if (2*Y - X)%3 or (2*X - Y)%3:
        print(0)
        exit()

    A = (2*Y - X) // 3
    B = (2*X - Y) // 3
    print(combinations_mod(A+B, A))


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)
  
  # 0,0始点で，片方の操作のみを連続で行う場合
  if Y == X * 2:
    print(1)
    exit(0)
  elif X == Y * 2:
    print(1)
    exit(0)
  
  # 始点となりえる点を探索
  stack = deque([(0, 0)])
  ans = 1
  while len(stack) > 0:
    start_x, start_y = stack.pop()
    if Y - start_y == (X - start_x) * 2:
      print(ans % (power(10, 9) + 7))
      exit(0)
    elif X - start_x == (Y - start_y) * 2:
      print(ans % (power(10, 9) + 7))
      exit(0)

    # 次の始点をスタック
    next_start_x = start_x + 3
    next_start_y = start_y + 3
    if next_start_x <= X and next_start_y <= Y:
      stack.append((next_start_x, next_start_y))
      ans += 1
  
  # a,b始点で，片方の操作のみを連続で行う場合
  

  # 枠外は移動済みとする
  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)
  
  # 0,0始点で，片方の操作のみを連続で行う場合
  if Y == X * 2:
    print(1)
    exit(0)
  elif X == Y * 2:
    print(1)
    exit(0)
  
  # 始点となりえる点を探索
  stack = deque([(0, 0)])
  ans = 1
  while len(stack) > 0:
    start_x, start_y = stack.pop()
    if Y - start_y == (X - start_x) * 2:
      print(ans)
      exit(0)
    elif X - start_x == (Y - start_y) * 2:
      print(ans)
      exit(0)

    # 次の始点をスタック
    next_start_x = start_x + 3
    next_start_y = start_y + 3
    if next_start_x <= X and next_start_y <= Y:
      stack.append((next_start_x, next_start_y))
      ans += 1
  
  # a,b始点で，片方の操作のみを連続で行う場合
  

  # 枠外は移動済みとする
  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()