結果

問題 No.2791 Beginner Contest
ユーザー lam6er
提出日時 2025-03-20 20:30:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 754 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 82,552 KB
実行使用メモリ 63,228 KB
最終ジャッジ日時 2025-03-20 20:32:06
合計ジャッジ時間 1,956 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

N, K = map(int, input().split())

max_m = N // K

max_fact = N  # Maximum possible n is N when m=0, K-1=0 (if K=1)

# Precompute factorial and inverse factorial modulo MOD
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

ans = 0
for m in range(0, max_m + 1):
    n = N - m * (K - 1)
    if n < m or n < 0:
        continue
    # Calculate combination(n, m)
    numerator = fact[n]
    denominator = (inv_fact[m] * inv_fact[n - m]) % MOD
    comb = (numerator * denominator) % MOD
    ans = (ans + comb) % MOD

print(ans)
0