結果

問題 No.1667 Forest
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-09-03 22:26:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,602 ms / 3,000 ms
コード長 1,629 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 82,160 KB
実行使用メモリ 77,488 KB
最終ジャッジ日時 2024-05-09 05:00:05
合計ジャッジ時間 11,686 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,602 ms
77,184 KB
testcase_01 AC 1,600 ms
77,488 KB
testcase_02 AC 1,546 ms
77,156 KB
testcase_03 AC 81 ms
76,400 KB
testcase_04 AC 1,593 ms
76,908 KB
testcase_05 AC 936 ms
77,020 KB
testcase_06 AC 604 ms
76,852 KB
testcase_07 AC 398 ms
76,624 KB
testcase_08 AC 224 ms
76,672 KB
testcase_09 AC 164 ms
77,100 KB
testcase_10 AC 103 ms
76,800 KB
testcase_11 AC 68 ms
72,960 KB
testcase_12 AC 39 ms
52,396 KB
testcase_13 AC 39 ms
52,608 KB
testcase_14 AC 40 ms
52,736 KB
testcase_15 AC 40 ms
52,480 KB
testcase_16 AC 40 ms
52,480 KB
testcase_17 AC 39 ms
52,736 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