結果

問題 No.2005 Sum of Power Sums
ユーザー lam6er
提出日時 2025-04-15 23:54:32
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,982 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 82,204 KB
実行使用メモリ 295,912 KB
最終ジャッジ日時 2025-04-15 23:55:43
合計ジャッジ時間 10,312 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 TLE * 1 -- * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    idx = 0
    N = int(data[idx])
    idx += 1
    M = int(data[idx])
    idx += 1
    K_list = list(map(int, data[idx:idx+N]))
    idx += N

    max_stirling = 5000
    # Precompute Stirling numbers of the second kind
    stirling = [[0] * (max_stirling + 1) for _ in range(max_stirling + 1)]
    stirling[0][0] = 1
    for n in range(1, max_stirling + 1):
        for j in range(1, n + 1):
            stirling[n][j] = (j * stirling[n-1][j] + stirling[n-1][j-1]) % MOD

    max_fact = 205001  # T + K_i +1 <= 2e5 +5000 +1 =205001
    # Precompute factorials and inverse factorials
    fact = [1] * (max_fact + 1)
    for i in range(1, max_fact + 1):
        fact[i] = fact[i-1] * i % MOD

    inv_fact = [1] * (max_fact + 1)
    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) % MOD

    T = N - 1
    max_K = max(K_list) if K_list else 0
    max_k = T + max_K + 1
    # Ensure max_k does not exceed precomputed max_fact
    if max_k > max_fact:
        max_k = max_fact

    n = M + T + 1
    n_mod = n % MOD

    # Precompute P array
    P = [1] * (max_k + 1)
    for k in range(1, max_k + 1):
        term = (n_mod - (k - 1)) % MOD
        P[k] = (P[k-1] * term) % MOD

    total = 0
    for K in K_list:
        current_sum = 0
        for j in range(0, K + 1):
            k = T + j + 1
            if k > max_k:
                continue
            s = stirling[K][j] if K <= max_stirling else 0
            fj = fact[j] if j <= max_fact else 0
            if k > max_fact:
                c = 0
            else:
                c = P[k] * inv_fact[k] % MOD
            term = s * fj % MOD
            term = term * c % MOD
            current_sum = (current_sum + term) % MOD
        total = (total + current_sum) % MOD

    print(total % MOD)

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