LOJ#6044. 「雅礼集训 2017 Day8」共
题面
题解
显然树是二分图。所以问题很容易地变成了:限制和 \(1\) 一边的点数为 \(K\) 的二分图生成树个数。(但是我并没有想出来这一步
首先求出限制和 \(1\) 一边的点数为 \(K\) 的二分图个数,为 \(\large\binom {N - 1} {K - 1}\)。
那么只需求出像那个样子的生成树个数即可。
矩阵树定理
写出该图的基尔霍夫矩阵消去最后一行一列的行列式:
\[\left|\begin{matrix}
N - K & 0 & \cdots & 0 & -1 & -1 & \cdots & -1\\
0 & N - K & \cdots & 0 & -1 & -1 & \cdots & -1\\
\vdots & \vdots & \ddots & \vdots & \vdots & \vdots & \vdots & \vdots \\
0 & 0 & \cdots & N - K & -1 & -1 & \cdots & -1\\
-1 & -1 & \cdots & -1 & K & 0 & \cdots & 0\\
-1 & -1 & \cdots & -1 & 0 & K & \cdots & 0\\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\
-1 & -1 & \cdots & -1 & 0 & 0 & \cdots & K
\end{matrix}\right|
\]
其中有 \(K\) 个 \(N - K\) 和 \(N - K - 1\) 个 \(K\)。
首先消下面一半,用第 \(i\) 行减第 \(i + 1\) 行,得
\[\left|\begin{matrix}
N - K & 0 & \cdots & 0 & -1 & -1 & -1 & \cdots & -1\\
0 & N - K & \cdots & 0 & -1 & -1 & -1 & \cdots & -1\\
\vdots & \vdots & \ddots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\
0 & 0 & \cdots & N - K & -1 & -1 & -1 & \cdots & -1\\
0 & 0 & \cdots & 0 & K & -K & 0 & \cdots & 0\\
0 & 0 & \cdots & 0 & 0 & K & -K & \cdots & 0\\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\
-1 & -1 & \cdots & -1 & 0 & 0 & 0 & \cdots & K
\end{matrix}\right|
\]
将最后一行乘以 \(N - K\),得:
\[\frac 1{N - K}\left|\begin{matrix}
N - K & 0 & \cdots & 0 & -1 & -1 & -1 & \cdots & -1\\
0 & N - K & \cdots & 0 & -1 & -1 & -1 & \cdots & -1\\
\vdots & \vdots & \ddots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\
0 & 0 & \cdots & N - K & -1 & -1 & -1 & \cdots & -1\\
0 & 0 & \cdots & 0 & K & -K & 0 & \cdots & 0\\
0 & 0 & \cdots & 0 & 0 & K & -K & \cdots & 0\\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\
K - N & K - N & \cdots & K - N & 0 & 0 & 0 & \cdots & K(N - K)
\end{matrix}\right|
\]
将第 \(1, 2, \cdots k\) 行加到最后一行,于是最后一行变为:
\[\begin{bmatrix}
0 & 0 & \cdots & 0 & -K & -K & \cdots & -K & K(N - K - 1)
\end{bmatrix}
\]
然后将第 \(k + 1\) 行加到最后一行,得到
\[\begin{bmatrix}
0 & 0 & \cdots & 0 & 0 & -2K & -K & \cdots & K(N - K - 1)
\end{bmatrix}
\]
同理将第 \(k + i\) 行 \(\times i\) 加到最后一行,可得
\[\begin{bmatrix}
0 & 0 & \cdots & (K(N - K - 1) - K(N - K - 2))
\end{bmatrix}
\]
即所求变为
\[\frac 1{N - K}\left|\begin{matrix}
N - K & 0 & \cdots & 0 & -1 & -1 & -1 & \cdots & -1\\
0 & N - K & \cdots & 0 & -1 & -1 & -1 & \cdots & -1\\
\vdots & \vdots & \ddots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\
0 & 0 & \cdots & N - K & -1 & -1 & -1 & \cdots & -1\\
0 & 0 & \cdots & 0 & K & -K & 0 & \cdots & 0\\
0 & 0 & \cdots & 0 & 0 & K & -K & \cdots & 0\\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\
0 & 0 & \cdots & 0 & 0 & 0 & 0 & \cdots & K
\end{matrix}\right|
\]
那么就是
\[\frac 1{N - K} (N - K)^K K^{N - K - 1} = (N - K)^{K - 1}K^{N - K - 1}
\]
所以答案就是
\[\binom{N - 1}{K - 1} (N - K)^{K - 1}K^{N - K - 1}
\]
直接计算即可。
Prufer 序列
等一发神仙 hyj 的 blog(
代码
#include <cstdio>
#include <algorithm>
#include <vector>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int N(5e5 + 10);
int n, K, Mod, fac[N], inv[N];
int C(int n, int m) { return 1ll * fac[n] * inv[m] % Mod * inv[n - m] % Mod; }
int fastpow(int x, int y)
{
int ans = 1;
for (; y; y >>= 1, x = 1ll * x * x % Mod)
if (y & 1) ans = 1ll * ans * x % Mod;
return ans;
}
int main()
{
n = read(), K = read(), Mod = read(), fac[0] = inv[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % Mod;
inv[n] = fastpow(fac[n], Mod - 2);
for (int i = n - 1; i; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % Mod;
printf("%lld\n", 1ll * C(n - 1, K - 1) * fastpow(K, n - K - 1) % Mod * fastpow(n - K, K - 1) % Mod);
return 0;
}