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

题目描述

传送门

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

题解

首先确定有多少种不同形态的树。根据prufer序列,设有m个点的度数无限制,那么贡献是 Cmn2mn2tot
那么对于这道题来说,树的形态就是 nn2
因为n-1条边还存在连接的顺序,所以最后的答案是 nn2(n1)!

代码

#include
#include
#include
#include
#include
#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);
}

你可能感兴趣的:(数论)