BZOJ 4001 概率论 解题报告


4001: [TJOI2015]概率论
Time Limit: 10 Sec  Memory Limit: 128 MB
Description
为了提高智商,ZJY开始学习概率论。有一天,她想到了这样一个问题:对于一颗随机生成的$n$个节点的有根二叉树(所以互相不同构的形态等概率出现),它的叶子节点数的期望是多少呢?
      
判断两棵树是否同构的伪代码如下:
算法1bool Check(T1, T2)
         Require
:两棵树的节点T1, T2
                  if T1==null || T2==null then
                          return T1==null && T2==null
                  else
                          return Check(T1->leftson, T2->leftson) && Check(T1->rightson, T2->rightson)
                  end if
Input
输入一个正整数$N$,代表有根树的结点数
Output
 输出这棵树期望的叶子节点数。要求误差小于$1\times10^{-9}$
Sample Input
1
Sample Output
1.000000000
HINT
$1\leqslant N\leqslant10^9$
Solution
$ \mathrm P(x)$表示$x$个节点的二叉树叶子节点数的期望,$ f_x$$x$个节点可构成的不同二叉树的数量,$s_x$$x$个节点构成的所有不同二叉树的叶子节点的和。由期望的定义可得,$ \mathrm P(x)=\frac{s_x}{f_x}$
$f_x$的求法是一个经典的卡塔兰数问题:固定某个点为根节点,设其左子树有$a$个节点,右子树有$b$个节点,在此条件下,不同的二叉树的数量等于$f_af_b$。而对于$f_x$$a+b=x-1$$ 0\leqslant a\leqslant x-1$。即:$$ f_x=\sum_{i=0}^{x-1}f_i\cdot f_{x-1-i}$$
显然$f_x$是第$x$个卡塔兰数(参见https://en.wikipedia.org/wiki/Catalan_number)。于是,$f_x=\frac1{x+1}\begin{pmatrix}2x\\x\end{pmatrix}$
我们用相似的方式推导$s_x$的递推式:
固定某个点为根节点,设其左子树有$a$个节点,右子树有$b$个节点,我们首先这考虑左子树对$s_x$的贡献,所有不同的左子树的叶子节点的数量和为$s_a$,每种方案都可以和一个右子树结合得到一棵不同形态的二叉树,结合的方案数为$f_b$,所以左子树对$s_x$的贡献为$s_af_b$。类似地,左右子树的总贡献为$s_af_b+f_as_b$。又因为$a+b=x$,所以:$$ s_x=\sum_{i=0}^{x-1}(s_i\cdot f_{x-1-i}+f_i\cdot s_{x-1-i})=2\sum_{i=0}^{x-1}s_i\cdot f_{x-1-i}$$
参考卡塔兰数通项公式的推导,我们采用母函数计算$s_x$的通项公式。

我们首先确定$s_0$的值,由于$s_1=2f_0s_0=1$$s_2=2s_0f_1+2s_1f_0=2$,我们发现$s_0$无论取和值都无法满足条件。于是我们大胆猜测,$s_x$的递推式不适用于第一项。这样,$s_0=0$
根据普通母函数的定义,设:$$ g(x)=\sum_{n=0}^\infty s_nx^n$$ $$ h(x)=\sum_{n=0}^\infty f_nx^n$$
展开,有$$g(x)=s_0+s_1x+s_2x^2+s_3x^3+...$$ $$h(x)=f_0+f_1x+f_2x^2+f_3x^3+...$$
考虑母函数与递推式的联系:$$ g(x)h(x)=s_0f_0+(s_0f_1+s_1f_0)x+(s_2f_0+s_1f_1+s_0f_2)x^2+...$$
整理,得:$$ g(x)h(x)=\sum_{i=0}^0s_if_{0-i}+(\sum_{i=0}^1s_if_{1-i})x+(\sum_{i=0}^2s_if_{2-i})x^2+...$$
根据$s_x$的递推式:$$ xg(x)h(x)=\sum_{i=0}^0s_if_{0-i}x+(\sum_{i=0}^1s_if_{1-i})x^2+(\sum_{i=0}^2s_if_{2-i})x^3+...$$ $$2xg(x)h(x)=0+s_2x^2+s_3x^3+...$$
即:$$ 2xg(x)h(x)=g\left(x\right)-s_1x$$
结合卡塔兰数的推导:$$\left\{\begin{array}{l}2xg(x)h(x)=g\left(x\right)-x\\xh(x)^2=h(x)-1\end{array}\right.$$
解得:$$\left\{\begin{array}{l}g(x)=\frac x{\sqrt{1-4x}}\\h(x)=\frac{1-\sqrt{1-4x}}{2x}\end{array}\right.,\left\{\begin{array}{l}g(x)=-\frac x{\sqrt{1-4x}}\\h(x)=\frac{1+\sqrt{1-4x}}{2x}\end{array}\right.$$
不妨令$g(x)=\frac x{\sqrt{1-4x}} =x(1-4x)^{-\frac12}$,根据二项式定理:$$g(x)=x\sum_{n=0}^\infty\begin{pmatrix}-\frac12\\n\end{pmatrix}\left(-4x\right)^n$$ $$g(x)=x\sum_{n=0}^\infty\frac{(-{\displaystyle\frac12})\times(-{\displaystyle\frac32})\times...\times(-{\displaystyle\frac12}-i+1)}{n!}\left(-4x\right)^n$$
$$g(x)=x\sum_{n=0}^\infty\frac{1\times3\times...\times(2n-1)}{n!}2^nx^n$$
$$g(x)=x\sum_{n=0}^\infty\frac{(2n)!}{n!\cdot n!} x^n$$
$$ g(x)=x\sum_{n=0}^\infty\begin{pmatrix}2n\\n\end{pmatrix}x^n$$
$$g(x)=\sum_{n=1}^\infty\begin{pmatrix}2n-2\\n-1\end{pmatrix}x^n$$
所以$$s_x=\begin{pmatrix}2x+2\\x+1\end{pmatrix}$$
     得到了$s_x$的通项公式。我们可以一眼看出$ \mathrm P(x)=\frac{x^2+x}{4x-2}$
       代码一分钟,公式一下午!
:(

CF 696B Puzzles 解题报告


Puzzles
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 through n and n - 1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit any city he wants by following roads.
Some girl has stolen Barney's heart, and Barney wants to find her. He starts looking for in the root of the tree and (since he is Barney Stinson not a random guy), he uses a random DFS to search in the cities. A pseudo code of this algorithm is as follows:

let starting_time be an array of length n
current_time = 0
dfs(v):
        current_time = current_time + 1
        starting_time[v] = current_time
        shuffle children[v] randomly (each permutation with equal possibility)
        // children[v] is vector of children cities of city v
        for u in children[v]:
               dfs(u)
As told before, Barney will start his journey in the root of the tree (equivalent to call dfs(1)).
Now Barney needs to pack a backpack and so he wants to know more about his upcoming journey: for every city i, Barney wants to know the expected value of starting_time[i]. He's a friend of Jon Snow and knows nothing, that's why he asked for your help.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 105) — the number of cities in USC.
The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the number of the parent city of city number i in the tree, meaning there is a road between cities numbered pi and i in USC.
Output
In the first and only line of output print n numbers, where i-th number is the expected value of starting_time[i].
Your answer for each city will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
input
7
1 2 1 1 4 4
output
1.0 4.0 5.0 3.5 4.5 5.0 5.0
input
12
1 1 2 2 4 4 3 3 1 10 8
output
1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0

题目翻译:
谜题
每个测试点时间限制 1
每个测试点空间限制 256兆字节
输入 标准输入
输出 标准输出
巴尼住在USC (查则合众国,United States of Charzeh). USC  n 座城市组成,编号为1  n ,由 n - 1 条道路将它们相连。城市与道路组成一颗有根树(巴尼不清楚为什么它有根)。根为1号城市。也就是说,如果一个人从1号城市出发,道路可以将他送达他想去的任何城市。
一个姑娘让巴尼爱慕,他想找到她。他从树的根开始,采用随机DFS搜索所有城市。搜索算法的伪代码如下:

starting_time 为长度为n的数组
current_time = 0
dfs(v):
        current_time = current_time + 1
        starting_time[v] = current_time
       
随机打乱 children[v] (每一种排列出现的概率相等)
        // children[v]
是城市v的子节点组成的数组
       
对于children[v]的每一个元素u
               dfs(u)
如上文所述,巴尼将从树的根出发(等价于调用dfs(1))。
现在巴尼收拾行李准备出发,他希望知道在他接下来的行程中:对于每一个城市i,巴尼想知道starting_time[i]的期望值。他是乔恩·斯诺的朋友,他什么都不懂,因此他向你求助。
输入
第一行包含一个整数 n (1 ≤ n ≤ 105) —USC的城市数量。
第二行包含 n - 1 个整数p2, p3, ..., pn (1 ≤ pi < i), pi 表示城市 i 的父节点, 也就是USC的城市 pi i 之间有一条道路相连。
输出
第一行(也是唯一一行)包含 n 个数字,第i个数字表示starting_time[i]的期望值
你的答案与标准答案的绝对或相对误差不超过10 - 6即被认为正确。
样例
输入
7
1 2 1 1 4 4
输出
1.0 4.0 5.0 3.5 4.5 5.0 5.0
输入
12
1 1 2 2 4 4 3 3 1 10 8
输出
1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0

解析
先证两个引理:
【引理1】在1~$n$的所有全排列中,数字1出现在任何一个位置的概率相等,且等于$ \frac1n$
证明:设数字1出现在了位置$ k(k\in\left\{x\in\boldsymbol N\vert1\leqslant x\leqslant n\right\})$,满足条件的全排列的个数$c_k=(n-1)!$,而全排列的个数$C=n!$,概率$ P(k)=\frac{(n-1)!}{n!}=\frac1n$
【引理2】对于$ \forall n\in\boldsymbol N\cap\lbrack2,+\infty\rbrack,\boldsymbol\;\sum_{i=1}^n\frac{\begin{pmatrix}n-1\\i-1\end{pmatrix}}{\begin{pmatrix}n\\i\end{pmatrix}}=\frac{n+1}2$
证明:设$m=n-1$,原命题转化为:$$\boldsymbol\;\sum_{i=1}^{m+1}\frac{\begin{pmatrix}m\\i-1\end{pmatrix}}{\begin{pmatrix}m+1\\i\end{pmatrix}}=\frac{m+2}2$$ $$\Leftrightarrow\boldsymbol\;\sum_{i=1}^m\frac{\begin{pmatrix}m\\i-1\end{pmatrix}}{\begin{pmatrix}m+1\\i\end{pmatrix}}+\frac{\begin{pmatrix}m\\m\end{pmatrix}}{\begin{pmatrix}m+1\\m+1\end{pmatrix}}=\frac m2+1$$ $$\Leftrightarrow\boldsymbol\;\sum_{i=1}^m\frac{\begin{pmatrix}m\\i-1\end{pmatrix}}{\begin{pmatrix}m+1\\i\end{pmatrix}}=\frac m2$$ $$\Leftrightarrow\boldsymbol\;\sum_{i=1}^m\frac{\displaystyle\frac{m!}{(m-i+1)!\cdot(i-1)!}}{\displaystyle\frac{(m+1)!}{(m+1-i)!\cdot i!}}=\frac m2$$ $$\Leftrightarrow\boldsymbol\;\sum_{i=1}^m\frac{\mathbf i}{\boldsymbol(\mathbf m\boldsymbol+\mathbf1\boldsymbol)}=\frac m2$$ $$\Leftrightarrow\frac{\displaystyle\frac{\mathbf m\boldsymbol(\mathbf m\boldsymbol+\mathbf1\boldsymbol)}{\mathbf2}}{\boldsymbol(\mathbf m\boldsymbol+\mathbf1\boldsymbol)}=\frac m2$$显然成立。故原命题成立。
引理1告诉我们,当巴尼位于点$u$时,如果点$u$的子节点有$n$个,他第$i$个遍历每一个子树的概率都等于$ \frac1n$。又根据深度优先搜索的性质,巴尼必须遍历完整棵子树后才会遍历下一棵子树,也就是一旦遍历节点$v$所在的子树,发现顺序的期望将增加$ sum_v\cdot p $$sum_v$为子树节点的总数,$p$为这种情况发生的概率)。
不妨令$n=6$我们有6种情况:第1个遍历节点$v$,第2个遍历节点$v$……第6个遍历节点$v$。为了方便讲解,设节点$u$的六个子树的节点数和分别为$a$$b$$c$$d$$e$$f$,并假设$sum_v=f$
情况一对期望的贡献为$ \frac16$,这个贡献是必须的,以下其余情况我们只讨论相对情况一的额外贡献。
情况二对期望的额外贡献为$ \frac16\cdot(\frac a5+\frac b5+\frac c5+\frac d5+\frac e5)$。可见情况二又细分为了5种子情况,假如即$a+b+c+d+e=s$,情况二的贡献可以写成$ \frac16\cdot(\frac s5)$
情况三更加复杂,因为在遍历点$v$之前,有10种遍历另外2个子树的方案,额外贡献为$ \frac16\cdot(\frac{a+b+a+c+a+d+a+e+b+c+b+d+b+e+c+d+c+e+d+e}{10})$,五个字母各出现了4次,即贡献为$ \frac16\cdot(\frac{4s}{10})$104是如何计算的呢?显然,它们分别等于$ \begin{pmatrix}5\\2\end{pmatrix}$,和$ \begin{pmatrix}4\\1\end{pmatrix}$
类似地,情况四的额外贡献为$ \frac s6\cdot\frac{\displaystyle\begin{pmatrix}4\\3\end{pmatrix}}{\begin{pmatrix}5\\4\end{pmatrix}}$
所有情况的总贡献为$\frac s6\sum_{i=1}^5\frac{\begin{pmatrix}4\\i-1\end{pmatrix}}{\begin{pmatrix}5\\i\end{pmatrix}}+1$根据引理2,这个值为$\frac s2+1$。同时,由于节点$v$会继承其父节点$u$的期望,$p_v=\frac s2+1+p_u$。而$s=sum_u-sum_v-1$,所以$ p_v=\frac{\displaystyle sum_u-sum_v-1}{\displaystyle2}+1+p_u $
于是,此题可以使用动态规划求解。