結果
問題 |
No.2381 Gift Exchange Party
|
ユーザー |
![]() |
提出日時 | 2025-03-20 21:09:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 135 ms / 2,000 ms |
コード長 | 950 bytes |
コンパイル時間 | 198 ms |
コンパイル使用メモリ | 82,364 KB |
実行使用メモリ | 68,924 KB |
最終ジャッジ日時 | 2025-03-20 21:09:53 |
合計ジャッジ時間 | 2,292 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 22 |
ソースコード
MOD = 998244353 n, p = map(int, input().split()) if n == 0: print(0) exit() # Precompute factorial and inverse factorial 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 m_max = n // p # Precompute pow_p and inv_pow_p pow_p = [1] * (m_max + 1) for i in range(1, m_max + 1): pow_p[i] = pow_p[i-1] * p % MOD inv_pow_p = [1] * (m_max + 1) for i in range(0, m_max + 1): inv_pow_p[i] = pow(pow_p[i], MOD-2, MOD) if i != 0 else 1 total = 0 for m in range(0, m_max + 1): s = n - m * p if s < 0: continue term = fact[n] term = term * inv_fact[s] % MOD term = term * inv_fact[m] % MOD term = term * inv_pow_p[m] % MOD total = (total + term) % MOD ans = (fact[n] - total + MOD) % MOD print(ans)