https://ac.nowcoder.com/acm/problem/13611
其实把,把树分成k个连通块有几种分配方法= ====== 从树上取下k-1条边有几种取法,排列组合就行了,,,,,,
#include<iostream> #include<algorithm> using namespace std; typedef long long ll; const ll mod = 1e9+7; //(a/c) % p == a * c^(p-2) % p ll k_q(ll a,ll b){ ll res = 1; while(b){ if(b&1){ res = (res*a)%mod; } b>>=1; a = (a*a)%mod; } return res%mod; } ll list[20000]; ll cns[20000]; ll C(ll n,ll m){ ll ans = (((list[n]*cns[n-m])%mod)*cns[m])%mod; return ans; } int main(){ list[0]=1; cns[0]=1; for(int i = 1;i<=2000;i++){ list[i] = (list[i-1]*i)%mod; cns[i] = k_q(list[i],mod-2)%mod; } int n,k; cin>>n>>k; list[0] = 1; int be,en; for(int i=1;i<n;i++){ cin>>be>>en; } ll ans = 0; for(int i=1;i<=min(n,k);i++){ ll c = (((C(n-1,i-1)*C(k,i))%mod )*list[i])%mod; ans = (ans+c)%mod; } cout<<ans<<endl; return 0; }