NC13611 树
原题地址:
https://ac.nowcoder.com/acm/problem/13611
基本思路:
- 说是树然而和树没有太大关系,实际上是一个组合数学;
- 对于使用 i 种颜色将树染色,我们实际上是先将树分成 i 个联通块, 然后用从k种颜色中任取的 i 种颜色将这 i 个联通块按任意顺序染色;
- 将上面的文字转换为数学语言即是
- 所以综上我们要求的即是:
参考代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define int long long
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define pdd pair <double, double>
#define mp(a, b) make_pair(a, b)
#define INF 0x3f3f3f3f
inline int read() {
int x = 0, neg = 1; char op = getchar();
while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
return neg * x;
}
inline void print(int x) {
if (x < 0) { putchar('-'); x = -x; }
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
const int maxn = 500;
int fact[maxn];
inline int qsm(int x,int n,int mod) {
int res = 1;
while (n > 0) {
if (n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
int mod_comb(int n,int k,int mod) {
return fact[n] * qsm(fact[k] * fact[n - k] % mod, mod - 2, mod) % mod;
}
const int mod = 1e9 + 7;
int n,k,u,v;
signed main() {
IO;
fact[0] = 1, fact[1] = 1;
for (int i = 2; i < maxn; i++) fact[i] = fact[i - 1] * i % mod;
cin >> n >> k;
rep(i, 1, n - 1) cin >> u >> v;
int ans = 0;
rep(i, 1, min(n, k)) ans += mod_comb(n - 1, i - 1, mod) * mod_comb(k, i, mod) % mod * fact[i] % mod;
cout << ans % mod << endl;
return 0;
}