C - 連続区間の積の和 / Sum of Products of Consecutive Intervals Editorial by admin Editorial for printing
Claude 4.5 OpusOverview
This problem asks us to calculate the sum of products for all ways of selecting \(K\) consecutive elements from a sequence. We solve it efficiently using a sliding window and modular inverse.
Analysis
Issues with the Naive Approach
A straightforward approach would calculate the product of \(K\) elements for each of the \(N - K + 1\) possible selections. However, with this method: - Number of selections: \(O(N - K + 1)\) - Calculation of each product: \(O(K)\)
The total is \(O((N - K + 1) \times K)\), which becomes \(O(N^2)\) in the worst case (\(K \approx N/2\)) and results in TLE.
Key Insight
When calculating products of consecutive intervals in order, adjacent intervals share \(K - 1\) elements.
For example, when \(N = 5, K = 3\): - Interval 1: \(A_1 \times A_2 \times A_3\) - Interval 2: \(A_2 \times A_3 \times A_4\) - Interval 3: \(A_3 \times A_4 \times A_5\)
When moving from interval 1 to interval 2, only \(A_1\) is removed and \(A_4\) is added. In other words: $\(\text{Product of interval 2} = \frac{\text{Product of interval 1}}{A_1} \times A_4\)$
The Division Problem
When working with remainders modulo \(10^9 + 7\), we cannot use ordinary division. Instead, we use the modular inverse.
Dividing by \(a\) is equivalent to multiplying by \(a^{-1}\) (the modular inverse of \(a\)). By Fermat’s Little Theorem, when \(p\) is prime: $\(a^{-1} \equiv a^{p-2} \pmod{p}\)$
Algorithm
Initialization: Calculate the product of the first \(K\) elements (\(A_1, A_2, \ldots, A_K\)) and store it as
product. Addproducttototal.Sliding Window: Repeat the following for \(i = 0, 1, \ldots, N - K - 1\):
- Calculate the modular inverse of \(A_{i+1}\) (which is \(A[i]\) in 0-indexed)
- “Divide”
productby \(A[i]\) and multiply by \(A[i+K]\) - Add the new
producttototal
Output Result: Print
total
[A0, A1, A2, A3, A4] (Example with N=5, K=3)
└──────┘ First product: A0×A1×A2
└──────┘ Next product: (A0×A1×A2) ÷ A0 × A3 = A1×A2×A3
└──────┘ Next product: (A1×A2×A3) ÷ A1 × A4 = A2×A3×A4
Complexity
Time Complexity: \(O(N \log MOD)\)
- Initial product calculation: \(O(K)\)
- Sliding window: \(O(N - K)\) loop iterations, each with modular inverse calculation (using fast exponentiation) in \(O(\log MOD)\)
- Total: \(O(K + (N - K) \log MOD) = O(N \log MOD)\)
Space Complexity: \(O(N)\)
- Storing array \(A\): \(O(N)\)
Implementation Notes
Modular Inverse Calculation: In Python,
pow(a, MOD - 2, MOD)efficiently computes \(a^{MOD-2} \mod MOD\).Overflow Prevention: By taking
% MODafter each calculation, we prevent values from becoming too large.Be Careful with 0-indexed vs 1-indexed: The code uses 0-indexed while the problem statement uses 1-indexed, so be careful not to confuse the correspondence.
Source Code
def solve():
MOD = 10**9 + 7
N, K = map(int, input().split())
A = list(map(int, input().split()))
# Calculate the product of the first K elements
product = 1
for i in range(K):
product = (product * A[i]) % MOD
total = product
# Calculate the remaining products using sliding window
# Remove A[i] and add A[i+K]
for i in range(N - K):
# Divide by A[i] and multiply by A[i+K]
# Division uses modular inverse
inv_ai = pow(A[i], MOD - 2, MOD)
product = (product * inv_ai) % MOD
product = (product * A[i + K]) % MOD
total = (total + product) % MOD
print(total)
solve()
This editorial was generated by claude4.5opus.
posted:
last update: