TJOI2015
Problem's Link
----------------------------------------------------------------------------
Mean:
求节点数为n的有根树期望的叶子结点数.(n≤10^9)
analyse:
方案数就是卡特兰数,$h_0=1, h_n = sum_{i=0}^{n-1} h_i h_{n-1-i} (。 设叶子数量和为)f_n(,则得到)f_n = 2 sum_{i=0}^{n-1} f_i h_{n-1-i}$
设(H(x))表示(h_n)的母函数,(F(x))表示(f_n)的母函数
容易得到:[H(x) = x H^2(x) + 1] [F(x) = 2 x F(x) H(x) + x]即:[H(x) = frac{1-sqrt{1-4x}}{2x}] [F(x) = frac{x}{1-sqrt{1-4x}}]发现[(xH(x))' = sum_{i=0}^{infty} (i+1)h_i x^i = frac{1}{sqrt{1-4x}} = frac{F(x)}{x}]即[F(x) = sum_{i=0}^{infty} (i+1)h_i x^{i+1} = sum_{i=1}^{infty} i h_{i-1} x^i = sum_{i=0}^{infty} f_i x^i]即(f_i = i h_{i-1})
所以(ans = frac{f_n}{h_n} = frac{n h_{n-1}}{h_n} = frac{n(n+1)}{2(2n-1)})
Time complexity: O(N)
view code
#include <bits/stdc++.h>
using namespace std;
typedef long double lf;
int main() {
lf n;
scanf("%Lf", &n);
printf("%.9Lf ", n*(n+1)/2/(n*2-1));
return 0;
}
using namespace std;
typedef long double lf;
int main() {
lf n;
scanf("%Lf", &n);
printf("%.9Lf ", n*(n+1)/2/(n*2-1));
return 0;
}