Cnm%:
1 #include<stdio.h> 2 #include<string.h> 3 #include<vector> 4 using namespace std; 5 #define LL __int64 6 #define MOD 1000000007ll 7 const LL mod = 1000000007; 8 const LL N = 100000+5; 9 const LL M=1e5+3; 10 vector<LL >mp[100500]; 11 LL ans; 12 LL n,k; 13 LL vis[100500]; 14 LL fac[100005]; //阶乘 15 LL inv_of_fac[100005]; //阶乘的逆元 16 LL qpow(LL x,LL n) 17 { 18 LL ret=1; 19 for(; n; n>>=1) 20 { 21 if(n&1) ret=ret*x%mod; 22 x=x*x%mod; 23 } 24 return ret; 25 } 26 void init() 27 { 28 fac[1]=1; 29 for(int i=2; i<=M; i++) 30 fac[i]=fac[i-1]*i%mod; 31 inv_of_fac[M]=qpow(fac[M],mod-2); 32 for(int i=M-1; i>=0; i--) 33 inv_of_fac[i]=inv_of_fac[i+1]*(i+1)%mod; 34 } 35 LL C(LL a,LL b) 36 { 37 if(b>a) return 0; 38 if(b==0) return 1; 39 return fac[a]*inv_of_fac[b]%mod*inv_of_fac[a-b]%mod; 40 } 41 //(C(k,n)-C(k,cont)-C(k,n-cont)+MOD)%MOD; 42 LL Dfs(int u) 43 { 44 vis[u]=1; 45 LL cont=1; 46 for(LL int i=0;i<mp[u].size();i++) 47 { 48 LL v=mp[u][i]; 49 if(vis[v]==0) 50 { 51 LL tmp=Dfs(v); 52 ans=(ans+(C(n,k)%MOD-C(tmp,k)%MOD-C(n-tmp,k)%MOD)%MOD+MOD)%MOD; 53 cont+=tmp; 54 } 55 } 56 return cont; 57 } 58 int main() 59 { 60 while(~scanf("%I64d%I64d",&n,&k)) 61 { 62 init(); 63 memset(vis,0,sizeof(vis)); 64 for(LL i=1;i<=n;i++)mp[i].clear(); 65 for(LL i=1;i<=n-1;i++) 66 { 67 LL x,y; 68 scanf("%I64d%I64d",&x,&y); 69 mp[x].push_back(y); 70 mp[y].push_back(x); 71 } 72 ans=0; 73 Dfs(1); 74 printf("%I64d\n",(ans+MOD)%MOD); 75 } 76 //prLLf("%I64d\n",C(5,2)); 77 }
原文:http://www.cnblogs.com/ECJTUACM-873284962/p/6562249.html