But before starting the game, he must first activate the product on the official site. There are too many passionate fans that the activation server cannot deal with all the requests at the same time, so all the players must wait in queue. Each time, the server deals with the request of the first player in the queue, and the result may be one of the following, each has a probability:
1. Activation failed: This happens with the probability of p1. The queue remains unchanged and the server will try to deal with the same request the next time.
2. Connection failed: This happens with the probability of p2. Something just happened and the first player in queue lost his connection with the server. The server will then remove his request from the queue. After that, the player will immediately connect to the server again and starts queuing at the tail of the queue.
3. Activation succeeded: This happens with the probability of p3. Congratulations, the player will leave the queue and enjoy the game himself.
4. Service unavailable: 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 requests that are still in the queue will never be dealt.
Tomato thinks it sucks if the server is down while he is still waiting in the queue and there are no more than K-1 guys before him. And he wants to know the probability that this ugly thing happens.
To make it clear, we say three things may happen to Tomato: he succeeded activating the game; the server is down while he is in the queue and there are no more than K-1 guys before him; the server is down while he is in the queue and there are at least K guys before him.
Now you are to calculate the probability of the second thing.
The answer should be rounded to 5 digits after the decimal point.
2 2 1 0.1 0.2 0.3 0.4 3 2 1 0.4 0.3 0.2 0.1 4 2 3 0.16 0.16 0.16 0.52
0.30427 0.23280 0.90343
题意:
有n个人排队激活游戏账号,每次会有四种情况发生。
1.激活失败: 队列保持不变,发生的概率为p1
2.连接失败: 队首重新排到队尾,队列长度不变,发生的概率为p2
3.激活成功: 队首移出队列,后面的元素都往前移一位,队列长度减1,发生的概率为p3
4.服务器崩溃: 不再提供服务。
刚开始有n个人在排队,Tomato在第m个位置,问最后服务器崩溃的时候Tomato在队列的前k个位置 发生的概率。
解答:
用dp[i][j]记录队列里面有i个人,Tomato在第j个位置的概率,那最后dp[n][m]即为所求。
dp[i][1]=p1*dp[i][1]+dp[i][i]*p2+p4
dp[i][j]=p1*dp[i][j] + dp[i][j-1]*p2+dp[i-1][j-1]*p3+p4; (j<=k)
dp[i][j]=p1*dp[i][j] + dp[i][j-1]*p2+dp[i-1][j-1]*p3;(j>k)
令
p21=p2/(1-p1);
p31=p3/(1-p1);
p41=p4/(1-p1);
dp[i][1]=p21*dp[i][1]+p41;
dp[i][j]=p21*dp[i][j-1]+dp[i-1][j-1]*p31+p41;(j<=k)
dp[i][j]=p21*dp[i][j-1]+dp[i-1][j-1]*p31;(j>k)
因为dp[i-1][j-1]在递推的时候可以解决
所以最后就转换成:
a[n]=An*a[n-1]+Cn;
a[n-1]=An-1 * a[n-2]+Cn-1;
...
a[2]=A2*a[1]+C2;
a[1]=A1*a[n]+C1;
通过迭代可以首先求出a[n],之后就可以求出所有的
代码:
# include<stdio.h>
# include<string.h>
# include<stdlib.h>
# include<math.h>
# define N 2005
# define eps 1e-10
double dp[N][N],c[N];
int main()
{
int i,j,n,m,K;
double p1,p2,p3,p4;
double p21,p31,p41;
double ans,sum;
while(scanf("%d%d%d",&n,&m,&K)!=EOF)
{
scanf("%lf%lf%lf%lf",&p1,&p2,&p3,&p4);
if(fabs(p4)<eps) {puts("0.00000");continue;}
p21=p2/(1-p1);
p31=p3/(1-p1);
p41=p4/(1-p1);
dp[1][1]=p4/(1-p1-p2);
for(i=2;i<=n;i++)
{
for(j=i;j>=2;j--)
{
c[j]=p31*dp[i-1][j-1];
if(j<=K) c[j]+=p41;
}
c[1]=p41;
ans=1;//系数
sum=0;//常数和
for(j=i;j>=1;j--)
{
sum+=ans*c[j];
ans*=p21;
}
dp[i][i]=sum/(1-ans);
dp[i][0]=dp[i][i];
for(j=1;j<i;j++)
dp[i][j]=p21*dp[i][j-1]+c[j];
}
printf("%.5lf\n",dp[n][m]);
}
return 0;
}