zoukankan      html  css  js  c++  java
  • 【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
    ",ans);
    29     return 0;
    30 }
    View Code

    2017-04-25 14:57:48

  • 相关阅读:
    Array数组的使用
    map集合中取出分类优先级最高的类别名称
    关键字static介绍
    构造方法
    封装和private,this,super关键字的简单应用
    hitTest:withEvent:方法流程
    Events and Responder Chain
    block没那么难(一):block的实现
    [译] Block 小测验
    OAuth流程
  • 原文地址:https://www.cnblogs.com/Konjakmoyu/p/6762140.html
Copyright © 2011-2022 走看看