結果
問題 | No.1489 Repeat Cumulative Sum |
ユーザー |
![]() |
提出日時 | 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 |
ソースコード
MOD = 10**9 + 7def main():import sysinput = sys.stdin.read().split()ptr = 0N = int(input[ptr])ptr += 1M = int(input[ptr])ptr += 1A = list(map(int, input[ptr:ptr + (N - 1)]))r = M % MODt_max = N - 2# Compute prefix_productsrequired_i = (MOD - r) % MODprefix_products = []current_product = 1for t in range(t_max + 1):if required_i <= t:current_product = 0else:term = (r + t) % MODcurrent_product = (current_product * term) % MODprefix_products.append(current_product)# Precompute factorial and inverse factorial up to t_max + 1max_fact = t_max + 1fact = [1] * (max_fact + 2)for i in range(1, max_fact + 1):fact[i] = (fact[i-1] * i) % MODinv_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)) % MODsum_total = 0for k in range(2, N + 1):idx = k - 2 # A's index for A_2, A_3, ..., A_Na_k = A[idx]t = N - kif t < 0 or t > t_max:continue # Shouldn't happen as k ranges from 2 to Nproduct_contrib = prefix_products[t]if product_contrib == 0:continueinv = inv_fact[t + 1]contribution = (a_k * product_contrib) % MODcontribution = (contribution * inv) % MODsum_total = (sum_total + contribution) % MODprint(sum_total % MOD)if __name__ == '__main__':main()