1430: 小猴打架
Time Limit: 5 Sec Memory Limit: 162 MB
Submit: 625 Solved: 452Description
一開始森林裏面有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。iosSource
【分析】ide
prufer的解釋在上一題。spa
答案顯然爲$(n-2)^{n}*(n-1)!$code
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 }
2017-04-25 14:57:48blog