結果

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

ソースコード

diff #

MOD = 10**9 + 7

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx +=1
    K = int(input[idx])
    idx +=1
    A = list(map(int, input[idx:idx+N]))
    
    max_m = 2 * 10**5  # since N can be up to 2e5, m_left = i-1 and m_right = N-i can each be up to 2e5-1

    # Precompute factorial and inverse factorial up to max_m
    fact = [1] * (max_m +1)
    for i in range(1, max_m +1):
        fact[i] = fact[i-1] * i % MOD
    inv_fact = [1] * (max_m +1)
    inv_fact[max_m] = pow(fact[max_m], MOD-2, MOD)
    for i in range(max_m -1, -1, -1):
        inv_fact[i] = inv_fact[i+1] * (i+1) % MOD

    # Precompute left_part = product_{j=1 to m} (K +j) mod MOD
    max_part = max_m  # enough for both left and right parts as m_left and m_right are up to N<=2e5
    left_part = [1] * (max_part +2)
    for m in range(1, max_part +1):
        term = (K % MOD + m) % MOD
        left_part[m] = left_part[m-1] * term % MOD

    # Compute the answer
    ans = 0
    for i in range(1, N+1):
        # Calculate left contribution
        m_left = i-1
        if m_left ==0:
            left = 1 % MOD
        elif m_left > max_part:
            left = 1
        else:
            if m_left <0:
                left = 1
            else:
                left = left_part[m_left] * inv_fact[m_left] % MOD
        
        # Calculate right contribution
        m_right = N -i
        if m_right ==0:
            right = 1 % MOD
        elif m_right > max_part:
            right =1
        else:
            if m_right <0:
                right =1
            else:
                right = left_part[m_right] * inv_fact[m_right] % MOD
        
        total = left * right % MOD
        ans = (ans + A[i-1] * total) % MOD
    print(ans % MOD)

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