結果

問題 No.2005 Sum of Power Sums
ユーザー gew1fw
提出日時 2025-06-12 19:03:28
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,695 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 82,908 KB
実行使用メモリ 321,108 KB
最終ジャッジ日時 2025-06-12 19:03:48
合計ジャッジ時間 9,829 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 TLE * 1 -- * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353
max_k = 5000
max_r = 205000  # 2e5 + 5e3 = 205000

# Precompute factorials and inverse factorials
fact = [1] * (max_r + 1)
for i in range(1, max_r + 1):
    fact[i] = fact[i-1] * i % MOD

inv_fact = [1] * (max_r + 1)
inv_fact[max_r] = pow(fact[max_r], MOD-2, MOD)
for i in range(max_r - 1, -1, -1):
    inv_fact[i] = inv_fact[i+1] * (i + 1) % MOD

# Precompute Stirling numbers of the second kind
stirling = [[0] * (max_k + 1) for _ in range(max_k + 1)]
stirling[0][0] = 1
for k in range(1, max_k + 1):
    for m in range(1, k + 1):
        stirling[k][m] = (m * stirling[k-1][m] + stirling[k-1][m-1]) % MOD

def main():
    import sys
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    M = int(input[ptr])
    ptr += 1
    K_list = list(map(int, input[ptr:ptr+N]))
    ptr += N
    
    n_mod = (M + N) % MOD
    R_max = N + 5000
    if R_max > max_r:
        R_max = max_r
    
    # Precompute falling factorial for this test case
    falling = [0] * (R_max + 1)
    falling[0] = 1
    for r in range(1, R_max + 1):
        term = (n_mod - (r - 1)) % MOD
        falling[r] = falling[r-1] * term % MOD
    
    ans = 0
    for K in K_list:
        current_sum = 0
        for m in range(0, K + 1):
            s = stirling[K][m]
            if s == 0:
                continue
            term = s * fact[m] % MOD
            r = N + m
            if r > R_max:
                continue
            if r < 0:
                continue
            c = falling[r] * inv_fact[r] % MOD
            current_sum = (current_sum + term * c) % MOD
        ans = (ans + current_sum) % MOD
    print(ans % MOD)

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