题目
[CodeForces 1109D] Sasha and Interesting Fact from Graph Theory
分析
不影响答案,不妨设 ,枚举 之间的边数 ,隔板法可得链 的方案数为 (中间的点编号可以任意取,所以是 )然后考虑剩下的点和边,首先边权随意: 然后考虑剩下的点,这个树是这个样子的:
因此接下来我们只需要求 个点构成一个的含有 棵树的森林的数量。根据广义 Cayley 定理, 个点构成的含有 棵树的森林数量为 。因此 个点构成一个的含有 棵树的森林的数量即为 。
证明:
将链 缩为一个点,那么这个树的数量为 ,再考虑把上图中的三角形轮换一圈(因为 算的是各个三角形的圆排列,现在把它转换成一般排列),答案即为 。
因此答案为
代码
#include <bits/stdc++.h>
const int MAXN = 1000000;
const int MOD = 1000000007;
int N, M, A1, B1;
int Fac[MAXN + 5], Inv[MAXN + 5];
int PowN[MAXN + 5], PowM[MAXN + 5];
inline int Add(int x, const int &y) {
x += y; if (x >= MOD) x -= MOD; return x;
}
inline int Mul(const int &x, const int &y) {
return (long long)x * y % MOD;
}
int Pow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1)
ret = Mul(ret, x);
x = Mul(x, x);
y >>= 1;
}
return ret;
}
int A(int n, int m) {
return Mul(Fac[n], Inv[n - m]);
}
int C(int n, int m) {
if (m > n) return 0;
return Mul(Fac[n], Mul(Inv[m], Inv[n - m]));
}
int main() {
scanf("%d%d%d%d", &N, &M, &A1, &B1);
int Max = std::max(N, M);
Fac[0] = PowN[0] = PowM[0] = 1;
for (int i = 1; i <= Max; i++) {
Fac[i] = Mul(Fac[i - 1], i);
PowN[i] = Mul(PowN[i - 1], N);
PowM[i] = Mul(PowM[i - 1], M);
}
Inv[Max] = Pow(Fac[Max], MOD - 2);
for (int i = Max - 1; i >= 0; i--)
Inv[i] = Mul(Inv[i + 1], i + 1);
int Ans = Mul(C(M - 1, N - 2), A(N - 2, N - 2));
for (int i = 1; i <= N - 2; i++)
Ans = Add(Ans, Mul(A(N - 2, i - 1), Mul(C(M - 1, i - 1), Mul(i + 1, Mul(PowN[N - i - 2], PowM[N - 1 - i])))));
printf("%d", Ans);
return 0;
}