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 defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


class PrimeComb:
  def __init__(self, MAX=666670, MOD=1000000007):
    # preprocessing: O(MAX)
    self.MAX  = MAX
    self.MOD  = MOD
    self.fac  = [0]*MAX
    self.inv  = [0]*MAX
    self.finv = [0]*MAX
    self.fac[0]  = self.fac[1] = 1
    self.finv[0] = self.finv[1] = 1
    self.inv[1]  = 1
    for i in range(2, MAX):
      self.fac[i]  = self.fac[i-1] * i % MOD
      self.inv[i]  = MOD - self.inv[MOD % i] * int(MOD / i) % MOD
      self.finv[i] = self.finv[i-1] * self.inv[i] % MOD

  def com(self, n, k):
    # compute -> nCk % self.P : O(1)
    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


def main():
  X, Y = list(map(int, input().split()))
  PC = PrimeComb()

  num_trans1 = 0
  num_trans2 = 0

  # min_ = min(X, Y) // 3
  # X -= 3*min_
  # Y -= 3*min_
  # num_trans1 += min_
  # num_trans2 += min_
  for i in range(min(X, Y)//3 + 1):
    tempX = X - 3*i
    tempY = Y - 3*i
    num_trans1 = i
    num_trans2 = i

    if tempX==tempY==0:
      break

    elif X > Y:
      if X % 2:
        # print(0)
        continue
      if X//2 != Y:
        # print(0)
        continue
      num_trans2 += Y
      break
    elif X < Y:
      if Y % 2:
        # print(0)
        continue
      if Y//2 != X:
        # print(0)
        continue
      num_trans1 += X
      break
  else:
    print(0)
    return

  # print(num_trans1, num_trans2)
  print(PC.com(num_trans1+num_trans2, num_trans1))


if(__name__ == '__main__'):
  main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


class PrimeComb:
  def __init__(self, MAX=666670, MOD=1000000007):
    # preprocessing: O(MAX)
    self.MAX  = MAX
    self.MOD  = MOD
    self.fac  = [0]*MAX
    self.inv  = [0]*MAX
    self.finv = [0]*MAX
    self.fac[0]  = self.fac[1] = 1
    self.finv[0] = self.finv[1] = 1
    self.inv[1]  = 1
    for i in range(2, MAX):
      self.fac[i]  = self.fac[i-1] * i % MOD
      self.inv[i]  = MOD - self.inv[MOD % i] * int(MOD / i) % MOD
      self.finv[i] = self.finv[i-1] * self.inv[i] % MOD

  def com(self, n, k):
    # compute -> nCk % self.P : O(1)
    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


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

  num_trans1 = 0
  num_trans2 = 0

  min_ = min(X, Y) // 3
  X -= 3*min_
  Y -= 3*min_
  num_trans1 += min_
  num_trans2 += min_

  if X==Y:
    if X % 3:
      print(0)
      return
  elif X > Y:
    if X % 2:
      print(0)
      return
    if X//2 != Y:
      print(0)
      return
    num_trans2 += Y
  elif X < Y:
    if Y % 2:
      print(0)
      return
    if Y//2 != X:
      print(0)
      return
    num_trans1 += X

  PC = PrimeComb()
  print(PC.com(num_trans1+num_trans2, num_trans1))


if(__name__ == '__main__'):
  main()

Submission status: Wrong Answer

Solution code:

def check_use_count(X, Y):
    a = (2 * X - Y) // 3
    b = X - 2 * a

    return a, b


def factorial(x, mod):
    x %= mod

    for i in range(x-1, 0, -1):
        x *= i
        x %= mod

    return x


def main():
    mod = 10**9 + 7
    X, Y = [int(x) for x in input().split()]

    # 必ず3で割り切れる
    if (X + Y) % 3 != 0:
        print(0)
        exit()

    a, b = check_use_count(X, Y)

    # 0だったら行けない
    if a < 0 or b < 0:
        print(0)
        exit()

    # 移動パターンの計算
    # !(a + b) / !a * !b
    up = factorial(a + b, mod)
    fact_a = factorial(a, mod)
    fact_b = factorial(b, mod)

    down = fact_a * fact_b % mod

    result = up * pow(down, mod - 2, mod) % mod
    print(result)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
from itertools import combinations

X,Y=(int(x) for x in input().rstrip('\n').split())
if (X+Y) % 3 != 0:
  print(0)
else:
  t = (X+Y)//3
  if X<t or Y<t:
    print(0)
  else:
    s = abs(X-Y)
    a = (t-s) // 2
    res = t
    for i in range(1,a):
      res = res*(t-i)
      ad = pow(i+1,1000000005,1000000007)%1000000007
      res = res*ad
      res = res%1000000007
    print(res%1000000007)

Submission status: Wrong Answer

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque

def inverse(a, p):
    """逆元"""
    a_, p_ = a, p
    x, y = 1, 0
    while p_:
        t = a_ // p_
        a_ -= t * p_
        a_, p_ = p_, a_
        x -= t * y
        x, y = y, x
    x %= p
    return x


def dev_mod(a, b, mod):
    """a(= A % mod) / b を mod で割った余り"""
    return (a * inverse(b, mod)) % mod


# from decorator import stop_watch
#
#
# @stop_watch
def solve(X, Y):
    mod = 10 ** 9 + 7
    if (2 * Y - X) % 3 != 0:
        print(0)
        return
    a = round((2 * Y - X) / 3)
    b = Y - 2 * a
    a, b = max(a, b), min(a, b)

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

    print(ans)


if __name__ == '__main__':
    X, Y = map(int, input().split())
    solve(X, Y)

Submission status: Wrong Answer

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque

def inverse(a, p):
    """逆元"""
    a_, p_ = a, p
    x, y = 1, 0
    while p_:
        t = a_ // p_
        a_ -= t * p_
        a_, p_ = p_, a_
        x -= t * y
        x, y = y, x
    x %= p
    return x


def dev_mod(a, b, mod):
    """a(= A % mod) / b を mod で割った余り"""
    return (a * inverse(b, mod)) % mod


# from decorator import stop_watch
# 
# 
# @stop_watch
def solve(X, Y):
    mod = 10 ** 9 + 7
    if (2 * Y - X) % 3 != 0:
        print(0)
        return
    a = (2 * Y - X) // 3
    b = Y - 2 * a
    a, b = max(a, b), min(a, b)

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

    print(ans)


if __name__ == '__main__':
    X, Y = map(int, input().split())
    solve(X, Y)

Submission status: Wrong Answer

Solution code:
mod = 10**9+7
def comb(n, k):
    c = 1
    for i in range(k):
        c *= n - i
        c %= mod
    d = 1
    for i in range(1, k + 1):
        d *= i
        d %= mod
    return (c * pow(d, mod - 2, mod)) % mod
x,y=map(int,input().split())

ans=0

z=x//2+1

for q in range(z,x+1):
  w=2*q-x
  if w*2+(q-w)==y:
    t=comb(q,w)
    ans+=t
    
  
  
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

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

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

n = (x + y) // 3
print(comb((x + y - 2 * n), x - n))

Submission status: Wrong Answer

Solution code:
mod = 10**9+7
MAX_N = 10 ** 6
fact = [1]
fact_inv = [0] * (MAX_N + 4)
for i in range(MAX_N + 3):
  fact.append(fact[-1] * (i + 1) % mod)
fact_inv[-1] = pow(fact[-1], mod - 2, mod)
for i in range(MAX_N + 2, -1, -1):
  fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod
def mod_comb_k(n, k, mod):
  return fact[n] * fact_inv[k] % mod * fact_inv[n - k] % mod
x,y=map(int,input().split())
if (2*x-y)>0 and (2*y-x)>0 and (x+y)%3==0:
  print(mod_comb_k((x+y)//3,(2*y-x)//3,mod))
else:print(0)

Submission status: Wrong Answer

Solution code:
mod = 10**9+7
MAX_N = 10 ** 6
fact = [1]
fact_inv = [0] * (MAX_N + 4)
for i in range(MAX_N + 3):
  fact.append(fact[-1] * (i + 1) % mod)
fact_inv[-1] = pow(fact[-1], mod - 2, mod)
for i in range(MAX_N + 2, -1, -1):
  fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod
def mod_comb_k(n, k, mod):
  return fact[n] * fact_inv[k] % mod * fact_inv[n - k] % mod
x,y=map(int,input().split())
if (x+y)%3==0:
  print(mod_comb_k((x+y)//3,(2*y-x)//3,mod))
else:print(0)

Submission status: Wrong Answer

Solution code:
mod = 10**9+7
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]


def init(n):
    for i in range(2, n + 1):
        fac.append(fac[-1] * i % mod)
        inv.append(-inv[mod % i] * (mod // i) % mod)
        finv.append(finv[-1] * inv[-1] % mod)


def com(n, k, mod):
    if n < 0 or k < 0 or n < k:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod


X, Y = map(int, input().split())
t_o = min([X//3, Y//3])
o_t = t_o
if X - 3*t_o == 0 and Y - 3*t_o == 0:
    dum = "3"
elif X - 3*t_o == 0 or Y - 3*t_o == 0:
    print(0)
    exit()
else:
    l = max([X - 3*t_o, Y - 3*t_o])
    s = min([X - 3*t_o, Y - 3*t_o])
    if X - 3*t_o > Y - 3*t_o:
        t_o += l//s
    else:
        o_t += l//s

# ここまではよろしい。階乗と逆元を使用して求めるらしいが・・・・
a = t_o+o_t
b = o_t
p = 10**9 + 7
init(a)
print(com(a, b, p))

Submission status: Wrong Answer

Solution code:
mod = 10**9+7
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]


def init(n):
    for i in range(2, n + 1):
        fac.append(fac[-1] * i % mod)
        inv.append(-inv[mod % i] * (mod // i) % mod)
        finv.append(finv[-1] * inv[-1] % mod)


def com(n, k, mod):
    if n < 0 or k < 0 or n < k:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod


X, Y = map(int, input().split())
t_o = min([X//3, Y//3])
o_t = t_o
if X - 3*t_o == 0 and Y - 3*t_o == 0:
    dum = "3"
elif X - 3*t_o == 0 or Y - 3*t_o == 0:
    print(0)
    exit()
else:
    l = max([X - 3*t_o, Y - 3*t_o])
    s = min([X - 3*t_o, Y - 3*t_o])
    if not l//s == 2:
        print(0)
        exit()
    else:
        if X - 3*t_o > Y - 3*t_o:
            t_o += l//s
        else:
            o_t += l//s

# ここまではよろしい。階乗と逆元を使用して求めるらしいが・・・・
a = t_o+o_t
b = o_t
p = 10**9 + 7
init(a)
print(com(a, b, p))

Submission status: Wrong Answer

Solution code:
mod = 10**9+7
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]


def init(n):
    for i in range(2, n + 1):
        fac.append(fac[-1] * i % mod)
        inv.append(-inv[mod % i] * (mod // i) % mod)
        finv.append(finv[-1] * inv[-1] % mod)


def com(n, k, mod):
    if n < 0 or k < 0 or n < k:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod


X, Y = map(int, input().split())
t_o = min([X//3, Y//3])
o_t = t_o
if X - 3*t_o == 0 and Y - 3*t_o == 0:
    dum = "3"
elif not (X - 3*t_o == 0 and Y - 3*t_o == 0):
    print(0)
    exit()
else:
    l = max([X - 3*t_o, Y - 3*t_o])
    s = min([X - 3*t_o, Y - 3*t_o])
    if not l//s == 2:
        print(0)
        exit()
    else:
        if X - 3*t_o > Y - 3*t_o:
            t_o += l//s
        else:
            o_t += l//s

# ここまではよろしい。階乗と逆元を使用して求めるらしいが・・・・
a = t_o+o_t
b = o_t
p = 10**9 + 7
init(a)
print(com(a, b, p))

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
a,b = list(map(int,input().split()))
if 2*a-b > 0 and (2*a-b)%3 == 0 and a-2*((2*a-b)//3) > 0:
    x = (2*a-b)//3
    y = a-2*x
    com = Com()
    print(com.com(x+y, x))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
x, y = map(int, input().split())
tmp = 2*y - x
if tmp%3 != 0:
    print(0)
    exit()
up = tmp // 3
total = y - up*2 + up

n = total
k = up
mod = 10**9 + 7

#print(n)
#print(k)
#print()

if k <= 0:
    print(1)
    exit()
#try:
modinv_table = [-1] * (k+1)
modinv_table[1] = 1
for i in range(2, k+1):
    modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod
#except:
    #k = total - up
    #modinv_table = [-1] * (k + 1)
    #modinv_table[1] = 1
    #for i in range(2, k + 1):
        #modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod

def binomial_coefficients(n, k):
    ans = 1
    for i in range(k):
        ans *= n-i
        ans *= modinv_table[i + 1]
        ans %= mod
    return ans

print(binomial_coefficients(n, k))

Submission status: Wrong Answer

Solution code:
x, y = map(int, input().split())
tmp = 2*y - x
if tmp%3 != 0:
    print(0)
    exit()
up = tmp // 3
total = y - up*2 + up

n = total
k = up
mod = 10**9 + 7

print(n)
print(k)
print()

if k <= 0:
    print(1)
    exit()
#try:
modinv_table = [-1] * (k+1)
modinv_table[1] = 1
for i in range(2, k+1):
    modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod
#except:
    #k = total - up
    #modinv_table = [-1] * (k + 1)
    #modinv_table[1] = 1
    #for i in range(2, k + 1):
        #modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod

def binomial_coefficients(n, k):
    ans = 1
    for i in range(k):
        ans *= n-i
        ans *= modinv_table[i + 1]
        ans %= mod
    return ans

print(binomial_coefficients(n, k))

Submission status: Wrong Answer

Solution code:
x, y = map(int, input().split())
tmp = 2*y - x
if tmp%3 != 0:
    print(0)
    exit()
up = tmp // 3
total = y - up*2 + up

n = total
k = up
mod = 10**9 + 7

#print(n)
#print(k)

if k == 0:
    print(1)
    exit()

try:
    modinv_table = [-1] * (k+1)
    modinv_table[1] = 1
    for i in range(2, k+1):
        modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod
except:
    print(1)
    exit()

def binomial_coefficients(n, k):
    ans = 1
    for i in range(k):
        ans *= n-i
        ans *= modinv_table[i + 1]
        ans %= mod
    return ans

print(binomial_coefficients(n, k))

Submission status: Wrong Answer

Solution code:
x, y = map(int, input().split())
tmp = 2*y - x
if tmp%3 != 0:
    print(0)
    exit()
up = tmp // 3
total = y - up*2 + up

n = total
k = up
mod = 10**9 + 7

try:
    modinv_table = [-1] * (k+1)
    modinv_table[1] = 1
    for i in range(2, k+1):
        modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod
except:
    print(0)
    
def binomial_coefficients(n, k):
    ans = 1
    for i in range(k):
        ans *= n-i
        ans *= modinv_table[i + 1]
        ans %= mod
    return ans

print(binomial_coefficients(n, k))

Submission status: Wrong Answer

Solution code:
import itertools

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

if (x+y) % 3 != 0:
  print(0)
else:
  roots = 0
  
  for seq in itertools.permutations(['u', 'l'], int((x+y)/3)):
    _x = 0
    _y = 0
    
    for v in seq:
      if v == 'u':
        _x += 1
        _y += 2
      else:
        _x += 2
        _y += 1

    if (_x == x) & (_y == y):
      roots += 1

  print(roots % (pow(10, 9)+7))

Submission status: Wrong Answer

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


S = -X + 2 * Y
T = 2 * X - Y

if S < 0 or T < 0:
    print(0)
    exit()
if S % 3 != 0 or T % 3 != 0:
    print(0)
    exit()

S //= 3
T //= 3


def cmb(n, r, p):
    r = min(n - r, r)
    if r == 0:
        return 1
    over = 1
    for i in range(n, n - r, -1):
        over = over * i % p
    under = 1
    for i in range(1, r + 1):
        under = under * i % p
    inv = pow(under, p - 2, p)
    return over * inv % p


print(S, T)
ans = cmb(S + T, S, MOD) % MOD
print(ans % MOD)

Submission status: Wrong Answer

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


S = -X + 2 * Y
T = 2 * X - Y

if S % 3 != 0 or T % 3 != 0:
    print(0)
    exit()

S //= 3
T //= 3


def cmb(n, r, p):
    r = min(n - r, r)
    if r == 0:
        return 1
    over = 1
    for i in range(n, n - r, -1):
        over = over * i % p
    under = 1
    for i in range(1, r + 1):
        under = under * i % p
    inv = pow(under, p - 2, p)
    return over * inv % p


ans = cmb(S + T, S, MOD) % MOD
print(ans % MOD)

Submission status: Wrong Answer

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


S = -X + 2 * Y
T = 2 * X - Y

if S % 3 != 0 or T % 3 != 0:
    print(0)
    exit()

S //= 3
T //= 3


def cmb(n, r, p):
    r = min(n - r, r)
    if r == 0:
        return 1
    over = 1
    for i in range(n, n - r, -1):
        over = over * i % p
    under = 1
    for i in range(1, r + 1):
        under = under * i % p
    inv = pow(under, p - 2, p)
    return over * inv % p


ans = cmb(S + T, S, MOD) % MOD
print(ans)

Submission status: Wrong Answer

Solution code:
x,y = map(int,input().split())
mod = 10**9+7
if 2*x-y%3!=0 and 2*y-x!=0:
  print(0)
  exit()
n = (2*x-y)//3
m = (2*y-x)//3
#n+mCm
if n==0 or m==0:
  print(1)
  exit()
def ka(x):
  a = 1
  while x!=1:
    a *= x
    x -= 1
    a%= mod
  return a

ans = (ka(n+m)*pow(ka(m),mod-2,mod)*pow(ka(n),mod-2,mod))%mod
print(ans)

Submission status: Wrong Answer

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

if x%3!=0 or y%3!=0:
  print(0)
  exit()
n = (2*x-y)//3
m = (2*y-x)//3
#n+mCm
if n==0 or m==0:
  print(1)
  exit()
def ka(x):
  a = 1
  while x!=1:
    a *= x
    x -= 1
    a%= mod
  return a

ans = (ka(n+m)*pow(ka(m),mod-2,mod)*pow(ka(n),mod-2,mod))%mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
MOD = 10**9 + 7

def mod_inv(n, m):
    return pow(n, m-2, m)

def nCr_mod_m(n, r, m):
    if r < 0 or n < r:
        return 0
    # n!
    fact = fact_prev = 1 # 0! = 1! = 1
    for i in range(2, n+1):
        fact = fact_prev * i % m
        fact_prev = fact
    # n!^(-1)
    fact_inv = [0]*(max(r, n-r)+1)
    fact_inv[0] = fact_inv[1] = 1  # 1 = 1 mod m
    for i in range(2, max(r, n-r)+1):
        fact_inv[i] = fact_inv[i-1] * mod_inv(i, m) % m
    return (fact * fact_inv[r] % m) * fact_inv[n-r] % m

def main():
    X, Y = map(int, input().split())
    if ((X+Y)%3 != 0) or (min(X,Y)*2 < max(X,Y)):
        print(0)
        sys.exit()

    base = (X + Y) // 3
    x = X - base
    y = Y - base
    print(x, y)

    print(nCr_mod_m(x+y, x, MOD))
main()

Submission status: Wrong Answer

Solution code:
import sys
from math import factorial
input = sys.stdin.readline


def log(*args):
    print(*args, file=sys.stderr)


def main():
    x, y = map(int, input().rstrip().split())
    # 全部 i + 1, j + 2が最初だったとする
    # a + 2b = x, 2a + b = y
    # =>
    # a = x - 2b
    # b = y - 2a
    # =>
    # b = y - 2x + 4b
    # 3b = 2x - y
    # b = (2x - y) / 3
    # a = (2y - x) / 3
    if (2 * x - y) % 3 != 0 or (2 * y - x) % 3 != 0:
        print(0)
        return
    a = (2 * x - y) // 3
    b = (2 * y - x) // 3
    # (a + b) C min(a, b)
    # => (a + b)! / (min(a, b) ! * (a + b - min(a, b)) !
    mod = 10**9 + 7
    ans = (factorial_mod(a + b, mod) * pow(factorial_mod(min(a, b), mod), mod - 2, mod)
           * pow(factorial_mod(a + b - min(a, b), mod), mod - 2, mod)) % mod
    print(ans)


def factorial_mod(a, mod):
    ans = 1
    for v in range(1, a + 1):
        ans *= v
        ans %= mod
    return ans


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from math import factorial
input = sys.stdin.readline


def log(*args):
    print(*args, file=sys.stderr)


def main():
    x, y = map(int, input().rstrip().split())
    # 全部 i + 1, j + 2が最初だったとする
    # a + 2b = x, 2a + b = y
    # =>
    # a = x - 2b
    # b = y - 2a
    # =>
    # b = y - 2x + 4b
    # 3b = 2x - y
    # b = (2x - y) / 3
    # a = (2y - x) / 3
    if (2 * x - y) % 3 != 0 or (2 * y - x) % 3 != 0:
        print(0)
        return
    a = (2 * x - y) // 3
    b = (2 * y - x) // 3
    # (a + b) C min(a, b)
    # => (a + b)! / (min(a, b) ! * (a + b - min(a, b)) !
    mod = 10**9 + 7
    ans = ((factorial(a + b) % mod) * pow(min(a, b), mod - 2, mod)
           * pow(a + b - min(a, b), mod - 2, mod)) % mod
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#n回,m回　　X =n + 2*m  Y = 2*n + m
X,Y = map(int,input().split())
import numpy as np
from functools import reduce
import math 
a = [[1, 2], [2, 1]]
#次に逆行列を求めます。
mod = pow(10,9)+7

b = np.linalg.inv(a)


Z = np.array([[X],[Y]])

n,m=np.dot(b,Z)
p,q = *n,*m

def comb(n,k,p):
    if k==0:
        return 1
    A = reduce(lambda x,y:x*y%p ,[n-k+1+i for i in range(k)])
    B = reduce(lambda x,y:x*y%p ,[i+1 for i in range(k)])
    return A*pow(B,p-2,p)%p


if p<0 or q<0:
    print(0)
elif not (p-round(p))<pow(10,-3) or not (q-round(q))<pow(10,-3):
    print(0)
elif p==0 and q==0:
    print(0)
elif p==0:
    print(1)
elif q==0:
    print(1)
else:
    n = int(round(p))
    m = int(round(q))
    ans = comb(n+m,m,mod)

    print(ans%mod)

Submission status: Wrong Answer

Solution code:
#n回,m回　　X =n + 2*m  Y = 2*n + m
X,Y = map(int,input().split())
import numpy as np
from functools import reduce
import math 
a = [[1, 2], [2, 1]]
#次に逆行列を求めます。
mod = pow(10,9)+7

b = np.linalg.inv(a)


Z = np.array([[X],[Y]])

n,m=np.dot(b,Z)
p,q = *n,*m

def comb(n,k,p):
    if k==0:
        return 1
    A = reduce(lambda x,y:x*y%p ,[n-k+1+i for i in range(k)])
    B = reduce(lambda x,y:x*y%p ,[i+1 for i in range(k)])
    return A*pow(B,p-2,p)%p


if p<0 or q<0:
    print(0)
elif not (p-round(p))<pow(10,-5) or not (q-round(q))<pow(10,-5):
    print(0)
elif p==0 and q==0:
    print(0)
elif p==0:
    print(1)
elif q==0:
    print(1)
else:
    n = int(p)
    m = int(q)
    ans = comb(n+m,m,mod)

    print(ans%mod)

Submission status: Wrong Answer

Solution code:
#n回,m回　　X =n + 2*m  Y = 2*n + m
X,Y = map(int,input().split())
import numpy as np
from functools import reduce
a = [[1, 2], [2, 1]]
#次に逆行列を求めます。
mod = pow(10,9)+7

b = np.linalg.inv(a)


Z = np.array([[X],[Y]])

n,m=np.dot(b,Z)
p,q = *n,*m

def comb(n,k,p):
    if k==0:
        return 1
    A = reduce(lambda x,y:x*y%p ,[n-k+1+i for i in range(k)])
    B = reduce(lambda x,y:x*y%p ,[i+1 for i in range(k)])
    return A*pow(B,p-2,p)%p

if p<0 or q<0:
    print(0)
elif not p == int(p) or not q==int(q):
    print(0)
elif p==0 and q==0:
    print(0)
elif p==0:
    print(1)
elif q==0:
    print(1)
else:
    n = int(p)
    m = int(q)
    ans = comb(n+m,m,mod)

    print(ans%mod)

Submission status: Wrong Answer

Solution code:
#n回,m回　　X =n + 2*m  Y = 2*n + m
X,Y = map(int,input().split())
import numpy as np
a = [[1, 2], [2, 1]]
#次に逆行列を求めます。
mod = pow(10,9)+7

b = np.linalg.inv(a)


Z = np.array([[X],[Y]])

n,m=np.dot(b,Z)
p,q = *n,*m



if p<0 or q<0:
    print(0)
elif not p == int(p) or not q==int(q):
    print(0)
elif p==0 and q==0:
    print(0)
elif p==0:
    print(1)
elif q==0:
    print(1)
else:
    n = int(p)+int(q)
    
    fac = [1]*(n+1)
    for i in range(1,n+1):
        fac[i] = (fac[i-1]*i) % mod 
    print(fac[n])

Submission status: Wrong Answer

Solution code:
#n回,m回　　X =n + 2*m  Y = 2*n + m
X,Y = map(int,input().split())
import numpy as np
a = [[1, 2], [2, 1]]
#次に逆行列を求めます。
mod = pow(10,9)+7

b = np.linalg.inv(a)


Z = np.array([[X],[Y]])

n,m=np.dot(b,Z)
p,q = *n,*m



if p<0 or q<0:
    print(0)
elif not p == int(p) or not q==int(q):
    print(0)
else:
    n = int(p)+int(q)
    
    fac = [1]*(n+1)
    for i in range(1,n+1):
        fac[i] = (fac[i-1]*i) % mod 
    print(fac[n])

Submission status: Wrong Answer

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

if (2*a-b)%3 != 0 or (2*b-a)%3 != 0:
    print(0)
    exit()

def comb(n,r):
    fact = 1
    invfact = 1
    for i in range(r):
        fact *= n-i
        fact %= mod
        invfact *= i+1
        invfact %=mod
    return fact*pow(invfact,mod-2,mod)%mod

print(comb(x+y,y)%mod)

Submission status: Wrong Answer

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

if (2*a-b)%3 != 0 or (2*b-a)%3 != 0:
    print(0)
    exit()

def comb(n,r):
    fact = 1
    invfact = 1
    for i in range(r):
        fact *= n-i
        fact %= mod
        invfact *= i+1
        invfact %=mod
    return fact*pow(invfact,mod-2,mod)%mod

print(comb(x+y,y))

Submission status: Wrong Answer

Solution code:
x,y = map(int, input().split())
a = 0
b = 0
if x>y:
  while x>y:
    x-=2
    y-=1
    a+=1
if x<y:
  while x>y:
    x-=1
    y-=2
    b+=1
if x == y and x%3==0:
    a+=x//3
    b+=y//3
else:
  print(0)
  exit(0)
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 )

print(cmb(a+b,b,mod))

Submission status: Wrong Answer

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

fact = [1]
fact_inv = [0] * (MAX_N + 4)
for i in range(MAX_N + 3):
    fact.append(fact[-1] * (i + 1) % mod)

fact_inv[-1] = pow(fact[-1], mod - 2, mod)
for i in range(MAX_N + 2, -1, -1):
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod

def mod_comb_k(n, k, mod):
    return fact[n] * fact_inv[k] % mod * fact_inv[n - k] % mod

OK = False
X, Y = map(int, input().split())
for i in range(0, max(X, Y)+1):
    if (Y - (X - i*2)*2) == i:
        x = i
        y = X - i*2
        print(mod_comb_k(x + y, min(x, y), mod))
        OK = True
        break
if not OK:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *
 
		
#階乗#
lim = 10**6   #必要そうな階乗の限界を入力
fact = [1] * (lim+1)
for n in range(1, lim+1):
	fact[n] = n * fact[n-1] % mod

#階乗の逆元#
fact_inv = [1]*(lim+1)
fact_inv[lim] = pow(fact[lim], mod-2, mod)
for n in range(lim, 0, -1):
	fact_inv[n-1] = n*fact_inv[n]%mod

def C(n, r):
	return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod

X, Y = MAP()

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

if a%1 == b%1 == 0 and 0 <= a and 0 <= b:
	print(C(int(a+b), int(a)))
else:
	print(0)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *
 
		
#階乗#
lim = 10**6   #必要そうな階乗の限界を入力
fact = [1] * (lim+1)
for n in range(1, lim+1):
	fact[n] = n * fact[n-1] % mod

#階乗の逆元#
fact_inv = [1]*(lim+1)
fact_inv[lim] = pow(fact[lim], mod-2, mod)
for n in range(lim, 0, -1):
	fact_inv[n-1] = n*fact_inv[n]%mod

def C(n, r):
	return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod

X, Y = MAP()

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

#print(a, b)
if a%1 == b%1 == 0 and 0 <= a and 0 <= b:
	print(C(int(a+b), int(a)))
else:
	print(0)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *
 
		
#階乗#
lim = 10**6   #必要そうな階乗の限界を入力
fact = [1] * (lim+1)
for n in range(1, lim+1):
	fact[n] = n * fact[n-1] % mod

#階乗の逆元#
fact_inv = [1]*(lim+1)
fact_inv[lim] = pow(fact[lim], mod-2, mod)
for n in range(lim, 0, -1):
	fact_inv[n-1] = n*fact_inv[n]%mod

def C(n, r):
	return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod

X, Y = MAP()

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

#print(a, b)
if a%1 == b%1 == 0:
	print(C(int(a+b), int(a)))
else:
	print(0)