题干:
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.
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.
For each test case, output the expected number of times rounded to 6 decimal places behind the decimal point in a line.
样例:
输入: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
输出:0.000000
1.693147
2.386294
3.079442
3.772589
1.847298
思路:
我们有一个长为L的pokey(是杏子,幻视ing),现有一个程序,如果L>d,则在L上等可能的选择任意一点,吃掉左边,得到新的L,然后重复此程序,求程序执行次数的期望。
即长为L的木棍,当L>d时,每次等可能的去掉X(0<X<=L),求操作次数的期望。
设f(x)为长度为x的pokey的操作次数的期望。
则f(x)=0 (x<=d)
f(x)=1+
∫
0
d
f
(
t
)
d
t
/
x
\int_{0}^{d} f(t) \,dt/x
∫0df(t)dt/x+
∫
d
x
f
(
t
)
d
t
/
x
\int_{d}^{x} f(t) \,dt/x
∫dxf(t)dt/x (x<=d) (因为长度小于等于d时就不再继续了,所以第一个积分值为0)
=1+(F(x)-F(d)) /x (因为取[d,x]是等可能的所有除以x)
则
f
(
x
)
˙
\dot{f(x)}
f(x)˙=
(
x
∗
F
(
x
)
˙
−
(
F
(
x
)
−
F
(
d
)
)
)
/
(
x
2
)
(x*\dot{F(x)}-(F(x)-F(d)))/(x^2)
(x∗F(x)˙−(F(x)−F(d)))/(x2) (求导)
=
(
x
∗
f
(
x
)
−
∫
d
x
f
(
t
)
d
t
)
/
(
x
2
)
(x*f(x)-\int_{d}^{x} f(t) \,dt)/(x^2)
(x∗f(x)−∫dxf(t)dt)/(x2)
=
(
x
∗
f
(
x
)
−
x
∗
f
(
x
)
+
x
)
/
(
x
2
)
(x*f(x)-x*f(x)+x)/(x^2)
(x∗f(x)−x∗f(x)+x)/(x2)
=1/x
所以
f
(
x
)
=
l
n
x
+
c
f(x)=lnx+c
f(x)=lnx+c
lim
x
→
+
d
f
(
x
)
\lim_{x\rightarrow+d} f(x)
limx→+df(x)=1(因为L=d的时执行)
所以c=
1
−
l
n
d
1-lnd
1−lnd
则得到最终答案
f
(
x
)
=
l
n
x
−
l
n
d
+
1
f(x)=lnx-lnd+1
f(x)=lnx−lnd+1
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t,n,a,b;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
long long ans=0;
for(int i=0;i<n;i++){
scanf("%d%d",&a,&b);
ans+=(long long)a*b;
}
printf("%lld\n",ans);
}
return 0;
}