您的位置:首页 > Web前端

bzoj 1430: 小猴打架 (prufer序列+数论)

2017-04-18 11:27 441 查看

题目描述

传送门

题目大意:N个点,每次可以连接两个不连通的点,N-1次后形成一棵树,问题有多少种不同的连接方式。

题解

首先确定有多少种不同形态的树。根据prufer序列,设有m个点的度数无限制,那么贡献是Cmn−2mn−2−tot

那么对于这道题来说,树的形态就是nn−2

因为n-1条边还存在连接的顺序,所以最后的答案是nn−2∗(n−1)!

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 1000003
#define p 9999991
#define LL long long
using namespace std;
int n;
LL quickpow(LL num,int x)
{
LL ans=1; LL base=num%p;
while (x) {
if (x&1) ans=ans*base%p;
x>>=1;
base=base*base%p;
}
return ans;
}
int main()
{
freopen("a.in","r",stdin);
scanf("%d",&n);
LL ans=1;
for (int i=1;i<=n-1;i++) ans=(LL)ans*i%p;
ans=ans*quickpow(n,n-2)%p;
printf("%lld\n",ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  prufer序列 数论