# LOJ#6044. 「雅礼集训 2017 Day8」共

### 题解

#### 矩阵树定理

$\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|$

$\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|$

$\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|$

$\begin{bmatrix} 0 & 0 & \cdots & 0 & -K & -K & \cdots & -K & K(N - K - 1) \end{bmatrix}$

$\begin{bmatrix} 0 & 0 & \cdots & 0 & 0 & -2K & -K & \cdots & K(N - K - 1) \end{bmatrix}$

$\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}$

### 代码

#include <cstdio>
#include <algorithm>
#include <vector>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)

{
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()
{