Ural1387 Vasya's Dad
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\)个点的无标号有根树的数目,则有以下的公式:
\]
其中\(c_k\)表示根节点的子树中大小为\(i\)的子树有多少个。
为什么是\(\binom{a_k+c_k-1}{c_k}\),这是个可重组合公式。我们可以这样考虑,我们现在有\(a_k\)中子树可以选,我们可以从中选出\(c_k\)个。那么我们相当于$$\sum_{i = 1}^{a_k}x_i = c_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那里迅速了)得到
\]
于是就可推导出:
\]
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的更多相关文章
- Milliard Vasya's Function-Ural1353动态规划
Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning mathematician. He decided to make ...
- CF460 A. Vasya and Socks
A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- 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 ...
- 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 ...
- codeforces 676C C. Vasya and String(二分)
题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Where is Vasya?
Where is Vasya? Vasya stands in line with number of people p (including Vasya), but he doesn't know ...
- 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 ...
- 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 ...
随机推荐
- mysql命令行方式添加用户及设置权限
以前总是喜欢通过phpmyadmin去添加用户和数据库,这次装完系统后,配置了一大堆东东,实在不想安装phpmyadmin了,就通过命令行方式创建了数据库和设置权限,记录一下,免得以后总是百度 关键步 ...
- List集合即其遍历
1. 首先List<E>集合继承与Collection<E>,是一个接口. ① Collection (集合框架是JDK1.2版本出现的) ② list:是有序的,元素可 ...
- MVVM - 基础介绍
MVVM模式:把页面UI和后台逻辑分开,这样做的好处是能使你的程序更容易测试,维护和改进.
- SQL*PLUS中批量执行SQL语句
SQL*PLUS中批量执行SQL语句 今天由于工作的需要,要在CMD中批量执行大量的SQL语句,对于Oracle学习还处在入门阶段的我,只能硬着头皮到处去寻找资料(主要是网络资料,也包括自己的电子书) ...
- Android——按钮的事件监听
关于Button按钮的四种事件监听方法总结 首先我们在activity_main.xml里面先定义一个Button空间 <RelativeLayout xmlns:android="h ...
- Objective-C 学习笔记(Day 2)
------------------------------------------- 如何根据题目准确完整清晰的声明一个类并实现给定的行为 /* //下面这个程序教大家如何根据题目去声明一个类,并 ...
- 开启或关闭SQLSERVER服务的bat文件
界面如下: 因为电脑的SQLSERVER服务没有自己启动,有时候又需要关闭SQLSERVER服务,就自己在网上找了下教程. 源码如下: @echo offchoice /t 3 /c yn /d y ...
- 公共语言运行库(CLR)和中间语言(IL)(一)
公共语言运行库(.net运行库)即CLR 1.C#先编译为IL,IL为ms的中间语言,IL是平台无关性的. 2.CLR再将IL编译为平台专用语言. 3.CLR在编译IL时为即时编译(JIT) VB.V ...
- 【原创】关于MVC自己新建的 action,Controller提示找不到页面的问题
一.实例: 1.比如我自己新建了一个~/view/Shop 文件夹下的IndexShop.aspx,那么在Controllers文件夹下就要对应一个ShopController.cs的Control ...
- javascript学习笔记20160121-css选择器
元素可以用id.标签名或类来描述: 更一般的,元素可以基于属性来选取: 这些基本的选择器可以组合使用: 选择器可以指定文档结构(重要,之前一直不太明白>的使用): 选择器可以组合起来选取多个或多 ...