結果

問題 No.2048 L(I+D)S
ユーザー gew1fw
提出日時 2025-06-12 15:13:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 954 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 82,412 KB
実行使用メモリ 67,320 KB
最終ジャッジ日時 2025-06-12 15:14:03
合計ジャッジ時間 2,335 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

n = int(input())

if n < 2:
    print(0 if n <= 0 else 0)
else:
    # Precompute factorial and inverse factorial up to n
    max_n = n
    fact = [1] * (max_n + 1)
    for i in range(1, max_n + 1):
        fact[i] = fact[i-1] * i % MOD
    
    inv_fact = [1] * (max_n + 1)
    inv_fact[max_n] = pow(fact[max_n], MOD-2, MOD)
    for i in range(max_n - 1, -1, -1):
        inv_fact[i] = inv_fact[i+1] * (i+1) % MOD
    
    def comb(a, b):
        if b < 0 or b > a:
            return 0
        return fact[a] * inv_fact[b] % MOD * inv_fact[a - b] % MOD
    
    total = 0
    for k in range(2, n-1):
        c = comb(n-2, k-2)
        numerator = n * c % MOD
        numerator = numerator * (n - k - 1) % MOD
        denominator = k
        inv_denominator = pow(denominator, MOD-2, MOD)
        term = numerator * inv_denominator % MOD
        term_sq = (term * term) % MOD
        total = (total + term_sq) % MOD
    
    print(total)
0