結果

問題 No.1414 東大文系数学2021第2問改
ユーザー lam6er
提出日時 2025-03-20 20:56:24
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,244 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 82,232 KB
実行使用メモリ 219,400 KB
最終ジャッジ日時 2025-03-20 20:56:46
合計ジャッジ時間 15,871 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

def main():
    import sys
    input = sys.stdin.read().split()
    N = int(input[0])
    M = int(input[1])
    K = int(input[2])
    
    if M == 0:
        print(1)
        return
    
    # Precompute factorial and inverse factorial up to max_needed
    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 a < 0 or b < 0 or a < b:
            return 0
        return fact[a] * inv_fact[b] % MOD * inv_fact[a - b] % MOD
    
    total = comb(N, M)
    n = N - M + 1
    res = 0
    max_i = M // K
    
    for i in range(0, max_i + 1):
        c_n_i = comb(n, i)
        rem = M - K * i
        if rem < 0:
            continue
        a = n + rem - 1
        b = rem
        c_a_b = comb(a, b)
        term = (pow(-1, i, MOD) * c_n_i) % MOD
        term = term * c_a_b % MOD
        res = (res + term) % MOD
    
    answer = (total - res) % MOD
    if answer < 0:
        answer += MOD
    print(answer)

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