Pocky HDU - 5984 (求期望+推公式+调和级数)

Problem Description
Let’s talking about something of eating a pocky. Here is a Decorer Pocky, with colorful decorative stripes in the coating, of length L.
While the length of remaining pocky is longer than d, we perform the following procedure. We break the pocky at any point on it in an equal possibility and this will divide the remaining pocky into two parts. Take the left part and eat it. When it is not longer than d, we do not repeat this procedure.
Now we want to know the expected number of times we should repeat the procedure above. Round it to 6 decimal places behind the decimal point.

Input
The first line of input contains an integer N which is the number of test cases. Each of the N lines contains two float-numbers L and d respectively with at most 5 decimal places behind the decimal point where 1 ≤ d, L ≤ 150.

Output
For each test case, output the expected number of times rounded to 6 decimal places behind the decimal point in a line.

Sample Input
6
1.0 1.0
2.0 1.0
4.0 1.0
8.0 1.0
16.0 1.0
7.00 3.00

Sample Output
0.000000
1.693147
2.386294
3.079442
3.772589
1.847298

大致题意:给你一段长度为L的巧克力棒,然后你等概率的选取一个点,将其分成左右两部分,然后将左边那部分吃掉,如果剩下的右半部分大于长度d,那么继续上面操作,否则停止,问期望次数是多少?

思路:一开始的思路,因为所给你的长度L和d小数点后最多有5位,所以我们可以将其都乘上1e5,假设dp[i]表示此时剩余长度为i时期望数,那么dp[d]=0,dp[d+1]=1,当i小于d时dp[i]都为0,当i大于d时dp[i]=1/(i-1)*(dp[d+1]+dp[d+2]+…..+dp[i-1])+1,最后答案即为dp[L]。
but。。。这么求出来后答案的最后几位是错的,因为乘上1e5后得到的精度还是不够。。。。如果乘1e9的话就太慢了会超时。。。
我们可以推出通项公式 dp[d+i]=1/d+1/(d+1)+1/(d+2)+…+1/(d+i-1)+1
假设sum(n)=1/1+1/2+1/3+….+1/n,那么dp[L]=sum(L-1)-sum(d-1)+1
又因为1/1,1/2,1/3,…,1/n是调和数列,它们的调和级数为ln(n+1)+r(r为常量),
所以答案最后的答案dp[L]=ln(L)-ln(d)+1

代码如下

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;  
int main()
{
    int T;
    scanf("%d",&T);
    double l,d;
    while(T--)
    {
        scanf("%lf%lf",&l,&d);
        if(l<=d)
            printf("0.000000\n");
        else
        {
            printf("%.6lf\n",log(l)-log(d)+1);
        }
    }
    return 0; 
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值