首页 > 代码库 > BZOJ1430: 小猴打架

BZOJ1430: 小猴打架

1430: 小猴打架

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 328  Solved: 234
[Submit][Status]

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

题解:

水题。。。

n个点不同形态的生成数有n^(n-2)个,然后打架过程还分先后相当于全排列

所以 ans=n^(n-2) * (n-1)!

代码:

 1 #include<cstdio> 2  3 #include<cstdlib> 4  5 #include<cmath> 6  7 #include<cstring> 8  9 #include<algorithm>10 11 #include<iostream>12 13 #include<vector>14 15 #include<map>16 17 #include<set>18 19 #include<queue>20 21 #include<string>22 23 #define inf 100000000024 25 #define maxn 500+10026 27 #define maxm 500+10028 29 #define eps 1e-1030 31 #define ll long long32 33 #define pa pair<int,int>34 35 #define for0(i,n) for(int i=0;i<=(n);i++)36 37 #define for1(i,n) for(int i=1;i<=(n);i++)38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)42 43 #define mod 999999144 45 using namespace std;46 47 inline int read()48 49 {50 51     int x=0,f=1;char ch=getchar();52 53     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}54 55     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();}56 57     return x*f;58 59 }60 61 int main()62 63 {64 65     freopen("input.txt","r",stdin);66 67     freopen("output.txt","w",stdout);68 69     ll n=read(),ans=1;70     for1(i,n-2)ans=(ans*n)%mod;71     for1(i,n-1)ans=(ans*i)%mod;72     printf("%lld\n",ans);73 74     return 0;75 76 }
View Code

 

BZOJ1430: 小猴打架