csuoj2131 Travel Frog(期望dp)

Description

As is known to all, the Travel Frog is quite popular with young people.

Also, Wells had download the app for leisure time. But soon Wells was bored with this game for the empty scene.

So Wells had cracked this game, which informed Wells that the Travel Frog's location obey following rules.

First, when click and run the app, the system will give an number N, which means the frog are N steps away from home(you can consider the track of frog is a straight line), and the home is at 1.

Second, for each second, if frog is at k, the frog can take a step of length of 0,1, 2, 3...k-1, of equal possibility. For instance, when frog is at 3, for the next second, the frog may be stay at 3, or 2, or 1 for 33.3333……% possibility. And it will repeat this until the frog come back home.

Wells know that it's quite complex to calculate the actual second for each time of the frog return, but the obstinate Wells still want to know the expected time(or average time to wait) of the frogs come back home.

Input

Input contains multiple lines, each line contain a number N(N<=109)N(N<=109) as described above. The number of test cases is no more than 200.

Output

For each test case, print a single integer on its own line denoting the number of ships capable of reaching the expedition site. The numbers should be accurate to an absolute or relative error of at most 10−6.

Sample Input

2
3
5

Sample Output

2.000000000
2.500000000
3.083333333


题目大意:一条长度为n的路,青蛙一开始在n,家在1。若青蛙在k,则每次只能跳0~k-1步,求到家的所跳次数的数学期望,

                 (n<=1000000000)

分析:概率dp从后往前推。

         dp[1]=0;

         dp[2]=2;

         dp[n]=(dp[1]+dp[2]+....+dp[n]+n)/n;

         dp[n]=(dp[1]+dp[2]+...+dp[n-1]+n-1+1)/(n-1)=dp[n-1]+1/(n-1);    (n>=3);

                  =2+1/2+1/3+1/4+....+1/(n-1);

          所以得调和级数dp[n]=(1+1/2+1/3+...+1/(n-1))+1;       

    代码:

       

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
double dp[1000010];
int main(){
    int i,n;
    dp[1]=0;
    dp[2]=2;
    for(i=2;i<=1000000;i++) dp[i]=dp[i-1]+1.0/(i-1);
    while(cin>>n){
        if(n<=1000000) printf("%.10f\n",dp[n]);
        else printf("%.10f\n",log(n-1)+0.57721566490+1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/guogai13/article/details/80558455