(color{#0066ff}{ 题目描述 })
为了提高智商,ZJY开始学习概率论。有一天,她想到了这样一个问题:对于一棵随机生成的n个结点的有根二叉树(所有互相不同构的形态等概率出现),它的叶子节点数的期望是多少呢?
判断两棵树是否同构的伪代码如下:
(color{#0066ff}{输入格式})
输入一个正整数n,表示有根树的结点数
(color{#0066ff}{输出格式})
输出这棵树期望的叶子节点数,要求误差小于1e-9
(color{#0066ff}{输入样例})
1
3
(color{#0066ff}{输出样例})
1.000000000
1.200000000
(color{#0066ff}{数据范围与提示})
对于30%的数据,1 ≤ n ≤ 10
对于70%的数据,1 ≤ n ≤ 100
对于100%的数据,1 ≤ n ≤ (10^9)
(color{#0066ff}{ 题解 })
根据概率,显然(ans=frac{sum二叉树叶子节点个数}{二叉树个数})
n个点的二叉树个数为(Catalan(n))
考虑第n个点的位置(作为叶子节点),通过手胡,可以发现有n个位置可以作为叶子节点,于是方案为(Catalan(n-1))
因此。。。(ans=frac{Catalan(n-1)*n}{Catalan(n)})
化简一下就是(frac{n*(n+1)}{4*n-2})
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
double n;
int main() {
n = in();
printf("%.10f", (double)((n) * (n + 1)) / (4.0 * n - 2));
return 0;
}