当前位置:编程学习 > 网站相关 >>

PKU 1055Tree(数学 调和级数欧拉常数)

1055:Tr
ViewSubmitStatisticsClarify
总Time Limit: 2000ms Memory Limit: 131072kB
Description
In our OI life (or ACM life), tree is regarded as an important part.
 
Every node has only one father and lots of sons. When we would like to create a tree, we can use the following algorithm:
 
 
 
We define the depth of tree. For the node i, Depth[i] = Depth[Father[i]]+1. And Depth[1] = 1.
 
One day, this algorithm interests Picks. Now the problem he need to face is that when the random function is fair enough with a number N which means the largest number of nodes; how much the expected depth of the nodes is ?
 
 
数据范围
 
 
 
 
评测方式
For each test point, you will pass it if and only if :
 
 
Input
There are multiple tests ( no more than 10 ). For each test:
 
The first line contains a number: N.
Output
For each test:
 
The answer.
Sample Input
1
2
Sample Output
1.0000000000
1.2500000000
Hint
When Nodes = 1, the expected depth of the nodes is 1.0.
 
When Nodes = 2, the expected depth of the nodes is 1.5.
 
The probability of Nodes = 1 is 0.5. So is the probability of Nodes = 2.
题目大意:开始题目理解错了,一直以为是二叉树,然后推公式打表。。。而其实题目中说的意思是每加入一个点,这个点可能会成为已经存在的任何一个结点的孩子。xiaohao也没搞懂题意,竟然直接试公式1+1/(2*2)+1/(3*3)。后来题目懂了之后直接推公式,题目所求是<=n个结点的所有结点的平均深度。
 
解题思路:
 
然后如果n很大的话,就可以直接用极限公式。而欧拉常数R=∑(1/k)-ln(n);
 
Ac代码:
 
#include<iostream>  
#include<cstring>  
#include<string>  
#include<cmath>  
#include<cstdio>  
#include<algorithm>  
using namespace std;  
  
  
//r=lim(∑(1/k)-ln(n))  称为欧拉常数  
double f[1000005];  
int main()  
{  
    long long p,i;  
    double r;  //r是欧拉常数,近似值约为0.57721566490153286060651209  
    f[1]=1;  
    for(i=2;i<=1000000;i++)  
        f[i]=f[i-1]+1.0/i;  
    r=f[1000000]-log(1000000);  
    //cout<<r<<endl;  
    while(~scanf("%lld",&p))  
    {  
        if(p<=1000000)  
            printf("%f\n",(p+1)*f[p]/double(p)-1);  
        else  
            printf("%f\n",(p+1)*(log(p+1)+r)/double(p)-1);  
    }  
    return 0;  
}  

 

 
补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,