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: Runtime Error

Solution code:
import copy
x,y = [int(s) for s in input().split()]
# print(x,y)
def replace_zahyo(list, sum):
    print(list,sum)
    list1 = copy.deepcopy(list)
    if list == []: #リスtがからで終わり
        return sum
    for first in list1:
        if first == (0,0): #0,0にたどり着いたら加算
            sum+=1
            list.remove(first)
            return replace_zahyo(list,sum)
        elif first[0] <= 0 or first[1] <= 0: #外に出たら終わり
            list.remove(first)
            return replace_zahyo(list,sum)
        else:#普通
            list.append((first[0]-1, first[1]-2))
            list.append((first[0]-2, first[1]-1))
            list.remove(first)
            return replace_zahyo(list,sum)

def replace_dict(dict,sum):
    # print(dict)
    if dict == {}:
        return sum
    # print(dict)
    # print(dict.keys())
    first_key = sorted(dict.keys())[0]
    first_value = dict.pop(first_key)
    # print(first_key,first_value)
    if first_key == (0,0):
        sum += first_value
        return replace_dict(dict,sum)
    elif first_key[0] <= 0 or first_key[1] <= 0:
        return replace_dict(dict,sum)
    else:
        dict.setdefault((first_key[0]-1,first_key[1]-2),0)
        dict.setdefault((first_key[0]-2,first_key[1]-1),0)
        dict[(first_key[0]-1,first_key[1]-2)]+= first_value
        dict[(first_key[0]-2,first_key[1]-1)]+= first_value
        return replace_dict(dict,sum)


print(replace_dict({(x,y):1},0)%(10**9+7))

Submission status: Runtime Error

Solution code:

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


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])
            #print(result)
            result%=(10**9+7)

    return result


X,Y=acinput()

import numpy as np
from numpy.linalg import solve
 

x=solve([[1,2],[2,1]],[X,Y])

#print(x)

if np.all(x%1.0==0) and np.all(x>=0):
    print(cmb(int(np.sum(x)),int(np.min(x)),10**9+7))

else:
    print(0)

Submission status: Runtime Error

Solution code:
t = input().rstrip().split(" ")
x = int(t[0])
y = int(t[1])
temp = {}
def get_num(x, y):
    res = 0
    if (x == 0) and (y == 0):
        res = 1
    elif (x <= 0) or (y == 0):
        res = 0
    elif (x, y) in temp:
        res = temp[(x, y)]
    else:
        res += get_num(x-1, y-2)
        res += get_num(x-2, y-1)
        res %= (1e9 + 7)
        temp[(x, y)] = res
    return res

print(int(get_num(x, y)))

Submission status: Runtime Error

Solution code:

from math import *
x, y=list(map(int,input().split()))

m1=(2*y-x)/3
n1=y-m1*2

m2=(2*x-y)/3
n2=x-m2*2

if(m1.is_integer()):
  print(int(factorial(m1+n1)/factorial(m1)/factorial(n1)))

else if (m2.is_integer()):
  print(int(factorial(m2+n2)/factorial(m2)/factorial(n2)))

else:
 print(0)

Submission status: Runtime Error

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
from inspect import currentframe
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def MI() : return map(int, input().split())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def chkprint(*args) : names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}; print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

X, Y = LI()

ans = 0
for i in range(int(X // 2) + 1):
    if 2 * (X - (i * 2)) + i == Y:
        ans += comb((X - (i * 2)) + 1, i)
        ans %= MOD

print(ans)

Submission status: Runtime Error

Solution code:
X,Y=map(int,input().split())
if (X+Y)%3!=0:
  print(0)
else:
  if 2*X<Y or 2*Y<X:
    print(0)
  else:
    p=(2X-Y)//3
    q=(X+Y)//3
    
    import math
    print(math.factorial(q) //(math.factorial(p) *math.factorial(q- p))%1000000007)

Submission status: Runtime Error

Solution code:
import math
x,y = map(int,input().split())
sum_xy = x+y

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

mod = 10**9+7 #出力の制限
N = 10**6
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

if sum_xy%3!=0:
    print(0)
else:
    #a+b
    ab_sum = sum_xy//3
    a = x-ab_sum
    b = y-ab_sum
#     a_kaizyo = 1
#     b_kaizyo = 1
#     ab_kaizyo = 1
#     for i in range(1,a+1):
#         temp =  a_kaizyo*i
#         a_kaizyo = temp%inf
#     for i in range(1,b+1):
#         temp =  b_kaizyo*i
#         b_kaizyo = temp%inf
#     for i in range(1,ab_sum+1):
#         temp =  ab_kaizyo*i
#         ab_kaizyo = temp%inf

    print(cmb(ab_sum,a,inf))

Submission status: Runtime Error

Solution code:
t = input().rstrip().split(" ")
x = int(t[0])
y = int(t[1])
temp = {}
def get_num(x, y):
    res = 0
    if (x == 0) and (y == 0):
        res = 1
    elif (x <= 0) or (y == 0):
        res = 0
    elif (x, y) in temp:
        res = temp[(x, y)]
    else:
        res += get_num(x-1, y-2) % (1e9+7)
        res += get_num(x-2, y-1) % (1e9+7)
        temp[(x, y)] = res
    return res

print(int(get_num(x, y)))

Submission status: Runtime Error

Solution code:
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under



X, Y = (int(i) for i in input().split())
if (X+Y) % 3 == 0 and max(X,Y) - min(X,Y) <= min(X,Y):
    print(cmb(X+Y/3, abs(X-Y)) / (10**9+7))
else:
    print(0)

Submission status: Runtime Error

Solution code:
from scipy.special import comb
X,Y = map(int,input().split())
if (X+Y)%3 == 0:
    N = (X+Y)//3
    print(comb(N,X-N, exact=True)%(10**9+7))
else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys
import math
from statistics import mean
from functools import reduce, lru_cache
from itertools import permutations


@lru_cache()
def distance(p1, p2):
    return math.sqrt(sum([(a - b) ** 2 for a, b in zip(p1, p2)]))


def distances(arr):
    return [distance(arr[i], arr[i+1]) for i in range(len(arr)-1)]


def main(x, y):
    print(reachable((0,0), (x,y)))


def reachable(current, target):
    if current == target:
        return 1
    elif (current[0]>target[0]) or (current[1]>target[1]):
        return 0
    else:
        return reachable((current[0]+1, current[1]+2), target) + reachable((current[0]+2, current[1]+1), target)


if __name__ == '__main__':
    p = input().split(' ')
    main(int(p[0]), int(p[1]))

Submission status: Runtime Error

Solution code:
from math import factorial
import sys

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

if min(X, Y) == 1:
    if max(X, Y) == 2:
        print('1')
    else:
        print('0')
else:

    for i in range(round(min(X, Y) / 2) + 1):
        if (min(X, Y) - i * 2) * 2== (max(X, Y) - i):
            answer = factorial(min(X, Y) - i) / factorial(i) / factorial(min(X, Y) - i * 2)
            print(round(answer % (10 ** 9 + 7)))
            sys.exit()
    print('0')

Submission status: Runtime Error

Solution code:
import sys
x,y=map(int,input().split())
if ((2*x)-y)%3!=0 or ((2*y)-x)%3!=0:
  print(0)
  sys.exit()
n=((2*x)-y)//3
m=((2*y)-x)//3
l=min(m,n)
num=m+n
z=1
for i in range(1,l):
  num=num*(m+n-i)
  z=z*i
z=z*l
print((num//z)%((10**9)+7))

Submission status: Runtime Error

Solution code:
X, Y=map(int, input().split())
a=(2*X-Y)/3
b=(2*Y-X)/3
import math
if a.is_integer()==True and b.is_integer()==True:
  print((math.factorial(a+b)//(math.factorial(a)*math.factorial(b)))%(10**9+7))
else:
  print(0)

Submission status: Runtime Error

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

# dp = [[0]*(X+1) for _ in range(Y+1)]
#
# dp[2][1] = 1
# dp[1][2] = 1
#
# for i in range(Y+1):  # iがy座標にあたる
#     for j in range(X+1):  # jがx座標にあたる
#         if i < 2 or j < 2:
#             continue
#         dp[i][j] += dp[i-1][j-2] + dp[i-2][j-1]
#
# print(dp[Y][X])

# dp_x = [0]*(X+1)
# dp_x[1] = 1
# dp_x[2] = 2
# for i in range(3, X+1):  # Xの動きだけ専門
#     dp_x[i] += dp_x[i-1] + dp_x[i-2]
#
# dp_y = [0]*(Y+1)
# dp_y[1] = 1
# dp_y[2] = 2
# for i in range(3, Y+1):
#     dp_y[i] += dp_y[i-1] + dp_y[i-2]
# print(dp_x[X-1]*dp_y[Y-1]%1000000007)
# # print(dp_x)
# # print(dp_y)

#
# dp = [[0]*4 for _ in range(4)]
# for

if (X+Y)%3 != 0 or not(min(X, Y)*2 >= max(X, Y)):
    print(0)
    exit()
dp = [0]*(min(X, Y)+1)
dp[1] = 1
dp[2] = 2
for i in range(3, len(dp)):
    dp[i] += (dp[i-1] + dp[i-2])%1000000007

print(sum(dp[math.ceil(max(X, Y)/2):-1])%1000000007)
# print(dp)

Submission status: Runtime Error

Solution code:
import math

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

if ((x+y)%3==0) & (x>=(x+y)/3) & (y>=(x+y)/3):
	a=math.factorial((x+y)/3)
	b=math.factorial((2*x-y)/3)
	c=math.factorial((2*y-x)/3)
	ans=int((a/b/c)%(10**9+7))
	
else:
	ans=0
print(ans)

Submission status: Runtime Error

Solution code:
#import itertools
from scipy.special import comb

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

def main():
    x, y = map(int, input().split())

    if x < y:
        x_ = y
        y_ = x

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

    else:
        n = (x+y)//3
        r = x//2

        #a = len(list(itertools.combinations(range(n), r)))
        #a = comb(n, r, exact=True)
        #a = comb_1(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 )

        a = cmb(n,r,mod)

        print(a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

    return inv_t
def co(n,r):
    P = 10**9 + 7
    N = r+1
    inv_t = [0]+[1]
    for i in range(2,N+1):
        inv_t += [inv_t[P % i] * (P - int(P / i)) % P]
    ans =1
    for i in range(1,r+1):
        ans = (ans * (n+i-1)) % P
    for i in range(1,r+1):
        ans = (ans * inv_t[i]) % P
    return ans
if (X+Y)%3==0:
    Z=X+Y
    if min(X,Y)-Z//3 >-1:
        #print(1)
        comb = Combination(Z)
        print(comb(Z//3,min(X,Y)-Z//3))
        #print(co(Z//3,min(X,Y)-Z//3))
    else:
        print(0)
else:
    print(0)

Submission status: Runtime Error

Solution code:
from math import factorial
import sys

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

if min(X, Y) == 1:
    if max(X, Y) == 2:
        print('1')
    else:
        print('0')
else:

    for i in range(round(min(X, Y) / 2) + 1):
        if (min(X, Y) - i * 2) * 2== (max(X, Y) - i):
            answer = factorial(max(X, Y) - i) / factorial(i) / factorial(max(X, Y) - i * 2)
            print(round(answer % (10 ** 9 + 7)))
            sys.exit()
    print('0')

Submission status: Runtime Error

Solution code:
import math
x,y = map(int, input().split())
n = 2 * x - y
m = 2 * y - x

ans = 0
if n % 3 == 0:
	if m % 3 == 0:
		nn = int(n / 3)
		mm= int(m / 3)
		ans = math.factorial(nn+ mm) / math.factorial(nn) * math.factorial(mm)
		ans = int(ans % ((10**9) + 7))

print(ans)

Submission status: Runtime Error

Solution code:
class Combination(object):
        """参考: https://harigami.net/contents?id=5f169f85-5707-4137-87a5-f0068749d9bb"""
        __slots__ = ['mod', 'factorial', 'inverse']

        def __init__(self, max_val_arg: int = 10**6, mod: int = 10**9 + 7):
            fac, inv = [1], []
            fac_append, inv_append = fac.append, inv.append

            for i in range(1, max_val_arg + 1):
                fac_append(fac[-1] * i % mod)

            inv_append(pow(fac[-1], mod - 2, mod))

            for i in range(max_val_arg, 0, -1):
                inv_append((inv[-1] * i) % mod)

            self.mod, self.factorial, self.inverse = mod, fac, inv[::-1]

        def combination(self, n, r):
            if n < 0 or r < 0 or n < r:
                return 0
            return self.factorial[n] * self.inverse[r] * self.inverse[n - r] % self.mod

if not S%3==0:
    print(0)
    exit()
else:
    small = S//3
    s_elm = min(X,Y)
    if s_elm<small:
        print(0)
        exit()
    r = s_elm - small
    ans = Combination().combination(small, r)
    ans %= u
print(ans)

Submission status: Runtime Error

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

    return inv_t
def co(n,r):
    P = 10**9 + 7
    N = r+1
    inv_t = [0]+[1]
    for i in range(2,N+1):
        inv_t += [inv_t[P % i] * (P - int(P / i)) % P]
    ans =1
    for i in range(1,r+1):
        ans = (ans * (n+i-1)) % P
    for i in range(1,r+1):
        ans = (ans * inv_t[i]) % P
    return ans
if (X+Y)%3==0:
    Z=X+Y
    if min(X,Y)-Z//3 >-1:
        #print(1)
        comb = Combination(Z//3)
        print(comb(Z//3,min(X,Y)-Z//3))
        #print(co(Z//3,min(X,Y)-Z//3))
    else:
        print(0)
else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys
import scipy.special as ss
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

x,y= (int(_) for _ in input().split()) 
if (x+y)%3!=0:
    print(0)
    sys.exit()
else:
    n=(x+y)/3
    n=int(n)
    if x<n or y<n:
         print(0)
         sys.exit()
    else:
        x=x-n
        y=y-n
        ans=int(ss.perm(x+y,x))
        print(ans%1000000007)

Submission status: Runtime Error

Solution code:
import math

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

def affine_inverse(x,y):
	return (x*2+y*-1)/3, (x*-1+y*2)/3

def func():
	x,y = [int(p) for p in input().split(' ')]
	x,y = affine_inverse(x,y)

	if not float.is_integer(x) or not float.is_integer(y):
		print('0')
		return

	x = int(x)
	y = int(y)

	fac = [0 for r in range(x+y+1)]
	fac_inv = [0 for r in range(x+y+1)]

	for i in range(x+y+1):
		if i == 0:
			fac[i] = 1
		else:
			fac[i] = (fac[i-1]*i)%1000000007

	for i in reversed(range(x+y+1)):
		if i == x+y:
			fac_inv[i] = modinv(math.factorial(i),1000000007)
		else:
			fac_inv[i] = fac_inv[i+1] * (i+1) % 1000000007

	print(fac[x+y]*fac_inv[x]*fac_inv[y]%1000000007)

func()

Submission status: Runtime Error

Solution code:
import math


X, Y = (int(i) for i in input().split())

i = (2*X-Y)//3
j = (2*Y-X)//3

if(  ((2*X-Y)%3)!=0 or (2*Y-X)%3!=0  ):
    print(0)
else:
    print( (math.factorial(i+j)/math.factorial(i)/math.factorial(j) ) % 1000000007 )

Submission status: Runtime Error

Solution code:
class Combination(object):
        """参考: https://harigami.net/contents?id=5f169f85-5707-4137-87a5-f0068749d9bb"""
        __slots__ = ['mod', 'factorial', 'inverse']

        def __init__(self, max_val_arg: int = 10**6, mod: int = 10**9 + 7):
            fac, inv = [1], []
            fac_append, inv_append = fac.append, inv.append

            for i in range(1, max_val_arg + 1):
                fac_append(fac[-1] * i % mod)

            inv_append(pow(fac[-1], mod - 2, mod))

            for i in range(max_val_arg, 0, -1):
                inv_append((inv[-1] * i) % mod)

            self.mod, self.factorial, self.inverse = mod, fac, inv[::-1]

        def combination(self, n, r):
            if n < 0 or r < 0 or n < r:
                return 0
            return self.factorial[n] * self.inverse[r] * self.inverse[n - r] % self.mod

if not S%3==0:
    print(0)
    exit()
else:
    small = S//3
    s_elm = min(X,Y)
    if s_elm<small:
        print(0)
        exit()
    print('a')
    r = s_elm - small
    print(small,r)
    ans = Combination().combination(small, r)
    ans %= u
print(ans)

Submission status: Runtime Error

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

if (X+Y)%3!=0:
  print(0)
  
n=int((X+Y)/3)
k=abs(X-Y)-1

def c(n,k): 
  return 1 if(k<=0 or n<=k) else c(n-1, k-1) + c(n-1, k)

print(c(n,k))

Submission status: Runtime Error

Solution code:
from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math

# 入力 =stdin.readline().rstrip()
# 分割 = stdin.readline().rstrip().split()

'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''

'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''

'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  # 和集合
print(list(set(a) & set(b)))  # 積集合
'''

'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''

'''
.append(要素)
.extend(要素)
.insert(場所,要素)
del 配列名[場所]
-1で末尾で末尾から一つ前
'''
'''
間に空白追加
' '.join(lis)
'''
'''
copy.copy(配列名)6

'''
'''
sort()50
sorted(reverse=true)
'''
'''
2分探索
bisect.bisect_left(リスト, 要素)
'''


def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)


t = [int(s) for s in stdin.readline().rstrip().split()]
X = t[0]
Y = t[1]
if ((X + Y) % 3 != 0):
    print(0)
else:
    n = (X + Y) / 3
    pos = X-n
    res = permutations_count(n, pos)
    print(res % (10**9+7))

Submission status: Runtime Error

Solution code:
import sys

def check(x, y):
    if x < y:
        tmp = x
        x = y
        y = tmp

    n = x - y
    x -= 2 * n

    if x % 3 == 0:
        return True
    else:
        return False
    

# Read
line = sys.stdin.readlines()[0]
X, Y = map(int,line.split(' '))

if not check(x, y):
    print(0)
    exit(0)

Submission status: Runtime Error

Solution code:
from math import factorial
import sys

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

if min(X, Y) == 1:
    if max(X, Y) == 2:
        print('1')
    else:
        print('0')
else:

    for i in range(min(X + 1, Y + 1) - 2 * i):
        if (min(X, Y) - i * 2) * 2== (max(X, Y) - i):
            answer = factorial(max(X, Y) - i) / factorial(i) / factorial(max(X, Y) - i * 2)
            print(round(answer % (10 ** 9 + 7)))
            sys.exit()
    print('0')

Submission status: Runtime Error

Solution code:
import math

a,b=map(int, input().split())
tempA, tempB =a,b
right=0
left=0
if a>b:
  temp = a//3
  tempA -= temp*3
  tempB -= temp*3
  right += temp
  left += temp
else
  temp = b//3
  tempA -= temp*3
  tempB -= temp*3
  right += temp
  left += temp
while tempA+tempB != 0:
  if tempA < 0 or tempB <0:
    print(0)
    exit()
  elif tempA >= tempB:
    tempA -= 2
    tempB -= 1
    right+=1
    #print("R")
  else:
    tempB -= 2
    tempA -= 1
    left+=1
    #print("L")

#print(right, left)
ans = math.factorial(right+left)/(math.factorial(right)*math.factorial(left))

print(int(ans%(10*9+7)))

Submission status: Runtime Error

Solution code:
X, Y=map(int, input().split())
a=(2*X-Y)/3
b=(2*Y-X)/3
import math
if a.is_integer()==True and b.is_integer()==True:
  print((math.factorial(a+b)/(math.factorial(a)*math.factorial(b)))%(10**9+7))
else:
  print(0)

Submission status: Runtime Error

Solution code:
import math
def comb(x,y):
    return (math.factorial(math.permutations_count(x, y))/math.factorial(y))



x,y = map(int,input().split())
if x<y:
    temp = x
    x = y
    y = x
count = 0

if x == y and x % 3 ==0 and y % 3 == 0:
    print(int(comb((x/3)*2,x/3)))
else:
    while True:
        print(count)
        if x == y*2:
            print(int(comb(y+(count*2), count)))
            break
        elif x > y*2:
            print(0)
            break
        else:
            x =x - 3
            y =y - 3
            count +=1

Submission status: Runtime Error

Solution code:
from functools import reduce

def modpow(a, m):
    ret = 1
    while m > 0:
        if m & 1:
            ret = ret * a % mod
        a = a * a % mod
        m = m >> 1
    return ret

def modinv(a):
    return modpow(a, mod - 2)

def cmb(n, r):
    r = min(r, n - r)
    if r == 0:
        return 1
    over = reduce(lambda a, b: a * b % mod, range(n, n - r, -1))
    under = reduce(lambda a, b: a * b % mod, range(1, r + 1))
    return over * modinv(under) % mod

x, y = [int(i) for i in input().split()]
if (x + y) % 3:
    print(0)
else:
    mod = 10**9 + 7
    print(cmb((x + y) // 3, (2 * x - y) // 3))

Submission status: Runtime Error

Solution code:
import math


X, Y = (int(i) for i in input().split())

i = (2*X-Y)//3
j = (2*Y-X)//3

if(  ((2*X-Y)%3)!=0 or (2*Y-X)%3!=0  ):
    print(0)
else:
    print(math.factorial(i+j)/math.factorial(i)/math.factorial(j))

Submission status: Runtime Error

Solution code:
x, y = input().split()
x = int(x)
y = int(y)
xy_sum = x + y
point_num = 0
kimari = 10 ** 9 + 7
if (xy_sum % 3 == 0):
    point_num += ((xy_sum - 1) / 3) ** 2
    point_num += (xy_sum - 1) / 6
    count = 2 ** point_num
    print(count % kimari)
else:
    print(0)

Submission status: Runtime Error

Solution code:
import math

x,y = map(int,input().split())
diff = abs(x-y)
mod = 10**9 + 7
n = 1
if min(x,y) * 2 == max(x,y):
    print("1")
elif (min(x,y) - diff) % 3 != 0:
    print("0")
else:
    cir = (min(x,y) - diff) // 3
    all_step = cir * 2 + diff
    a_buf = all_step
    for i in range(min(all_step-cir, cir)):
        n *= a_buf
        a_buf -= 1
    k = math.factorial(min(all_step-cir, cir))
    print((n // k) % mod)

Submission status: Runtime Error

Solution code:
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under



X, Y = (int(i) for i in input().split())
if (X+Y) % 3 == 0 and max(X,Y) - min(X,Y) <= min(X,Y):
    print(cmb(X+Y/3, abs(X-Y)))
else:
    print(0)

Submission status: Runtime Error

Solution code:
import math
def com(n, r):
    return math.factorial(n) / (math.factorial(n - r)*math.factorial(r))
x,y=map(int,input().split())
if (2*x-y)/3==int((2*x-y)/3) and (2*y-x)/3==int((2*y-x)/3):
  k=(2*x-y)/3
  l=(2*y-x)/3
  print(com(k+l,k))
else:
  print(0)

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
input=lambda :sys.stdin.readline().rstrip()
inputs= sys.stdin.buffer.readlines
INF=float("inf")
MOD=10**9+7
#input = sys.stdin.buffer.readline
sys.setrecursionlimit(2147483647)
#rstrip().decode('utf-8')
#import math
#import numpy as np
#import operator
#import bisect
#from heapq import heapify,heappop,heappush
#from math import gcd
#from fractions import gcd
#from collections import deque
#from collections import defaultdict
#from collections import Counter
#from itertools import accumulate
#from itertools import groupby
#from itertools import permutations
#from itertools import combinations
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import floyd_warshall
#from scipy.sparse.csgraph import csgraph_from_dense
#from scipy.sparse.csgraph import dijkstra
#map(int,input().split())
#from scipy.special import comb

def calc(a,b,p):
	if b == 0:
		return 1
	elif b % 2 == 0:
		d = calc(a, b / 2, p)
		return (d * d) % p
	else:
		return (a * calc(a, b - 1, p)) % p





def main():
	X,Y=map(int,input().split())
	
	a=max(X,Y)
	b=min(X,Y)
	
	ans=0
	
	t=2*a-b
	
	
	if t%3==0 and t>0 and a<=b*2:
		c1=t//3
		c2=a-2*t//3
		
		n=c1+c2
		r=min(c1,c2)
		
		mod = 10 ** 9 + 7  # 出力の制限
		N = n
		g1 = [1, 1]  # 元テーブル
		g2 =1  # 逆元テーブル
		inv = [0, 1]  # 逆元テーブル計算用テーブル
		
		for i in range(2,r+1):
			inv.append(calc(i,mod-2,mod))
		
		for i in range(2,r):
			g2=g2*inv[i]%mod
			
			
		
		for i in range(2, 100):
			g1.append((g1[-1] * i) % mod)
			print(g1[n]*g2)
		
		
	print(ans)
		
	
	
	
	
if __name__ == '__main__':
	main()

Submission status: Runtime Error

Solution code:
import math
import time

start = time.perf_counter()
x, y = map(int, input().split())

if (x + y) % 3 != 0:
    print(0)
else:
    n = (x + y) / 3
    a = y - n
    b = x - n
    res = math.factorial(n) // (math.factorial(a) * math.factorial(b))
    print(res % (10 ** 9 + 7))

Submission status: Runtime Error

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

x,y=map(int,input().split())
a=[0]*1001
b=[0]*1001
a[1]=1
if (x+y)%3!=0:
    print(0)
    exit()
#move cnt
m=(x+y)//3

if x>m*2 or y>m*2:
    print(0)
    exit()
for i in range(m):
    for j in range(1,1000):
        if i%2==0:
            if a[j]==a[j-1]==0: break
            b[j]=(a[j]+a[j-1])%mod
        elif i%2:
            if b[j]==b[j-1]==0: break
            a[j]=(b[j]+b[j-1])%mod
    if i%2==0:
        a=[i for i in b]
    else:
        b=[i for i in a]
o=min(x,y)
p=max(x,y)
print(a[abs(p-o-m)])

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

INPUT = lambda: sys.stdin.readline().rstrip()
MAP = lambda: map(int, INPUT().split())

sys.setrecursionlimit(10 ** 9)
mod = 10 ** 9 + 7


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

    s = (2*X - Y)//3
    t = (2*Y - X)//3
    if s < 0 or t < 0:
        print(0)
        sys.exit()
        
    MAX_NUM = max(X, Y)
    dp = np.zeros((X+1, Y+1), np.int)
    for i in range(MAX_NUM):
        if i == 0:
            for j in range(len(dp[0])):
                if j == 0: dp[i][j] = 0
                else: dp[i][j] = 1
        else:
            for j in range(len(dp[0]-1)):
                if j == 0: dp[i][j] = 1
                else: dp[i][j] = dp[i-1][j] + dp[i][j-1]

    print(dp[s][t] % mod)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

MOD = 10**9+7


if (X+Y)%3 != 0:
    ans = 0
else:
    N = int((X+Y)/3)

    if X >= Y:
        #K = N - (X - Y) + 1
        K = N/2 - (X - Y) + 1

    else:
        K = N/2 + (Y - X) + 1

    #ans = factorial(N-1)/(factorial(N-K)*factorial(K-1))
    # ans = factorial(N+1-1)/(factorial(N-K+1)*factorial(K-1))
    # ans %= MOD
    bunsi = factorial(N)%MOD
    bunbo = (factorial(N-K+1)*factorial(K-1))%MOD
    ans = int(bunsi/bunbo)


#print(N)
#print(K)
print(ans)

Submission status: Runtime Error

Solution code:
import math

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

def affine_inverse(x,y):
	return (x*2+y*-1)/3, (x*-1+y*2)/3

def main():
	x,y = [int(p) for p in input().split(' ')]
	x,y = affine_inverse(x,y)

	if float.is_integer(x) or float.float.is_integer(y):
		print(0)
		return

	x = int(x)
	y = int(y)

	fac = [0 for r in range(x+y+1)]
	fac_inv = [0 for r in range(x+y+1)]

	for i in range(x+y+1):
		if i == 0:
			fac[i] = 1
		else:
			fac[i] = (fac[i-1]*i)%1000000007

	for i in reversed(range(x+y+1)):
		if i == h+w-3:
			fac_inv[i] = modinv(math.factorial(i),1000000007)
		else:
			fac_inv[i] = fac_inv[i+1] * (i+1) % 1000000007

	print(fac[x+y]*fac_inv[x]*fac_inv[y])%1000000007

main()

Submission status: Runtime Error

Solution code:
import math
import numpy as np
#ABC共通 
abc = input()
abc_list = abc.split(' ')
 
#問題用変数
abc_intlist = []

 
#inputを整数化
for c1 in abc_list:
  abc_intlist.append(int(c1))
 
#以下処理
tesuu = (abc_intlist[0]+abc_intlist[1])/3
diff = max(abc_intlist)-min(abc_intlist)
k = (tesuu-diff)/2

if (tesuu-diff)%2==1:
  print(0)
elif k<0:
  print(0)
elif (abc_intlist[0]+abc_intlist[1])%3 != 0:
    print(0)
else:
    maa = max([tesuu-k,k])
    mk = int(tesuu - maa)
    calc=np.zeros(mk+1, dtype=np.float128)
    calc[0] = 1
    #for c1 in range(mk):
    #    calc = calc*(tesuu-c1)
    for c2 in range(mk):
        calc[c2+1] = calc[c2]*(tesuu-c2)/(mk-c2)
    #print(tesuu/1)
    #print(calc[c2+1])
    print(calc[c2+1]%(10**9+7))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

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

mod=10**9+7
fact, inv_fact = [1], [1]
fact_tmp = 1
for i in range(1, max(X,Y)+1):
    fact_tmp *= i
    fact_tmp %= mod
    fact.append(fact_tmp)
    inv_fact.append(pow(fact_tmp, mod-2, mod))
 
 
def ncr(n,r):
	if n < 0 or r < 0 or n < r:	return 0
	else:	return (fact[n] * inv_fact[r] * inv_fact[n-r]) %mod

if X==Y:
    if X%3!=0:
        print 0
    else:
        x=X/3
        print ncr(x*2,x)
else:
    if X<Y: X,Y=Y,x
    sho=X/2
    x=X-(sho*2)
    y=Y-sho
    if (x,y)==(0,0):
        print 1
    elif (x,y)!=(1,2):
        print 0
    else:
        print ncr(sho+1,sho)

Submission status: Runtime Error

Solution code:
# D
def perm(N, k):
    a = 1
    for i in range(int(k)):
        a*=N
        N-=1
        return a
from math import factorial
# from math import perm

tmp = list(map(int, input().split()))
X = tmp[0]
Y = tmp[1]

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

if not x.is_integer():
    print(0)
elif not y.is_integer():
    print(0)
else:
    if x>y:
        denominator = perm(x+y, x)%(10**9+7)
        numerator = factorial(y)%(10**9+7)
        tmp = denominator//numerator
        print(int(tmp))
    else:
        denominator = perm(x+y, y)%(10**9+7)
        numerator = factorial(x)%(10**9+7)
        tmp = denominator//numerator
        print(int(tmp))