Description

Saratov ACM ICPC teams have a tradition to come together on Halloween and recollect terrifying stories. And the most popular story among the newcomers is the story about the "Mescher Tree". A long time ago, when the famous Dmitry Mescheryakov aka Mescher was very young and he even didn't know how to write Dijkstra algorithm, he faced a difficult problem with a tree. Input file contained \(n\) — the number of vertices, and pairs of vertices, connected with an edge. Without thinking a lot (honestly, the exact reason of that mistake is unknown), he wrote the following code:

read(n);
for i := 1 to n do begin
read(u, v);
g[u, v] := true;
g[v, u] := true;
end;

Mescher successfully compiled his code, got WA on sample test and started long debugging... This story has become a true legend. So it's no surprise that Saratov ACM ICPC teams use the following definition: connected undirected graph with n vertices and n edges is called Mescheryakov Tree or, less formally, Mescher Tree. The area of application of Mescher trees is not well-studied, so we suggest you to solve one of the problems connected with such trees: given n, find the number of distinct Mescher trees with \(n\) vertices. Trees are labeled, i.e. two trees are considered distinct if and only if their adjacency matrices differ.

Input

Input contains single integer number \(n (3 \le n \le 5000)\).

Output

Output the number of Mescher trees with \(n\) vertices without leading zeroes.

Sample Input

3

Sample Output

1

这是一个比较经典的题目,即求\(n\)个点\(n\)条边的有标号无向连通图的个数。

我们设\(f(n)\)为答案,那么有$$f(n) = \frac{(n-1)! \bullet n^n}{2} \bullet \sum_{k = 3}^n \frac{1}{n^k(n-k)!}$$

证明我们可以采用算两次的方法进行证明:

设\(g(n,k)\)表示由\(k\)棵有根数组成的,节点数为\(n\)且带标号的图的个数,不妨设边的方向都是由父亲指向儿子。

再令\(h(n,k)\)表示有多少种长度为\(n-k\)有向边的序列,使得按这个顺序加边后刚好会构成由\(k\)棵根树构成的节点带有标号的图。则我们可以得到\(h(n,k)\)的一种计算方法

\[h(n,k) = g(n,k) \times (n-k)!
\]

这个式子不难得到。我们在考虑另一种方法——每次选出一条边,其中这条边由一个点连向非他自己所属有根树的根(初始每个点自己就是一棵树),每增加一条边就会减少一棵树。故有

\[h(n,k) = n(n-1) \times n(n-2) \times \cdots \times nk = n^{n-k} \times \frac{(n-1)!}{(k-1)!}
\]

于是有$$g(n,k) \times (n-k)! = n^{n-k} \times \frac{(n-1)!}{(k-1)!}$$

解得$$g(n,k) = n^{n-k} \times \binom{n-1}{k-1}$$

之后考虑如何用\(g(n,k)\)求\(f(n)\)。由于\(n\)个点\(n\)条边的无向连通图一定有且只有\(1\)个环。我们可以枚举环的长度\(k\),然后把环看成根,就等同于环上套了\(k\)棵有根树。从\(3\)开始枚举环的长度,便有

\[f(n) = \sum_{k=3}^n(g(n,k) \times \frac{(n-1)!}{2})
\]

其中\(\frac{(k-1)!}{2}\)是\(k\)个数排成一个圈的本质不同排列数。

于是将$$g(n,k) = n^{n-k} \times \binom{n-1}{k-1}$$代入,便可得到开始的公式了。

由于SGU目前不可提交,所以没有代码。

SGU481 Hero of Our Time的更多相关文章

  1. H5游戏开发之Stick Hero

    自从上次发布一个小恐龙游戏以后,到现在10天了,前后又写了3个游戏,挑了一个感觉比较有挑战的游戏和大家分享一下. 效果演示 这是我模拟一个苹果游戏<stick hero>游戏写的一个小游戏 ...

  2. BZOJ 1191 超级英雄 Hero 题解

    BZOJ 1191 超级英雄 Hero 题解 Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金 ...

  3. 2016HUAS暑假集训训练2 K - Hero

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/K 这也是一道贪心题,刚开始写时以为只要对每一敌人的攻击和血的乘积进行从小到大排序即 ...

  4. 【入门】匈牙利算法+HNOI2006 hero超级英雄

    一.关于匈牙利算法 匈牙利算法是由匈牙利数学家Edmonds提出的,用增广路径求二分图最大匹配的算法. 听起来高端,其实说白了就是: 假设不存在单相思(单身狗偷偷抹眼泪),在一个同性恋不合法的国家里( ...

  5. 2016HUAS_ACM暑假集训2K - Hero(英雄)

    这也属于一个贪心题.关键是排序的依据. 这题排序的依据是敌人的伤害/血量(DPS/HP),不难证明,当这个比值相同时,不论先解决谁效果是相同的.思路大部分在注释里. 题目大意: 假设你的血量无限,但是 ...

  6. bzoj 1191: [HNOI2006]超级英雄Hero

    1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec  Memory Limit: 162 MB 二分图匹配... Description 现在电视台有一种节目叫做超 ...

  7. HDU 4901 The Romantic Hero

    The Romantic Hero Time Limit: 3000MS   Memory Limit: 131072KB   64bit IO Format: %I64d & %I64u D ...

  8. HDU4901 The Romantic Hero 计数DP

    2014多校4的1005 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4901 The Romantic Hero Time Limit: 6000/30 ...

  9. HDU 4310 Hero (贪心算法)

    A - Hero Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

随机推荐

  1. [转]Oracle开发专题之:%TYPE 和 %ROWTYPE

    本文转自:http://www.cnblogs.com/kingjiong/archive/2009/02/19/1393837.html 1. 使用%TYPE 在许多情况下,PL/SQL变量可以用来 ...

  2. JavaScript高级程序设计(第三版)学习,第一次总结

    Array类型 var arr = []; arr.length; //返回数组元素个数 改变length可以动态改变数组大小 检测数组 instanceof可以检测某个对象是否是数组,限制:只能是一 ...

  3. MySQL 에서 root 암호 변경하기

    먼저 터미널이나 SSH를 이용해 접속합니다. 그리고 리눅스 쉘상에서 다음과 같이 입력합니다. [mysqladmin을 이용한 암호변경] 형식) root계정]$mysqladmin -u ...

  4. oc语言学习之基础知识点介绍(三):类方法、封装以及继承的介绍

    一.类方法的使用 /* 像我们之前学的方法,必须先实例化这个类的对象才能调用这个方法,类方法不用实例化对象,直接调用这个方法. 之前学的方法调用的语法: [对象名 方法名]; //对象方法 类方法: ...

  5. SQL SERVER 主键约束

    主键约束: 遵循关系型模型中的第二范式.唯一的识别一条记录,不能为空. CREATE TABLE Persons ( Id_P int NOT NULL PRIMARY KEY, LastName v ...

  6. ###《Video Event Detection by Inferring Temporal Instance Lables》

    论文作者:Kuan-Ting Lai, Felix X. Yu, Ming-Syan Chen and Shih-Fu Chang. #@author: gr #@date: 2014-01-25 # ...

  7. C# ACM poj1004

    水题.. public static void acm1004(float[] a) { ; foreach (var item in a) { sum += item; } Console.Writ ...

  8. C# ACM poj1003

    这题很有内涵,先用简单方法 public static void acm1003(double a) { ) { return; } ; ) { / b; a = a - c; b++; } Cons ...

  9. Oracle PL/SQL 找出100以内是3和5的倍数的数 循环语句

    循环: loop --执行代码 exit when 表达式;--当表达式为真退出循环.(注意,其编写位置决定循环为先判断还是先执行,相当于java的while或do-while) end loop; ...

  10. String Shifting

    我们规定对一个字符串的shift操作如下:略去.shift(string, x) = string(0 <= x < n). 分析:一看这题,这不很简单么,直接模拟判断,但是这套路有这么简 ...