luogu3830 [SHOI2012]随机树

ref
主要学到了期望有时候直接当平均值就好了,还有就是一个期望的问题:

\(E(x)=\sum_{i=1}^\infty P(x \geq i)\),他的意思是说对于一个变量 \(x\),他的期望值是 \(x \geq i\) 的概率之和。为什么呢?

\(E(x)=0(P(\geq 0)-P(\geq 1)) + 1(P(\geq 1)-P(\geq 2))+\cdots\)

这样比较像差分,就很容易证明了。

#include <iostream>
#include <cstdio>
using namespace std;
int q, n;
namespace touhou{
    double f[105];
    void main(){
        for(int i=2; i<=n; i++)
            f[i] = f[i-1] + 2.0 / i;
        printf("%.6f\n", f[n]);
    }
}
namespace project{
    double f[105][105], ans;
    void main(){
        for(int x=1; x<=n; x++){
            f[x][0] = 1;
            for(int d=1; d<=x; d++){
                for(int i=1; i<x; i++)
                    f[x][d] += f[i][d-1] + f[x-i][d-1] - f[i][d-1] * f[x-i][d-1];
                if(x!=1)    f[x][d] /= x - 1;
            }
        }
        for(int i=1; i<=n; i++)
            ans += f[n][i];
        printf("%.6f\n", ans);
    }
}
int main(){
    cin>>q>>n;
    if(q&1) touhou::main();
    else    project::main();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/poorpool/p/9103497.html
今日推荐