結果

問題 No.1102 Remnants
ユーザー lam6er
提出日時 2025-03-20 20:57:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 1,240 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 81,976 KB
実行使用メモリ 113,532 KB
最終ジャッジ日時 2025-03-20 20:57:52
合計ジャッジ時間 3,127 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

def main():
    import sys
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    K = int(input[ptr])
    ptr += 1
    A = list(map(int, input[ptr:ptr+N]))
    
    if K == 0:
        print(sum(A) % MOD)
        return
    
    max_m = N - 1
    if max_m < 0:
        max_m = 0
    product1 = [1] * (max_m + 1)
    for m in range(1, max_m + 1):
        product1[m] = product1[m - 1] * (K + m) % MOD
    
    # Compute factorial and inverse factorial up to max_m
    max_fact = max_m
    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
    
    total = 0
    for i in range(1, N + 1):
        a = i - 1
        term1 = product1[a] * inv_fact[a] % MOD if a >= 0 else 1
        
        b = N - i
        term2 = product1[b] * inv_fact[b] % MOD if b >= 0 else 1
        
        contribution = (term1 * term2) % MOD
        total = (total + A[i - 1] * contribution) % MOD
    
    print(total % MOD)

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