当前位置:网站首页>[problem solving] [show2012] random tree
[problem solving] [show2012] random tree
2022-04-23 16:52:00 【Ants looking up at the stars】
probability / expect dp Basic questions .
There is a binary tree background .
There are two things worth learning about this problem trick .
First look at the first question . Suppose there is i − 1 i-1 i−1 Leaf node , The average depth of each node is f i − 1 f_{i-1} fi−1 .
Then randomly select a node to copy . The expected depth of this node is f i − 1 f_{i-1} fi−1
therefore f i = ( i − 1 ) f i − 1 + f i − 1 + 2 i = f i − 1 + 2 i f_{i}=\frac{(i-1)f_{i-1}+f_{i-1}+2}{i}=f_{i-1}+\frac{2}{i} fi=i(i−1)fi−1+fi−1+2=fi−1+i2
Let's look at the second question .
set up g [ i ] [ j ] g[i][j] g[i][j] Express i i i Second expansion , The depth of the final tree ≥ j \geq j ≥j Of probability .
So the expected depth of the tree ∑ i = 1 n − 1 g [ n − 1 ] [ i ] \sum_{i=1}^{n-1}g[n-1][i] ∑i=1n−1g[n−1][i]
The total number of schemes observed is ( i − 1 ) ! (i-1)! (i−1)! , If the left subtree k k k Time , Right subtree i − k − 1 i-k-1 i−k−1 Time , that p = k ! ( i − k − 1 ) ! ( i − 1 k ) i ! = 1 i p=\frac{k!(i-k-1)!\binom{i-1}{k}}{i!}=\frac{1}{i} p=i!k!(i−k−1)!(ki−1)=i1 .
So according to probability / The expectation formula , g [ i ] [ j ] = ∑ k = 0 i − 1 g [ k ] [ j ] + g [ i − k − 1 ] [ j ] − g [ k ] [ j ] ∗ g [ i − k − 1 ] [ j ] i g[i][j]=\frac{\sum_{k=0}^{i-1}g[k][j]+g[i-k-1][j]-g[k][j]*g[i-k-1][j]}{i} g[i][j]=i∑k=0i−1g[k][j]+g[i−k−1][j]−g[k][j]∗g[i−k−1][j]
Time complexity O ( n 3 ) O(n^3) O(n3) .
#include<bits/stdc++.h>
#define db double
using namespace std;
const int N=105;
db dp[N],dp2[N][N];
int n,Q;
int main() {
for(int i=1;i<=100;i++) {
dp[i]=dp[i-1]+2.0/(i+1);
}
for(int i=0;i<=100;i++) dp2[i][0]=1;
for(int i=1;i<=100;i++) {
for(int j=1;j<=i;j++) {
for(int k=0;k<i;k++) {
dp2[i][j]+=(dp2[k][j-1]+dp2[i-k-1][j-1]-dp2[k][j-1]*dp2[i-k-1][j-1])/i;
}
}
}
scanf("%d%d",&Q,&n);
if(Q==1) printf("%lf",dp[n-1]);
else {
db res=0;
for(int i=1;i<=n-1;i++) {
res+=dp2[n-1][i];
}
printf("%lf",res);
}
}
版权声明
本文为[Ants looking up at the stars]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231649165891.html
边栏推荐
- Zhongang Mining: Fluorite Flotation Process
- MySQL master-slave synchronization pit avoidance version tutorial
- Gartner announces emerging technology research: insight into the meta universe
- Server log analysis tool (identify, extract, merge, and count exception information)
- 博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
- Feign report 400 processing
- org. apache. parquet. schema. InvalidSchemaException: A group type can not be empty. Parquet does not su
- LVM与磁盘配额
- Query the data from 2013 to 2021, and only query the data from 2020. The solution to this problem is carried out
- Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
猜你喜欢
1959年高考数学真题
Nodejs installation and environment configuration
DDT + Excel for interface test
Phpstudy V8, a commonly used software for station construction 1 graphic installation tutorial (Windows version) super detailed
The font of the soft cell changes color
Mock test
Selenium IDE and XPath installation of chrome plug-in
Real time operation of vim editor
如何建立 TikTok用户信任并拉动粉丝增长
loggie 源码分析 source file 模块主干分析
随机推荐
深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
Project framework of robot framework
网络安全之渗透靶场实战详解
How to implement distributed locks with redis?
Knowledge points and examples of [seven input / output systems]
MySQL master-slave configuration under CentOS
Talk about browser cache control
Installing labellmg tutorial in Windows
MySQL personal learning summary
无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
Encapsulating the logging module
VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)
Blue Bridge Cup provincial road 06 -- the second game of the 12th provincial competition
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
Pycham connects to the remote server and realizes remote debugging
oracle 中快速获取表的列名列表
LVM与磁盘配额
如何建立 TikTok用户信任并拉动粉丝增长
OMNeT学习之新建工程