【BZOJ 1430】 1430: 小猴打架 (Prufer数列)

2023-05-06,,

1430: 小猴打架

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 625  Solved: 452

Description

一开始森林里面有N只互不相识的小猴子,它们经常打架,但打架的双方都必须不是好朋友。每次打完架后,打架的双方以及它们的好朋友就会互相认识,成为好朋友。经过N-1次打架之后,整个森林的小猴都会成为好朋友。 现在的问题是,总共有多少种不同的打架过程。 比如当N=3时,就有{1-2,1-3}{1-2,2-3}{1-3,1-2}{1-3,2-3}{2-3,1-2}{2-3,1-3}六种不同的打架过程。

Input

一个整数N。

Output

一行,方案数mod 9999991。

Sample Input

4

Sample Output

96

HINT

50%的数据N<=10^3。
100%的数据N<=10^6。

Source

【分析】

  prufer的解释在上一题。

  答案显然为$(n-2)^{n}*(n-1)!$

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Mod 9999991
#define LL long long int qpow(int x,int b)
{
int ans=;
while(b)
{
if(b&) ans=1LL*ans*x%Mod;
x=1LL*x*x%Mod;
b>>=;
}
return ans;
} int main()
{
int n;
scanf("%d",&n);
int ans=qpow(n,n-);
for(int i=;i<=n-;i++) ans=1LL*ans*i%Mod;
printf("%d\n",ans);
return ;
}

2017-04-25 14:57:48

BZOJ 1430】 1430: 小猴打架 (Prufer数列)的相关教程结束。

《【BZOJ 1430】 1430: 小猴打架 (Prufer数列).doc》

下载本文的Word格式文档,以方便收藏与打印。