結果

問題 No.1667 Forest
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-09-03 22:26:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,688 ms / 3,000 ms
コード長 1,629 bytes
コンパイル時間 376 ms
コンパイル使用メモリ 87,184 KB
実行使用メモリ 78,144 KB
最終ジャッジ日時 2023-08-21 22:53:35
合計ジャッジ時間 12,994 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,688 ms
77,620 KB
testcase_01 AC 1,658 ms
78,016 KB
testcase_02 AC 1,612 ms
77,780 KB
testcase_03 AC 108 ms
77,400 KB
testcase_04 AC 1,639 ms
78,144 KB
testcase_05 AC 992 ms
77,668 KB
testcase_06 AC 658 ms
77,644 KB
testcase_07 AC 433 ms
77,552 KB
testcase_08 AC 259 ms
77,504 KB
testcase_09 AC 195 ms
77,484 KB
testcase_10 AC 136 ms
77,480 KB
testcase_11 AC 97 ms
76,988 KB
testcase_12 AC 72 ms
71,424 KB
testcase_13 AC 70 ms
71,328 KB
testcase_14 AC 73 ms
71,088 KB
testcase_15 AC 72 ms
71,296 KB
testcase_16 AC 70 ms
71,152 KB
testcase_17 AC 72 ms
71,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""




1667:
M辺なので、連結成分の数は固定
n頂点の木の個数は、n**(n-2)
どの頂点をどこに入れるか
を考えなくてはいけない

f(x) = x**(x-2) / fac(x)
として推移させればおk?

dp[x][y] = x頂点を、y個の木にする場合の数
y = 1なら
x**(x-2) である。

そうでないとき、
この中で、最小の数字が含まれる木だけ構築することにする。
k頂点の木の作り方は、
k**(k-2) * nCr(x-1,k-1) である。
これに、dp[x-k][y-1] を掛ければおk

"""

import math
import sys
from sys import stdin

def modfac(n, MOD):
 
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
    return factorials, invs

def modnCr(n,r,mod,fac,inv):
    return fac[n] * inv[n-r] * inv[r] % mod


N,mod = map(int,stdin.readline().split())
fac,inv = modfac(2*N+100,mod)

tree = [1,1,1]
for i in range(3,400):
    tree.append( pow(i,i-2,mod) )

dp = [[0] * (N+1) for i in range(N+1)]
dp[0][0] = 1

for x in range(1,N+1):
    for y in range(1,N+1):

        if x < y:
            now = 0
        elif x == y:
            now = 1
        elif y == 1:
            now = tree[x]
        else:
            now = 0
            for k in range(1,x):
                now += tree[k] * modnCr(x-1,k-1,mod,fac,inv) * dp[x-k][y-1]
                now %= mod

        dp[x][y] = now % mod

for M in range(N):

    print (dp[N][N-M])
0