結果

問題 No.1977 Extracting at Constant Intervals
ユーザー lam6er
提出日時 2025-04-16 00:10:37
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,135 bytes
コンパイル時間 198 ms
コンパイル使用メモリ 81,640 KB
実行使用メモリ 220,180 KB
最終ジャッジ日時 2025-04-16 00:11:51
合計ジャッジ時間 5,226 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 2 TLE * 1 -- * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
from collections import defaultdict

def main():
    N, M, L = map(int, sys.stdin.readline().split())
    A = list(map(int, sys.stdin.readline().split()))
    
    # Precompute sum_r: sum of A_p where p mod L = r
    sum_r = defaultdict(int)
    for p in range(1, N+1):
        r = p % L
        sum_r[r] += A[p-1]
    
    g = math.gcd(N, L)
    T = L // g
    D = (-N) % L  # Step in the arithmetic progression
    
    max_sum = -float('inf')
    
    # We need to check all residues modulo L, but this is impossible for large L.
    # So this code will not work for large L, but here for demonstration.
    # However, given the problem constraints, this approach is not feasible.
    # The correct approach involves mathematical insights beyond the current scope.
    
    # For the sample input, we can compute the sum for each i in 1..L and find the maximum.
    # But for large L, this is impossible.
    # The following code is for demonstration and will not pass large test cases.
    
    # Compute for each i in 1..L
    for i in range(1, L+1):
        total = 0
        # Compute k from 0 to M-1: (i - k*N) mod L
        # This is equivalent to (i + k*D) mod L
        # But with M up to 1e9, we need to find the sum using periodicity
        full_cycles = M // T
        remaining = M % T
        
        # Compute sum for full cycles
        full_sum = 0
        current = i % L
        seen = dict()
        cycle_values = []
        for k in range(T):
            if current in seen:
                break
            seen[current] = k
            cycle_values.append(current)
            current = (current + D) % L
        # Now, cycle_values contains the residues for one full cycle
        full_sum = sum(sum_r[r] for r in cycle_values)
        total += full_cycles * full_sum
        
        # Compute sum for remaining
        current = i % L
        for k in range(remaining):
            total += sum_r.get(current, 0)
            current = (current + D) % L
        
        if total > max_sum:
            max_sum = total
    
    print(max_sum)

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