結果

問題 No.1489 Repeat Cumulative Sum
ユーザー lam6er
提出日時 2025-03-20 20:40:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 1,620 bytes
コンパイル時間 231 ms
コンパイル使用メモリ 82,232 KB
実行使用メモリ 94,120 KB
最終ジャッジ日時 2025-03-20 20:40:44
合計ジャッジ時間 3,080 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

def main():
    import sys
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    M = int(input[ptr])
    ptr += 1
    A = list(map(int, input[ptr:ptr + (N - 1)]))
    
    r = M % MOD
    t_max = N - 2
    
    # Compute prefix_products
    required_i = (MOD - r) % MOD
    prefix_products = []
    current_product = 1
    for t in range(t_max + 1):
        if required_i <= t:
            current_product = 0
        else:
            term = (r + t) % MOD
            current_product = (current_product * term) % MOD
        prefix_products.append(current_product)
    
    # Precompute factorial and inverse factorial up to t_max + 1
    max_fact = t_max + 1
    fact = [1] * (max_fact + 2)
    for i in range(1, max_fact + 1):
        fact[i] = (fact[i-1] * i) % MOD
    
    inv_fact = [1] * (max_fact + 2)
    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
    
    sum_total = 0
    for k in range(2, N + 1):
        idx = k - 2  # A's index for A_2, A_3, ..., A_N
        a_k = A[idx]
        t = N - k
        if t < 0 or t > t_max:
            continue  # Shouldn't happen as k ranges from 2 to N
        product_contrib = prefix_products[t]
        if product_contrib == 0:
            continue
        inv = inv_fact[t + 1]
        contribution = (a_k * product_contrib) % MOD
        contribution = (contribution * inv) % MOD
        sum_total = (sum_total + contribution) % MOD
    
    print(sum_total % MOD)

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