/ Randle /

记录详情

Wrong Answer


  
# 状态 耗时 内存占用
#1 Accepted 5ms 256.0 KiB
#2 Wrong Answer 3ms 256.0 KiB
#3 Wrong Answer 3ms 256.0 KiB
#4 Wrong Answer 10ms 472.0 KiB
#5 Wrong Answer 10ms 512.0 KiB
#6 Wrong Answer 10ms 476.0 KiB
#7 Wrong Answer 8ms 384.0 KiB
#8 Wrong Answer 14ms 348.0 KiB
#9 Wrong Answer 12ms 460.0 KiB
#10 Wrong Answer 10ms 500.0 KiB

代码

#include<bits/stdc++.h>
#define LL long long
const LL mod=1e9+7;
inline void read(LL &a)
{
	a=0;char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')
	{
		a=(a<<1)+(a<<3)+c-'0';
		c=getchar();
	}
}
inline void write(LL a)
{
	if(a<0){putchar('-');a=-a;}
	if(a>9)write(a/10);
	putchar(a%10+'0');
}
#define maxn 2001
LL ans=0,n,k,d=0,num[maxn],point[maxn],next[maxn],to[maxn],father[maxn],step[maxn];
inline void add(LL a,LL b)
{
	next[++d]=point[a];
	point[a]=d;
	to[d]=b;
}
inline LL fast_pow(LL a,LL b)
{
	LL base=a,ans=1;
	while(b)
	{
		if(b&1)ans=ans*base%mod;
		base=base*base%mod;
		b>>=1;
	}
	return ans;
}
inline LL dfs1(LL p)
{
	LL side=point[p];
	num[p]=1;
	while(side)
	{
		if(to[side]!=father[p])
		{
			father[to[side]]=p;
			num[p]+=dfs1(to[side]);
		}
		side=next[side];
	}
	return num[p];
}
inline LL C(LL m,LL n)
{
	if(m==n)return 1;
	return step[m]*fast_pow(step[n]*step[(m-n)]%mod,mod-2)%mod;
}
inline void dfs2(LL p)
{
	for(LL i=1;i<=k&&i<=num[p];i++)
		if(k-i<=n-num[p]&&k-i)
		{
			ans=(ans+C(num[p],i)*C(n-num[p],k-i)%mod)%mod;
			//printf("C(%lld,%lld)*C(%lld,%lld)=%lld\n",num[p],i,n-num[p],k-i,C(num[p],i)*C(n-num[p],k-i)%mod);
		}
	LL side=point[p];
	while(side)
	{
		if(to[side]!=father[p])dfs2(to[side]);
		side=next[side];
	}
}
int main()
{
	//freopen("1.in","r",stdin);
	read(n);read(k);
	LL st=1; 
	for(LL i=1;i<=n;i++)st=step[i]=i*st%mod;
	memset(num,0,sizeof(num));
	memset(point,0,sizeof(point));
	LL u,v;
	for(LL i=1;i<=n-1;i++){read(u);read(v);add(u,v);add(v,u);}
	dfs1(1);
	dfs2(1);
	write(ans);
	return 0;
}

信息

递交者
类型
递交
题目
月上树(原创)
题目数据
下载
语言
C++
递交时间
2017-11-07 15:38:06
评测时间
2017-11-07 15:38:08
评测机
分数
10
总耗时
89ms
峰值内存
512.0 KiB