結果

問題 No.1414 東大文系数学2021第2問改
ユーザー lam6er
提出日時 2025-04-09 21:05:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 1,151 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 82,252 KB
実行使用メモリ 219,932 KB
最終ジャッジ日時 2025-04-09 21:07:55
合計ジャッジ時間 5,435 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

def main():
    import sys
    N, M, K = map(int, sys.stdin.readline().split())
    if K > M:
        print(0)
        return
    if K == M:
        ans = (N - M + 1) % MOD
        print(ans)
        return

    max_fact = N
    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

    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

    c = N - M + 1
    s_max = min(c, M // K)
    sum_terms = 0

    for s in range(s_max + 1):
        remain = M - K * s
        if remain < 0:
            continue
        a = N - K * s
        current_comb = comb(c, s) * comb(a, remain) % MOD
        if s % 2 == 1:
            current_comb = (-current_comb) % MOD
        sum_terms = (sum_terms + current_comb) % MOD

    total = (comb(N, M) - sum_terms) % MOD
    print(total)

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