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

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)!$

 

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 #define Mod 9999991
 8 #define LL long long
 9 
10 int qpow(int x,int b)
11 {
12     int ans=1;
13     while(b)
14     {
15         if(b&1) ans=1LL*ans*x%Mod;
16         x=1LL*x*x%Mod;
17         b>>=1;
18     }
19     return ans;
20 }
21 
22 int main()
23 {
24     int n;
25     scanf("%d",&n);
26     int ans=qpow(n,n-2);
27     for(int i=1;i<=n-1;i++) ans=1LL*ans*i%Mod;
28     printf("%d\n",ans);
29     return 0;
30 }
View Code

 

2017-04-25 14:57:48

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章