Problem: 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.

Submission attempts (in order):
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

def cinit(N):
    g1,g2,g3 = [0]*N,[0]*N,[0]*N
    mod = 10**9+7 
    g1[0],g1[1],g2[0],g2[1],inverse[0],inverse[1] = 1,1,1,1,0,1 
    for i in range( 2, N + 1 ):
        g1[i] = ( ( g1[-1] * i ) % mod )
        inverse[i]=( ( -inverse[mod % i] * (mod//i) ) % mod )
        g2[i]=( (g2[-1] * inverse[-1]) % mod )

X,Y = map(int, input().split())
if (X+Y)%3==0and(X<Y*2 or Y<X*2):
    cinit((X+Y)//3)
    p = (min(X,Y)*2 - max(X,Y))//3
    print(cmb((X+Y)//3,p,mod))
else:
    print(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 = 1000000
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 計算用テーブル

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

X,Y = map(int, input().split())
maxY = X * 2
if (X+Y)%3 == 0 and (maxY - Y)%3==0:
    cinit()
    num2 = abs(maxY - Y)//3
    num1 = X - num2 * 2
    print(cmb(num1+num2,num2,mod))
else:
    print(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 = 1000000
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 計算用テーブル

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

X,Y = map(int, input().split())
maxY = X * 2
if (maxY - Y)%3==0:
    cinit()
    num2 = abs(maxY - Y)//3
    num1 = X - num2 * 2
    print(num1,num2)
    print(cmb(num1+num2,num2,mod))
else:
    print(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 = 1000000
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 計算用テーブル

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

X,Y = map(int, input().split())
maxY = X * 2
if (maxY - Y)%3==0:
    cinit()
    num2 = abs(maxY - Y)//3
    num1 = X - num2 * 2
    print(cmb(num1+num2,num2,mod))
else:
    print(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 = 1000000
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 計算用テーブル

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

X,Y = map(int, input().split())
maxY = X * 2
if X!=0 and Y!=0 and (maxY - Y)%3==0:
    cinit()
    num2 = abs(maxY - Y)//3
    num1 = X - num2 * 2
    print(cmb(num1+num2,num2,mod))
else:
    print(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

def cinit(N):
    g1,g2,inverse = [0]*N,[0]*N,[0]*N
    mod = 10**9+7 
    g1[0],g1[1],g2[0],g2[1],inverse[0],inverse[1] = 1,1,1,1,0,1 
    for i in range( 2, N + 1 ):
        g1[i] = ( ( g1[-1] * i ) % mod )
        inverse[i]=( ( -inverse[mod % i] * (mod//i) ) % mod )
        g2[i]=( (g2[-1] * inverse[-1]) % mod )

X,Y = map(int, input().split())
if (X+Y)%3==0and(X<Y*2 or Y<X*2):
    cinit((X+Y)//3)
    p = (min(X,Y)*2 - max(X,Y))//3
    print(cmb((X+Y)//3,p,mod))
else:
    print(0)

mod = 10**9+7
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
def cinit(N):
    for i in range( 2, N + 1 ):
        g1[i] = ( ( g1[i-1] * i ) % mod )
        inverse[i] = ( ( -inverse[mod % i] * (mod//i) ) % mod )
        g2[i] = ( (g2[i-1] * inverse[i]) % mod )
X,Y = map(int, input().split())
if (X+Y)%3==0and(X<Y*2 or Y<X*2):
    N = (X+Y)//3
    g1,g2,inverse = [0]*(N+1),[0]*(N+1),[0]*(N+1)
    g1[0],g1[1],g2[0],g2[1],inverse[0],inverse[1] = 1,1,1,1,0,1
    cinit(N)
    p = (min(X,Y)*2 - max(X,Y))//3
    print(cmb((X+Y)//3,p,mod))
else:
    print(0)

Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Accepted