lightoj 1408 概率dp
作者:互联网
https://blog.csdn.net/moon_sky1999/article/details/98097470
博主在此,牛逼神犇
1 #include<bits/stdc++.h> 2 using namespace std; 3 const double eps = 1e-9; 4 int main(){ 5 int T,k1,k2; 6 double p; 7 scanf("%d",&T); 8 for(int cas=1;cas<=T;cas++){ 9 scanf("%lf",&p); 10 scanf("%d%d",&k1,&k2); 11 printf("Case %d: ",cas); 12 if(p<eps) printf("%d\n",k1); 13 else if(1-p<eps) printf("%d\n",k2); 14 else{ 15 double x1=1-pow(1-p,k1-1); 16 double x2=1-pow(p,k2-1); 17 double y1=x1/p; 18 double y2=x2/(1-p); 19 double a=(x1*y2+y1)/(1-x1*x2); 20 double b=(y1*x2+y2)/(1-x1*x2); 21 printf("%.7f\n",(1-p)*a+p*b+1); 22 } 23 } 24 return 0; 25 }
题意:一个人在击球,有p的概率集中,有(1-p)的概率击不中。如果能够连续击中x次将停止,连续不集中y次也将停止。问最终停止击球时击球次数的期望。
思路:设f[i]代表连续击中i次之后距离结束还剩的期望步数。g[i]代表连续不集中i次后距离结束的期望步数。可以列出下列方程:{f[i]=p∗f[i+1]+(1−p)∗g[1]+1g[i]=(1−p)∗g[i+1]+p∗f[1]+1 \left\{\begin{aligned}f[i]=p*f[i+1]+(1-p)*g[1]+1\\g[i]=(1-p)*g[i+1]+p*f[1]+1\end{aligned}\right.{ f[i]=p∗f[i+1]+(1−p)∗g[1]+1g[i]=(1−p)∗g[i+1]+p∗f[1]+1
边界条件:{f[x]=0g[y]=0 \left\{\begin{aligned}f[x]=0\\g[y]=0\end{aligned}\right.{ f[x]=0g[y]=0
答案:ans=p∗f[1]+(1−p)∗g[1] ans = p*f[1]+(1-p)*g[1]ans=p∗f[1]+(1−p)∗g[1]
推导过程:令:{AB=(1−p)∗g[1]+1=p∗f[1]+1 \left\{\begin{aligned}A &= (1-p)*g[1]+1\\B&=p*f[1]+1\end{aligned}\right.{ AB =(1−p)∗g[1]+1=p∗f[1]+1
则原式:{f[i]g[i]=p∗f[i+1]+A=(1−p)∗g[i+1]+B \left\{\begin{aligned}f[i] &= p*f[i+1]+A\\g[i]&=(1-p)*g[i+1]+B\end{aligned}\right.{ f[i]g[i] =p∗f[i+1]+A=(1−p)∗g[i+1]+B
求解f[1]和g[1]:f[1]=p∗f[2]+A=p∗(p∗f[3]+A)+A=p2∗f[3]+A∗(1+p)=px−1∗f[x]+A∗(1+p+p2+...+px−2)=0+A∗1−px−11−p=A∗1−px−11−p=(1−px−1)∗g[1]+1−px−11−p. \begin{aligned}f[1] &= p*f[2]+A\\ &= p*(p*f[3]+A)+A\\&= p^2*f[3]+A*(1+p)\\&=p^{x-1}*f[x]+A*(1+p+p^2+...+p^{x-2})\\&=0+A*\frac{1-p^{x-1}}{1-p}\\&=A*\frac{1-p^{x-1}}{1-p}\\&=(1-p^{x-1})*g[1]+\frac{1-p^{x-1}}{1-p}\end{aligned}.f[1] =p∗f[2]+A=p∗(p∗f[3]+A)+A=p 2 ∗f[3]+A∗(1+p)=p x−1 ∗f[x]+A∗(1+p+p 2 +...+p x−2 )=0+A∗ 1−p1−p x−1 =A∗ 1−p1−p x−1 =(1−p x−1 )∗g[1]+ 1−p1−p x−1 .
同理g[1]=[1−(1−p)y−1]∗f[1]+1−(1−p)y−1p g[1]=[1-(1-p)^{y-1}]*f[1]+\frac{1-(1-p)^{y-1}}{p}g[1]=[1−(1−p) y−1 ]∗f[1]+ p1−(1−p) y−1
令⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪CDEF=[1−(1−p)y−1]=1−(1−p)y−1p=(1−px−1)=1−px−11−p \left\{\begin{aligned}C&= [1-(1-p)^{y-1}]\\D&=\frac{1-(1-p)^{y-1}}{p}\\E&=(1-p^{x-1})\\F&=\frac{1-p^{x-1}}{1-p}\end{aligned}\right.⎩⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎧ CDEF =[1−(1−p) y−1 ]= p1−(1−p) y−1 =(1−p x−1 )= 1−p1−p x−1
则{g[1]f[1]=C∗f[1]+D=E∗g[1]+F \left\{\begin{aligned}g[1]&=C*f[1]+D\\f[1]&=E*g[1]+F\end{aligned}\right.{ g[1]f[1] =C∗f[1]+D=E∗g[1]+F
求得f[1]=DE+F1−CE f[1]=\frac{DE+F}{1-CE}f[1]= 1−CEDE+F
g[1]=C∗f[1]+D g[1]=C*f[1]+Dg[1]=C∗f[1]+D
带入ans即可。需要注意p=0或p=1时的情况。代码————————————————版权声明:本文为CSDN博主「Celestine_Jq」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/moon_sky1999/article/details/98097470
标签:begin,end,px,lightoj,1408,amp,aligned,frac,dp 来源: https://www.cnblogs.com/pangbi/p/11614130.html