結果

問題 No.1956 猫の額
ユーザー gew1fw
提出日時 2025-06-12 12:57:28
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,256 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 82,792 KB
実行使用メモリ 285,612 KB
最終ジャッジ日時 2025-06-12 13:04:08
合計ジャッジ時間 9,392 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other MLE * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    N, M, C = map(int, sys.stdin.readline().split())
    A = list(map(int, sys.stdin.readline().split()))
    total_sum = sum(A)
    
    # Initialize DP: dp[j] is a dictionary where keys are sums and values are counts mod M
    dp = [defaultdict(int) for _ in range(C + 1)]
    dp[0][0] = 1  # Base case: 0 elements selected, sum 0
    
    for a in A:
        # Process in reverse to avoid overwriting the current state
        for j in range(C - 1, -1, -1):
            current_dict = dp[j]
            next_dict = dp[j + 1]
            # Iterate over a copy of the keys to avoid modifying the dict during iteration
            for k in list(current_dict.keys()):
                count = current_dict[k]
                new_sum = k + a
                next_dict[new_sum] = (next_dict[new_sum] + count) % M
    
    # Prepare the result for sums from 1 to total_sum
    result = [0] * (total_sum + 1)
    for s, cnt in dp[C].items():
        if 1 <= s <= total_sum:
            result[s] = cnt % M
    
    # Generate the output string
    output = []
    for s in range(1, total_sum + 1):
        output.append(str(result[s]))
    print(' '.join(output))

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