Description

Vasya’s dad is good in maths. Lately his favorite objects have been "beautiful" directed graphs. Dad calls a graph "beautiful" if all the following conditions are true:

  • The graph contains exactly \(N\) vertices and \(N−1\) edges.
  • Exactly one vertex has no entering edges.
  • The graph contains no directed cycles.

Dad calls two "beautiful" graphs isomorphic, if the vertices of the first graph can be renumbered in such way that it turns into the second one.

Dad picks an integer \(N\), stocks up blank paper, and draws a "beautiful" graph on each sheet. He verifies that no two drawn graphs are isomorphic.

Given the number \(N\), you are to find the number of sheets that Vasya's dad has to stock up.

Input

Input contains the single integer \(N (1 \le N \le 50)\).

Output

Output the number of "beautiful" graphs with \(N\) vertices.

Sample Input

3

Sample Output

9

题目大意——求\(N\)个点有标号的有根树的数目是多少。

假设\(a_n\)是\(n\)个点的无标号有根树的数目,则有以下的公式:

\[a_n = \sum_{\sum_{i = 1}^{n-1}}[\prod_{k=1}^{n-1}\binom{a_k+c_k-1}{c_k}]
\]

其中\(c_k\)表示根节点的子树中大小为\(i\)的子树有多少个。

为什么是\(\binom{a_k+c_k-1}{c_k}\),这是个可重组合公式。我们可以这样考虑,我们现在有\(a_k\)中子树可以选,我们可以从中选出\(c_k\)个。那么我们相当于$$\sum_{i = 1}^{a_k}x_i = c_k$$的非负整数解的方案数。也就等价于

\[\sum_{i = 1}^{a_k}x_i = c_k+a_k$$的正整数解的方案数。使用隔板法,不难得出公式
$$\binom{a_k+c_k-1}{a_k-1} = \binom{a_k+c_k-1}{c_k}\]

再用下乘法原理,上述公式就得证了。但是复杂度太高,虽然打表依旧可过。然后我们可以利用生成函数优化公式(母函数),然而这一块我们看懂。wtz说了用了很高深的解析组合的公式。希望以后学了后我能够看懂,先记在这里。

设$$A(x) = \sum_{n = 0}{\infty}a_nxn$$

基于上述分析可以迅速(tm那里迅速了)得到

\[A(x) = x \times e^{\sum_{r = 1}^{\infty}A(x^r)}
\]

于是就可推导出

\[a_{n+1} = \frac{1}{n} \times \sum_{i = 1}^n(i \times a_i \times \sum_{j=1}^{\lfloor n/i \rfloor}a_{n+1-i \times j})
\]

wtz还告诉了我假如树无根,那么也有公式:

  • 当\(n\)是奇数时,答案为$$a_n-\sum_{1 \le i \le \frac{n}{2}}a_ia_{n-i}$$
  • 当\(n\)是偶数时,答案为$$a_n-\sum_{1 \le i \le n}a_ia_{n-1}+\frac{1}{2}a_{\frac{n}{2}}(a_{\frac{n}{2}}+1)$$

然后我就用java(因为要高精度)对着公式打,就ac了。

import java.math.*;
import java.util.*;
public class Main
{
static final int maxn = 55;
static BigInteger A[] = new BigInteger[maxn]; static int N;
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
N = cin.nextInt();
A[1] = BigInteger.valueOf(1);
A[2] = BigInteger.valueOf(1);
A[3] = BigInteger.valueOf(2);
for (int n = 3;n < N;++n)
{
A[n+1] = BigInteger.ZERO;
for (int i = 1;i <= n;++i)
{
BigInteger res; res = BigInteger.ZERO;
for (int j = 1;j <= n/i;++j) res = res.add(A[n+1-i*j]);
A[n+1] = A[n+1].add(res.multiply(A[i]).multiply(BigInteger.valueOf(i)));
}
A[n+1] = A[n+1].divide(BigInteger.valueOf(n));
}
System.out.println(A[N]);
}
}

Ural1387 Vasya's Dad的更多相关文章

  1. Milliard Vasya's Function-Ural1353动态规划

    Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning mathematician. He decided to make ...

  2. CF460 A. Vasya and Socks

    A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  4. Codeforces Round #281 (Div. 2) D. Vasya and Chess 水

    D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分

    C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. codeforces 676C C. Vasya and String(二分)

    题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...

  7. Where is Vasya?

    Where is Vasya? Vasya stands in line with number of people p (including Vasya), but he doesn't know ...

  8. Codeforces Round #324 (Div. 2) C. Marina and Vasya 贪心

    C. Marina and Vasya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pr ...

  9. Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题

    A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...

随机推荐

  1. 理解JavaScript原型式继承

    0.基础 javascript没有类的概念, javascript不需要实例化某个具体类的实例.javascript对象本身可以用来创建对象, 而对象可以继承自其他对象, 这个概念称为原型式继承 每个 ...

  2. gd库复制图片做水印

    将复制源图片的某个位置复制到目标图片中,不能调整大小 imagecopy(目标图片画布,复制源画布,目标画布左上角x,y,源画布左上角x,y,复制图片的宽,高); 允许调整大小 imagecopyre ...

  3. 【算法】A*改进算法

    目的:我这里希望实现一个java A* 游戏里的战斗寻径 定义部分: 这个定义引用自 http://www.cnblogs.com/kanego/archive/2011/08/30/2159070. ...

  4. client 如何找到正确的RegionServer(HBase -ROOT-和.META.表)

    在HBase中,大部分的操作都是在RegionServer完成的,Client端想要插入,删除,查询数据都需要先找到相应的RegionServer.什么叫相应的RegionServer?就是管理你要操 ...

  5. 移动硬盘安装linux系统小记

    由于某种原因,笔记本电脑不在身边,因教学需要必须进行电脑展示教学,所以就有了如下的做法,写下来也是为以后方便吧.-- 目前手头有移动硬盘,怎么样才能实现用移动硬盘进行教学呢?!!! 机房若干台机器都是 ...

  6. Centos搭建PHP5.3.8+Nginx1.0.9+Mysql5.5.17

    操作环境 操作系统:Mac Os Lion 虚拟主机:VMware Fusion 虚拟系统:Centos 5.5+ 操作用户:Root 实现目的:搭建LNMP环境. 安装依赖库和开发环境 #依赖库和开 ...

  7. Oracle 递归函数与拼接

    ) name FROM table tb START ) CONNECT BY PRIOR ID=mt.parentid ; 在Oracle中,SYS_CONNECT_BY_PATH函数主要作用是可以 ...

  8. Mysql 流程控制

    流程控制 分支结构 if分支结构 语法:     if 条件then         -- 语句体     else         -- 缺省语句体     end if; 示例: 循环结构 whi ...

  9. 2016ACM竞赛训练暑期课期末考试 a题

    描述 给出n个正整数,任取两个数分别作为分子和分母组成最简真分数,编程求共有几个这样的组合. 输入 第一行是一个正整数n(n<=600).第二行是n个不同的整数,相邻两个整数之间用单个空格隔开. ...

  10. 转:基于IOS上MDM技术相关资料整理及汇总

    一.MDM相关知识: MDM (Mobile Device Management ),即移动设备管理.在21世纪的今天,数据是企业宝贵的资产,安全问题更是重中之重,在移动互联网时代,员工个人的设备接入 ...