当前位置:网站首页>[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
边栏推荐
- Detailed explanation of Niuke - Gloves
- Query the data from 2013 to 2021, and only query the data from 2020. The solution to this problem is carried out
- TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
- 5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
- Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
- Nacos detailed explanation, something
- Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
- 安装及管理程序
- LVM与磁盘配额
- MySQL master-slave replication
猜你喜欢

网络安全之渗透靶场实战详解

昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案

建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细

Node access to Alipay open platform sandbox to achieve payment function

Pycham connects to the remote server and realizes remote debugging

批量制造测试数据的思路,附源码

Construction of promtail + Loki + grafana log monitoring system

详解牛客----手套

【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验

LVM与磁盘配额
随机推荐
oracle 中快速获取表的列名列表
How vscode compares the similarities and differences between two files
Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme
Detailed explanation of UWA pipeline function | visual configuration automatic test
Detailed explanation of the penetration of network security in the shooting range
MySQL personal learning summary
Loading order of logback configuration file
众昂矿业:萤石浮选工艺
Calculate pie chart percentage
磁盘管理与文件系统
LVM与磁盘配额
【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
Rtklib 2.4.3 source code Notes
File upload and download of robot framework
Differences between MySQL BTREE index and hash index
DanceNN:字节自研千亿级规模文件元数据存储系统概述
05 Lua control structure
Xinwangda: HEV and Bev super fast charging fist products are shipped on a large scale
1959年高考数学真题
人脸识别框架之dlib