Time limit 1000 ms
Memory limit 65536 kB
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.
题目分析
本来还以为只是个普通期望。。没想到这么难。。也可能是我太菜了+_+
令
f
(
x
)
f(x)
f(x)为长度为
x
x
x的期望次数
若
x
≤
d
x\leq d
x≤d,则
f
(
x
)
=
0
f(x)=0
f(x)=0
若
x
≥
d
x\geq d
x≥d,则
f
(
x
)
=
1
+
1
x
(
∫
0
d
f
(
t
)
d
t
+
∫
d
x
f
(
t
)
d
t
)
f(x)=1+\frac{1}{x}(\ \int_0^df(t)dt\ +\ \int_d^xf(t)dt\ )
f(x)=1+x1( ∫0df(t)dt + ∫dxf(t)dt )
f
(
x
)
=
1
+
1
x
∫
d
x
f
(
t
)
d
t
f(x)=1+\frac{1}{x}\int_d^xf(t)dt
f(x)=1+x1∫dxf(t)dt
根据牛顿-莱布尼茨公式,设 F ′ ( x ) = f ( x ) F^{'}(x)=f(x) F′(x)=f(x)
f ( x ) = 1 + 1 x ( F ( x ) − F ( d ) ) f(x)=1+\frac{1}{x}(F(x)-F(d)) f(x)=1+x1(F(x)−F(d))
此时对 f ( x ) f(x) f(x)求导
f
′
(
x
)
=
x
F
′
(
x
)
−
F
(
x
)
+
F
(
d
)
x
2
f^{'}(x)=\frac{xF^{'}(x)-F(x)+F(d)}{x^2}
f′(x)=x2xF′(x)−F(x)+F(d)
f
′
(
x
)
=
x
f
(
x
)
−
∫
d
x
f
(
t
)
d
t
x
2
f^{'}(x)=\frac{xf(x)-\int_d^xf(t)dt}{x^2}
f′(x)=x2xf(x)−∫dxf(t)dt
f
′
(
x
)
=
x
f
(
x
)
−
x
(
f
(
x
)
−
1
)
x
2
=
1
x
f^{'}(x)=\frac{xf(x)-x(f(x)-1)}{x^2}=\frac{1}{x}
f′(x)=x2xf(x)−x(f(x)−1)=x1
得
f
(
x
)
=
ln
x
+
C
f(x)=\ln x+C
f(x)=lnx+C
同时取右极限时
f
(
d
)
=
ln
d
+
C
=
1
⇒
C
=
1
−
ln
d
f(d)=\ln d+C=1\Rightarrow C=1-\ln d
f(d)=lnd+C=1⇒C=1−lnd
代入上式
f
(
x
)
=
ln
x
d
+
1
f(x)=\ln \frac{x}{d}+1
f(x)=lndx+1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long lt;
typedef double dd;
int read()
{
int f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}
int main()
{
int T=read();
while(T--)
{
dd L,d;
scanf("%lf%lf",&L,&d);
if(L<=d) printf("0.000000\n");
else printf("%.6lf\n",log(L/d)+1.0);
}
return 0;
}