Daily AtCoder # 50 in Python

Introduction

Last time

50 today. Fast. I was planning to write ABC164-D, but I couldn't solve the question, so I put it on hold.

#50 Problem

** Thoughts ** Since the total of (x, y) coordinates increases by 3 in one move, it cannot arrive unless the total of (x + y) is a multiple of 3. Let the movement of (i + 1, j + 2) be s times and the movement of (i + 2, j + 1) be t times. Solving the simultaneous equations of $ s + 2t = x, 2s + t = y $ yields $ s + t = x + y $. Once you have calculated $ s, t $, all you have to do is calculate where to do $ s $. However, if I honestly calculate $ _ {s + t} C _s $, the number is too large to calculate. Therefore, we use the calculation using the inverse element. Detailed calculation article

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

def comb(n,k,mod,fac,ifac):
    k = min(k,n-k)
    return fac[n] * ifac[k] * ifac[n-k] % mod

def make_tables(mod,n):
    fac = [1,1]
    ifac = [1,1]
    inverse = [0,1]

    for i in range(2,n+1):
        fac.append((fac[-1]*i)%mod)
        inverse.append((-inverse[mod%i]* (mod//i)) % mod)
        ifac.append((ifac[-1]* inverse[-1])%mod)
    return fac, ifac

if x > y:
    x, y = y, x
d = x+y
if d % 3 != 0:
    print(0)
else:
    s = (x+y)//3
    t = x - s

    if y > 2 * x: #Cannot arrive if y is greater than 2x
        print(0)
    else:
        mod = 10**9+7

        fac, ifac = make_tables(mod,s)
        ans = comb(s,t,mod,fac,ifac)
        print(ans)

Summary

difficult. See you again, good night.

Recommended Posts

Daily AtCoder # 36 in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Daily AtCoder # 53 in Python
Daily AtCoder # 33 in Python
Daily AtCoder # 7 in Python
Daily AtCoder # 24 in Python
Daily AtCoder # 37 in Python
Daily AtCoder # 42 in Python
Daily AtCoder # 21 in Python
Daily AtCoder # 17 in Python
Daily AtCoder # 38 in Python
Daily AtCoder # 54 in Python
Daily AtCoder # 11 in Python
Daily AtCoder # 15 in Python
Daily AtCoder # 47 in Python
Daily AtCoder # 13 in Python
Daily AtCoder # 40 in Python
Daily AtCoder # 10 in Python
Daily AtCoder # 5 in Python
Daily AtCoder # 39 in Python
Daily AtCoder # 20 in Python
Daily AtCoder # 52 in Python
Daily AtCoder # 3 in Python
Daily AtCoder # 14 in Python
Daily AtCoder # 50 in Python
Daily AtCoder # 26 in Python
Daily AtCoder # 4 in Python
Daily AtCoder # 43 in Python
Daily AtCoder # 29 in Python
Daily AtCoder # 22 in Python
Daily AtCoder # 49 in Python
Daily AtCoder # 27 in Python
Daily AtCoder # 1 in Python
Daily AtCoder # 25 in Python
Daily AtCoder # 16 in Python
Daily AtCoder # 12 in Python
Daily AtCoder # 48 in Python
Daily AtCoder # 23 in Python
Daily AtCoder # 34 in Python
Daily AtCoder # 51 in Python
Daily AtCoder # 31 in Python
Daily AtCoder # 46 in Python
Daily AtCoder # 35 in Python
Daily AtCoder # 9 in Python
Daily AtCoder # 44 in Python
Daily AtCoder # 41 in Python
Atcoder ABC164 A-C in Python
atCoder 173 Python
Python Input Note in AtCoder
Atcoder ABC167 A-D in Python
Atcoder ABC165 A-D in Python
Atcoder ABC166 A-E in Python
Atcoder ABC169 A-E in Python
AtCoder ABC177 A-D in python
Solve Atcoder ABC169 A-D in Python
[Python] Basic knowledge used in AtCoder
Quadtree in Python --2
Python in optimization