結果

問題 No.1667 Forest
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-10-10 18:08:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,700 ms / 3,000 ms
コード長 1,106 bytes
コンパイル時間 1,838 ms
コンパイル使用メモリ 87,076 KB
実行使用メモリ 96,156 KB
最終ジャッジ日時 2023-10-12 14:55:07
合計ジャッジ時間 14,532 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,700 ms
96,156 KB
testcase_01 AC 1,687 ms
95,836 KB
testcase_02 AC 1,642 ms
95,048 KB
testcase_03 AC 109 ms
77,320 KB
testcase_04 AC 1,555 ms
94,888 KB
testcase_05 AC 912 ms
84,884 KB
testcase_06 AC 597 ms
80,948 KB
testcase_07 AC 384 ms
78,944 KB
testcase_08 AC 235 ms
78,116 KB
testcase_09 AC 176 ms
78,116 KB
testcase_10 AC 136 ms
78,056 KB
testcase_11 AC 104 ms
77,284 KB
testcase_12 AC 73 ms
71,284 KB
testcase_13 AC 73 ms
71,264 KB
testcase_14 AC 74 ms
71,272 KB
testcase_15 AC 72 ms
71,288 KB
testcase_16 AC 72 ms
71,308 KB
testcase_17 AC 71 ms
71,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Combinatorics:
	def __init__(self, n: int) -> None:
		self.n = n
		self.fa = [1] * (self.n * 2 + 1)
		self.fi = [1] * (self.n * 2 + 1)

		for i in range(1, self.n * 2 + 1):
			self.fa[i] = self.fa[i - 1] * i % mod

		self.fi[-1] = pow(self.fa[-1], mod - 2, mod)

		for i in range(self.n * 2, 0, -1):
			self.fi[i - 1] = self.fi[i] * i % mod

	def comb(self, n: int, r: int) -> int:
		if n < r:return 0
		if n < 0 or r < 0:return 0
		return self.fa[n] * self.fi[r] % mod * self.fi[n - r] % mod

	def perm(self, n: int, r: int) -> int:
		if n < r:return 0
		if n < 0 or r < 0:return 0
		return self.fa[n] * self.fi[n - r] % mod
		
	def combr(self, n: int, r: int) -> int:
		if n == r == 0:return 1
		return self.comb(n + r - 1, r)

n, mod = map(int, input().split())
dp = [[0] * n for i in range(n + 1)]
C = Combinatorics(300)
dp[0][0] = 1
for i in range(n):
	for j in range(i + 1):
		dp[i + 1][j] += dp[i][j]
		dp[i + 1][j] %= mod
		for k in range(2, n - i + 1):
			dp[i + k][j + k - 1] += dp[i][j] * C.comb(n - i - 1, k - 1) * pow(k, k - 2, mod)
			dp[i + k][j + k - 1] %= mod
print(*dp[n], sep="\n")
0