Batting Practice LightOJ - 1408
连续击球,击不中的概率是 p p p,连续击中 k 1 k_1 k1 次时或连续 k 2 k_2 k2 次都没有击中时停止击球,问停止击球时的期望击球数。
设 f ( x ) f(x) f(x) 为已经连续击中 x x x 次后停止击球的期望击球数。
设 g ( x ) g(x) g(x) 为已经连续失击 x x x 次后停止击球的期望击球数。
则 f ( k 1 ) = g ( k 2 ) = 0 f(k_1)=g(k_2)=0 f(k1)=g(k2)=0 ,对于 x ≤ k 1 x\le k_1 x≤k1:
f ( x ) = ( 1 − p ) f ( x + 1 ) + p g ( 1 ) + 1 f ( x ) + b = ( 1 − p ) ( f ( x + 1 ) + b ) f ( x ) = ( 1 − p ) f ( x + 1 ) − p b b = − g ( 1 ) − 1 p f ( x ) + b = ( 1 − p ) k 1 − x ( f ( k 1 ) + b ) = b ( 1 − p ) k 1 − x f ( 1 ) = b ( 1 − p ) k 1 − 1 − b f ( 1 ) = ( g ( 1 ) + 1 p ) ( 1 − ( 1 − p ) k 1 − 1 ) \begin{aligned} f(x)&=(1-p)f(x+1)+pg(1)+1\\ f(x)+b&=(1-p)(f(x+1)+b)\\ f(x)&=(1-p)f(x+1)-pb\\ b&=-g(1)-\frac{1}{p}\\ f(x)+b&=(1-p)^{k_1-x}(f(k_1)+b)\\ &=b(1-p)^{k_1-x}\\ f(1)&=b(1-p)^{k_1-1}-b\\ f(1)&=(g(1)+\frac{1}{p})(1-(1-p)^{k_1-1}) \end{aligned} f(x)f(x)+bf(x)bf(x)+bf(1)f(1)=(1−p)f(x+1)+pg(1)+1=(1−p)(f(x+1)+b)=(1−p)f(x+1)−pb=−g(1)−p1=(1−p)k1−x(f(k1)+b)=b(1−p)k1−x=b(1−p)k1−1−b=(g(1)+p1)(1−(1−p)k1−1)
对于 x ≤ k 2 x\le k_2 x≤k2:
g ( x ) = p g ( x + 1 ) + ( 1 − p ) f ( 1 ) + 1 g ( x ) + a = p ( g ( x + 1 ) + a ) g ( x ) = p g ( x + 1 ) + a ( p − 1 ) a = − f ( 1 ) − 1 1 − p g ( x ) + a = p k 2 − x ( g ( k 2 ) + a ) = a p k 2 − x g ( 1 ) = a p k 2 − 1 − a g ( 1 ) = ( f ( 1 ) + 1 1 − p ) ( 1 − p k 2 − 1 ) \begin{aligned} g(x)&=pg(x+1)+(1-p)f(1)+1\\ g(x)+a&=p(g(x+1)+a)\\ g(x)&=pg(x+1)+a(p-1)\\ a&=-f(1)-\frac{1}{1-p}\\ g(x)+a&=p^{k_2-x}(g(k_2)+a)\\ &=ap^{k_2-x}\\ g(1)&=ap^{k_2-1}-a\\ g(1)&=(f(1)+\frac{1}{1-p})(1-p^{k_2-1}) \end{aligned} g(x)g(x)+ag(x)ag(x)+ag(1)g(1)=pg(x+1)+(1−p)f(1)+1=p(g(x+1)+a)=pg(x+1)+a(p−1)=−f(1)−1−p1=pk2−x(g(k2)+a)=apk2−x=apk2−1−a=(f(1)+1−p1)(1−pk2−1)
然后解出来 f ( 1 ) , g ( 1 ) f(1),g(1) f(1),g(1),如下:
f ( 1 ) = ( ( f ( 1 ) + 1 1 − p ) ( 1 − p k 2 − 1 ) + 1 p ) ( 1 − ( 1 − p ) k 1 − 1 ) f ( 1 ) = ( ( f ( 1 ) + 1 q ) ( 1 − p k 2 − 1 ) + 1 p ) ( 1 − q k 1 − 1 ) ( 1 − ( 1 − p k 2 − 1 ) ( 1 − q k 1 − 1 ) ) f ( 1 ) = 1 q ( 1 − p k 2 − 1 ) ( 1 − q k 1 − 1 ) + 1 p ( 1 − q k 1 − 1 ) f ( 1 ) = p k q k p + q q k p q ( 1 − p k q k ) = p k q k / q + q k / p 1 − p k q k g ( 1 ) = ( f ( 1 ) + 1 q ) p k = p k q k q + p p k p q ( 1 − p k q k ) = p k q k / p + p k / q 1 − p k q k \begin{aligned} f(1)&=((f(1)+\frac{1}{1-p})(1-p^{k_2-1})+\frac{1}{p})(1-(1-p)^{k_1-1})\\ f(1)&=((f(1)+\frac{1}{q})(1-p^{k_2-1})+\frac{1}{p})(1-q^{k_1-1})\\ (1-(1-p^{k_2-1})(1-q^{k_1-1}))f(1)&=\frac{1}{q}(1-p^{k_2-1})(1-q^{k_1-1})+\frac{1}{p}(1-q^{k_1-1})\\ f(1)&=\frac{p_kq_kp+qq_k}{pq(1-p_kq_k)}=\frac{p_kq_k/q+q_k/p}{1-p_kq_k}\\ g(1)&=(f(1)+\frac{1}{q})p_k\\ &=\frac{p_kq_kq+pp_k}{pq(1-p_kq_k)}=\frac{p_kq_k/p+p_k/q}{1-p_kq_k}\\ \end{aligned} f(1)f(1)(1−(1−pk2−1)(1−qk1−1))f(1)f(1)g(1)=((f(1)+1−p1)(1−pk2−1)+p1)(1−(1−p)k1−1)=((f(1)+q1)(1−pk2−1)+p1)(1−qk1−1)=q1(1−pk2−1)(1−qk1−1)+p1(1−qk1−1)=pq(1−pkqk)pkqkp+qqk=1−pkqkpkqk/q+qk/p=(f(1)+q1)pk=pq(1−pkqk)pkqkq+ppk=1−pkqkpkqk/p+pk/q
然后结果为 r e s = ( 1 − p ) f ( 1 ) + p g ( 1 ) + 1 res=(1-p)f(1)+pg(1)+1 res=(1−p)f(1)+pg(1)+1
代码如下:
#include<iostream>
#include<cstdio>
#include<cmath>
//#define WINE
#define eps 1e-6
using namespace std;
int T,iCase,k1,k2;
double p,q,pk,qk,f1,g1;
int main(){
#ifdef WINE
freopen("data.in","r",stdin);
#endif
scanf("%d",&T);
while(T--){
scanf("%lf%d%d",&p,&k1,&k2);
q=1-p;
if(p<eps){
printf("Case %d: %d\n",++iCase,k1);
continue;
}
if(q<eps){
printf("Case %d: %d\n",++iCase,k2);
continue;
}
pk=1-pow(p,k2-1);qk=1-pow(q,k1-1);
f1=(pk*qk/q+qk/p)/(1-pk*qk);
g1=(pk*qk/p+pk/q)/(1-pk*qk);
printf("Case %d: %.8lf\n",++iCase,q*f1+p*g1+1);
}
return 0;
}