BZOJ 1430 小猴打架

可以用Prüfer编码与Cayley公式(orz sxy大佬)证;

也可以用矩阵树定理(虽然我不会证)证;

然后因为顺序有关,所以再乘上!(n-1);

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
typedef long long LL;
const LL mod=9999991;
using namespace std;
int n;
LL ans=1;
LL ksm(LL a,LL b) {
    LL res=1,base=a;
    while(b) {
        if(b&1) (res*=base)%=mod;
        (base*=base)%=mod;
        b>>=1;
    }
    return res;
}
int main()
{
    scanf("%d",&n);
    for(int i=2;i<n;i++) (ans*=i)%=mod;
    printf("%lld
",(ans*ksm(n,n-2))%mod);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Achenchen/p/7563014.html