Cnm%(个人模版)

时间:2022-12-08 10:07:25

Cnm%:

 #include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
#define LL __int64
#define MOD 1000000007ll
const LL mod = ;
const LL N = +;
const LL M=1e5+;
vector<LL >mp[];
LL ans;
LL n,k;
LL vis[];
LL fac[]; //阶乘
LL inv_of_fac[]; //阶乘的逆元
LL qpow(LL x,LL n)
{
LL ret=;
for(; n; n>>=)
{
if(n&) ret=ret*x%mod;
x=x*x%mod;
}
return ret;
}
void init()
{
fac[]=;
for(int i=; i<=M; i++)
fac[i]=fac[i-]*i%mod;
inv_of_fac[M]=qpow(fac[M],mod-);
for(int i=M-; i>=; i--)
inv_of_fac[i]=inv_of_fac[i+]*(i+)%mod;
}
LL C(LL a,LL b)
{
if(b>a) return ;
if(b==) return ;
return fac[a]*inv_of_fac[b]%mod*inv_of_fac[a-b]%mod;
}
//(C(k,n)-C(k,cont)-C(k,n-cont)+MOD)%MOD;
LL Dfs(int u)
{
vis[u]=;
LL cont=;
for(LL int i=;i<mp[u].size();i++)
{
LL v=mp[u][i];
if(vis[v]==)
{
LL tmp=Dfs(v);
ans=(ans+(C(n,k)%MOD-C(tmp,k)%MOD-C(n-tmp,k)%MOD)%MOD+MOD)%MOD;
cont+=tmp;
}
}
return cont;
}
int main()
{
while(~scanf("%I64d%I64d",&n,&k))
{
init();
memset(vis,,sizeof(vis));
for(LL i=;i<=n;i++)mp[i].clear();
for(LL i=;i<=n-;i++)
{
LL x,y;
scanf("%I64d%I64d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
ans=;
Dfs();
printf("%I64d\n",(ans+MOD)%MOD);
}
//prLLf("%I64d\n",C(5,2));
}