結果

問題 No.1683 Robot Guidance
ユーザー lam6er
提出日時 2025-03-20 20:42:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 1,823 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 82,032 KB
実行使用メモリ 123,388 KB
最終ジャッジ日時 2025-03-20 20:42:58
合計ジャッジ時間 6,418 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

# Precompute factorial and inverse factorial arrays up to a sufficiently large number
max_fact = 3 * 10**6 + 10  # Adjusted to handle possible large values
fact = [1] * (max_fact + 1)
for i in range(1, max_fact + 1):
    fact[i] = fact[i-1] * i % MOD

inv_fact = [1] * (max_fact + 1)
inv_fact[max_fact] = pow(fact[max_fact], MOD-2, MOD)
for i in range(max_fact - 1, -1, -1):
    inv_fact[i] = inv_fact[i+1] * (i+1) % MOD

def comb(n, k):
    if n < 0 or k < 0 or k > n:
        return 0
    return fact[n] * inv_fact[k] % MOD * inv_fact[n - k] % MOD

# Read input
A, B, X, Y = map(int, input().split())

# Calculate the number of intervals for each state (0, 1, 2, 3)
n0 = (B // 4) + 1
n1 = ((B - 1) // 4) + 1
n2 = ((B - 2) // 4) + 1
n3 = ((B - 3) // 4) + 1
n = [n0, n1, n2, n3]

# Check if (A - X - Y) is even and non-negative
temp = A - X - Y
if temp % 2 != 0:
    print(0)
    exit()
D = temp // 2
if D < 0:
    print(0)
    exit()

# Determine the valid range for k2
low_k2 = max(0, -X)
maxY = max(0, -Y)
high_k2 = D - maxY

if low_k2 > high_k2:
    print(0)
    exit()

total = 0

# Iterate over all possible k2 values
for k2 in range(low_k2, high_k2 + 1):
    k3 = D - k2
    if k3 < max(0, -Y):
        continue
    k0 = k2 + X
    k1 = k3 + Y
    if k0 < 0 or k1 < 0:
        continue
    
    ks = [k0, k1, k2, k3]
    current = 1
    for s in range(4):
        k = ks[s]
        ns = n[s]
        if ns == 0:
            if k != 0:
                current = 0
                break
            continue
        if k < 0:
            current = 0
            break
        a = k + ns - 1
        b = ns - 1
        if a < 0 or b < 0 or b > a:
            current = 0
            break
        c = comb(a, b)
        current = current * c % MOD
    
    total = (total + current) % MOD

print(total)
0