結果
問題 | No.2528 pop_(backfront or not) |
ユーザー |
|
提出日時 | 2024-12-27 23:57:54 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 482 ms / 2,000 ms |
コード長 | 2,370 bytes |
コンパイル時間 | 375 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 76,452 KB |
最終ジャッジ日時 | 2024-12-27 23:58:00 |
合計ジャッジ時間 | 4,851 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 19 |
ソースコード
## https://yukicoder.me/problems/no/1766MOD = 998244353class CombinationCalculator:"""modを考慮したPermutation, Combinationを計算するためのクラス"""def __init__(self, size, mod):self.mod = modself.factorial = [0] * (size + 1)self.factorial[0] = 1for i in range(1, size + 1):self.factorial[i] = (i * self.factorial[i - 1]) % self.modself.inv_factorial = [0] * (size + 1)self.inv_factorial[size] = pow(self.factorial[size], self.mod - 2, self.mod)for i in reversed(range(size)):self.inv_factorial[i] = ((i + 1) * self.inv_factorial[i + 1]) % self.moddef calc_combination(self, n, r):if n < 0 or n < r or r < 0:return 0if r == 0 or n == r:return 1ans = self.inv_factorial[n - r] * self.inv_factorial[r]ans %= self.modans *= self.factorial[n]ans %= self.modreturn ansdef calc_permutation(self, n, r):if n < 0 or n < r:return 0ans = self.inv_factorial[n - r]ans *= self.factorial[n]ans %= self.modreturn ansdef main():N = int(input())combi = CombinationCalculator(2 * N + 1, MOD)# n = 1dp = [0, 1, 0]for n in range(1, N):new_dp = [0] * (2 * (n + 1) + 1)# pop upfor j in range(1, 2 * (n + 1)):new_dp[j] += dp[j - 1]new_dp[j] %= MOD# 任意に選ぶfor j in range(2 * (n + 1) + 1):## 左2つ選ぶif j > 2:new_dp[j] += combi.calc_combination(j - 1, 2) * dp[j - 2]new_dp[j] %= MOD## 左右1つずつ選ぶif j > 1 and j < 2 * (n + 1) - 1:a = ((j - 1) * (2 * (n + 1) - 1 - j)) % MODa *= dp[j - 1]a %= MODnew_dp[j] += anew_dp[j ] %=MOD## 右を2つ選ぶif j < 2 * (n + 1) - 2:x = 2 * (n + 1) - j - 1new_dp[j] += combi.calc_combination(x, 2) * dp[j]new_dp[j] %= MODdp = new_dpfor i in range(2 * N + 1):print(dp[i])if __name__ == '__main__':main()