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.
InputThe 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.
OutputFor 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.00Sample Output
0.000000 1.693147 2.386294 3.079442 3.772589 1.847298
分析:
题意:给定一个长度L的pocky,然后可以在任一点等概率地切割,切割后会把左边的部分吃掉,剩下右边的部分,
然后继续切割一直到剩下的长度小于d为止,问切割次数的期望值是?
假设切割当前长度为x的pocky时,期望值为f(x),如果x<=d,那么f(x)=0.
如果x>d,那么f(x)=1+f(1~d)+f(d~x),显然f(1~d)=0,即:f(x)=1+f(d~x)。
假设现在要切割d~x上的t点,切割t点的概率是1/x,切割该点的期望是f(t)/x,对t从d~x积分得f(d~x)=(1/x)*∫(d->x)f(t)dt.
那么f(x)=1+f(d~x)=1+(1/x)*∫(d->x)f(t)dt.
左右两边同时求导:f'(x)=(f(x)*x-∫(d->x)f(t)dt)/x^2①.又因为f(x)=1+(1/x)*∫(d->x)f(t)dt②.
联立①,②消去∫(d->x)f(t)dt得到f'(x)=1/x.即:f(x)=ln(x)+C,带入f(d)=0解得f(x)=ln(x/d)+1,根据题意得输入,x=L.
综上:
当L<=d时,输出0.000000.
否则,输出ln(L)-ln(d)+1.
AC code:
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { //freopen("input.txt","r",stdin); int t; scanf("%d",&t); while(t--) { double L,d; scanf("%lf%lf",&L,&d); if(L<=d) printf("%.6f\n",0.0); else printf("%.6f\n",1+log(L)-log(d)); } return 0; }View Code
END