HDU 5984 Pocky 【思维 数学】

来源:互联网 发布:js数组取前几个 编辑:程序博客网 时间:2024/06/05 07:27

Pocky

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 756    Accepted Submission(s): 424


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
61.0 1.02.0 1.04.0 1.08.0 1.016.0 1.07.00 3.00
 

Sample Output

0.0000001.6931472.3862943.0794423.7725891.847298
 

题意为给你一个长为L的蛋糕 一刀一刀随机切  切一刀后分为两部分 丢一部分 当剩下的那部分不长于d时停止 问这个过程预期要切几刀。
根据题意可以知道 在最开始分为两种情况 切或者不切 当d的值大于或者等于L时 不需要进行上述步骤 直接输出0即可  当没出现0的情况时  最少的预期也是要切1刀  所以我们将1提出来 
既然是随机下刀  那么就不能用传统方式计算期望 我们来分析样例 分别在提出1后 是0.69,1.38,2.07,2.77
因为ln2的值是0.69  而且我们又发现后面几个数分别是2*ln2  3*ln2  4*ln2  
ln2+ln2=ln2*2=ln4   ln4+ln2=ln8
在看样例输入  2 4 8 16 符合上述ln2的规律
所以我刚开始大胆的直接输出1+ln(l) 但是在最后一个样例出现差错 
按照上述规律 输出2.94 比正确输出高出1点多  所以我们要想办法减少输出的数并且同时不能影响之前的数据
分析ln()函数 需要减少括号内的值 而且之前的数据是带1的 d为1  那么正好l/d是不影响之前的输出值的 而且还能减少7 3 样例的输出  所以尝试输出1+ln(l/d)  成功
代码如下
#include<bits/stdc++.h>using namespace std;int main(){    int t;    scanf("%d",&t);    while(t--)    {        double l,d;        scanf("%lf%lf",&l,&d);        if(d>=l)            printf("0.000000\n");        else        {            printf("%.6lf\n",1+log(l/d));        }    }    return 0;}

原创粉丝点击