zoukankan      html  css  js  c++  java
  • 洛谷P3830 [SHOI2012]随机树(期望dp)

    题面

    luogu

    题解

    第一问:

    (f[i])表示(i)步操作后,平均深度期望

    (f[i] = frac {f[i - 1] * (i - 1)+f[i-1]+2}{i}=f[i-1]+frac{2}{i})

    第二问就比较难受了:

    (E(x)=∑_{i=1}^{x}P)

    随机变量(x)的期望为对于所有(i)(i≤x)的概率之和

    我们设(f[i][j])表示(i)步后,树的深度(>=j)的概率

    我们每次新建一个根,然后枚举左右子树分配节点情况

    (f[i][j] = frac{1}{i-1}sum_{k=1}^{i-1}(f[k][j-1]*1+f[i-k][j-1]*1-f[k][j-1]*f[i-k][j-1]))

    然后

    (Ans = sum_{i=1}^{n-1}f[n][i])

    Code

    #include<bits/stdc++.h>
    
    #define LL long long
    #define RG register
    
    using namespace std;
    template<class T> inline void read(T &x) {
    	x = 0; RG char c = getchar(); bool f = 0;
    	while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
    	while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
    	x = f ? -x : x;
    	return ;
    }
    template<class T> inline void write(T x) {
    	if (!x) {putchar(48);return ;}
    	if (x < 0) x = -x, putchar('-');
    	int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
    	for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
    }
    const int N = 110;
    int q, n;
    namespace cpp1 {
    	double f[N];
    	void main() {
    		for (int i = 2; i <= n; i++) f[i] = f[i - 1] + 2.0 / i;
    		printf("%lf
    ", f[n]);
    		return ;
    	}
    }
    namespace cpp2 {
    	double f[N][N];
    	void main() {
    		for (int i = 1; i <= n; i++) f[i][0] = 1;
    		for (int i = 2; i <= n; i++)
    			for (int j = 1; j < n; j++) {
    				for (int k = 1; k < i; k++)
    					f[i][j] += f[k][j - 1] + f[i - k][j - 1] - f[k][j - 1] * f[i - k][j - 1];
    				f[i][j] /= (i - 1);
    			}
    		double ans = 0;
    		for (int i = 1; i < n; i++) ans += f[n][i];
    		printf("%lf
    ", ans);
    	}	
    }
    
    int main() {
    	read(q), read(n);
    	if (q == 1) cpp1 :: main();
    	else cpp2 :: main();
    	return 0;
    }
    
    
  • 相关阅读:
    「AtCoder AGC023F」01 on Tree
    「Wallace 笔记」平面最近点对 解法汇总
    「Codeforces 1181E」A Story of One Country (Easy & Hard)
    「NOI2018」「LOJ #2720」「Luogu P4770」 你的名字
    IdentityServer4设置RefreshTokenExpiration=Sliding不生效的原因
    【知识点】IQueryable.SumAsync方法的NULL异常
    Beyond Compare 4 密钥被吊销
    【知识点】Uri对象的完整地址
    git文件夹大小写问题
    .Net Core学习资料
  • 原文地址:https://www.cnblogs.com/zzy2005/p/10551554.html
Copyright © 2011-2022 走看看