After 4 years' waiting, the game "Chinese Paladin 5" finally comes out. Tomato is a
crazyfan,andluckilyhegotthefirstrelease.Nowheisathome,readytobeginhisjourney.
Butbeforestartingthegame,hemustfirstactivatetheproductontheofficialsite.Thereare
toomanypassionate fans thatthe activationserver cannot dealwith allthe requestsat the
sametime,soalltheplayersmustwaitinqueue.Eachtime,theserverdealswiththerequest
of the first player in the queue, and the result may be one of the following, each has a
probability:
1. Activation failed
1. Activation failed
11.. AAccttiivvaattiioonn ffaaiilleedd: This happens with the probability of p1. The queue remains
unchangedandtheserverwilltrytodealwiththesamerequestthenexttime.
2.Connectionfailed
2.Connectionfailed
22..CCoonnnneeccttiioonnffaaiilleedd:Thishappenswiththeprobabilityofp2.Somethingjusthappened
andthefirstplayerinqueuelosthisconnectionwiththeserver.Theserverwill thenremove
hisrequestfromthequeue.Afterthat,theplayerwill immediatelyconnecttotheserveragain
andstartsqueuingatthetailofthequeue.
3.Activationsucceeded
3.Activationsucceeded
33..AAccttiivvaattiioonnssuucccceeeeddeedd: Thishappenswiththeprobability ofp3.Congratulations,the
playerwill leavethequeueandenjoythegamehimself.
4. Service unavailable
4. Service unavailable
44.. SSeerrvviiccee uunnaavvaaiillaabbllee: This happens with the probability of p4. Something just
happened and the server is down. The website must shutdown the server at once.All the
requeststhatarestillinthequeuewill neverbedealt.
Tomatothinks it sucks if theserver is down while heis still waiting in the queue and
thereareno morethanK-1guysbeforehim.Andhewantstoknowtheprobabilitythatthis
uglythinghappens.
Tomakeit clear,wesaythreethingsmayhappentoTomato:hesucceededactivatingthe
game;theserverisdownwhileheisinthequeueandtherearenomorethanK-1guysbefore
him; the server is downwhile heis in the queue andthere areat least Kguys beforehim.
Nowyouaretocalculatetheprobabilityofthesecondthing.
Input
Input
IInnppuutt
Therearenomorethan40test cases.Eachcaseinoneline,containsthreeintegersand
fourrealnumbers:N,M(1<=M<=N<=2000),K(K>=1),p1,p2,p3,p4(0<=p1,p2,p3,
p4<=1,p1+p2+p3+p4=1),indicatingthereareNguysinthequeue(thepositionsare
numbered from 1 to N), and at the beginning Tomato is at the Mth position, with the
probabilityp1,p2,p3,p4mentionedabove.
Output
Output
OOuuttppuutt
Arealnumberinonelineforeachcase,theprobabilitythattheuglythinghappens.
Theanswershouldberoundedto5digitsafterthedecimalpoint.
SampleInput
SampleInput
SSaammpplleeIInnppuutt
2210.10.20.30.4
3210.40.30.20.1
4230.160.160.160.52
SampleOutput
SampleOutput
SSaammpplleeOOuuttppuutt
0.30427
0.23280
0.90343
//f[i][j]=p1*f[i][j]+p2*f[i][j-1]+p3*f[i-1][j-1]+p4*(j<=k)
//f[n][n]=...+p2*f[n][n-1]+..
//f[n][1]=...+p2*f[n][m]+..
//f[n][2]=...+p2*f[n][1]+..p3*f[n-1][1]
//f[n][n-1]=...+p2*f[n][n-2]+..+p3*f[n-1][n-3]
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,k;
double p1,p2,p3,p4;
double f[2100][2100];
//考虑1的情况
bool equ(double a,double b)
{
return fabs(a-b)<1e-8;
}
int main()
{
while(scanf("%d%d%d%lf%lf%lf%lf",&n,&m,&k,&p1,&p2,&p3,&p4)==7)
{
if(equ(p4,0))
{
printf("0.00000\n");
continue;
}
memset(f,0,sizeof(f));
f[1][1]=p4*(1<=k)/(1-p1-p2);//?
for(int i=2;i<=n;i++)
{
double a=p2,b=p4*(1<=k);
double tmp=p2/(1-p1);//?
if(i==3&&b==0.1)
{
b*=tmp;b+=p3*f[2][1];
cout<<b<<endl;
b=p4*(1<=k);
}
for(int j=2;j<=i-1;j++)
{
a*=tmp,b*=tmp;b+=p3*f[i-1][j-1]+p4*(j<=k);
}
a*=tmp,b*=tmp,b+=p3*f[i-1][i-1]+p4*(i<=k);
f[i][i]=b/(1-p1-a);//?
f[i][1]=(p2*f[i][i]+p4*(1<=k))/(1-p1);
for(int j=2;j<=i-1;j++)
{
f[i][j]=(p2*f[i][j-1]+p3*f[i-1][j-1]+p4*(j<=k))/(1-p1);//?
}
}
printf("%.5lf\n",f[n][m]);
}
return 0;
}