Activation
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1628 Accepted Submission(s): 621
Problem Description
After 4 years' waiting, the game "Chinese Paladin 5" finally comes out. Tomato is a crazy fan, and luckily he got the first release. Now he is at home, ready to begin his journey.
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.
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.
Input
There are no more than 40 test cases. Each case in one line, contains three integers and four real numbers: N, M (1 <= M <= N <= 2000), K (K >= 1), p1, p2, p3, p4 (0 <= p1, p2, p3, p4 <= 1, p1 + p2 + p3 + p4 = 1), indicating there are N guys in the queue (the positions are numbered from 1 to N), and at the beginning Tomato is at the Mth position, with the probability p1, p2, p3, p4 mentioned above.
Output
A real number in one line for each case, the probability that the ugly thing happens.
The answer should be rounded to 5 digits after the decimal point.
The answer should be rounded to 5 digits after the decimal point.
Sample Input
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
Sample Output
0.30427 0.23280 0.90343
Source
Recommend
lcy
题意
N个人排队激活游戏,Tomato在第M个位置。
每次服务器处理第一个人的请求,有可能产生以下四种结果:
1、激活失败:队列不变,再次尝试激活队列中第一个人,可能性为p1;
2、连接失败:把队头的人放到队尾,可能性为p2;
3、激活成功:队头的人出队,可能性为p3;
4、无法连接服务器:队列不变,可能性为p4。
求当服务器无法连接时,Tomato处于队列前K个人的概率。
解析
求概率的期望,所以可以按求期望的方法来求。
用dp[i][j]表示Tomato在i个人的队列里处于第j位置时,服务器无法连接时处于前K个的概率的期望。期望可以分解为子期望的加权和,权为子期望发生的概率,分析可得:
j = 1 时:dp[i][1] = p1*dp[i][1] + p2*dp[i][i] + p4
2 <= j <=k 时:dp[i][j] = p1*dp[i][j] + p2*dp[i][j-1] + p3*dp[i-1][j-1] + p4;
k < j <= i 时:dp[i][j] = p1*dp[i][j] + p2*dp[i][j-1] + p3*dp[i-1][j-1];
可以化简为:
j = 1 时:dp[i][1] = p21*dp[i][i] + p41
2 <= j <=k 时:dp[i][j] = p21*dp[i][j-1] + p31*dp[i-1][j-1] + p41;
k < j <= i 时:dp[i][j] = p21*dp[i][j-1] + p31*dp[i-1][j-1];
其中
p21 = p2 / (1-p1)
p31 = p3 / (1-p1)
p41 = p4 / (1-p1)
可以从dp[1][]递推到dp[n][]。显然dp[1][1] = p41 / (1-p21)。
在计算dp[i][]的时候,dp[i-1][]就相当于常数了。然后根据上述分析可知,对于dp[i][1~i]可以得到i个方程,同时只有i个变量,就可以解出dp[i][1~i]了。(PS:我当时在这纠结了好一会儿,跟我一样的数学渣们赶紧去补补数学吧)
另外还有一个需要注意的点:p4 < 1e-5 时直接输出0.00000,不然会wa。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
int n,m,k;
double p1,p2,p3,p4;
double dp[2010][2010], pp[2010];
int main()
{
while(~scanf("%d%d%d%lf%lf%lf%lf",&n,&m,&k,&p1,&p2,&p3,&p4))
{
if(p1 > 1.0-(1e-8) || p4 < 1e-5)
{
printf("0.00000\n");
continue;
}
memset(dp,0,sizeof(dp));
double p21 = p2/(1-p1);
double p31 = p3/(1-p1);
double p41 = p4/(1-p1);
pp[0] = 1.0;
for(int i = 1; i <= n; i++) pp[i] = pp[i-1]*p21;
dp[1][1] = p41/(1-p21);
for(int i = 2; i <= n; i++)
{
double tmp = p41*pp[i-1];
for(int j = 2; j <= k && j <= i; j++) tmp += (p31*dp[i-1][j-1]+p41)*pp[i-j];
for(int j = k+1; j <= i; j++) tmp += p31*dp[i-1][j-1]*pp[i-j];
dp[i][i] = tmp/(1-pp[i]);
dp[i][1] = p21*dp[i][i]+p41;
for(int j = 2; j <= k && j <= i; j++) dp[i][j] = dp[i][j-1]*p21+p31*dp[i-1][j-1]+p41;
for(int j = k+1; j < i; j++) dp[i][j] = dp[i][j-1]*p21 + p31*dp[i-1][j-1];
}
printf("%.5f\n",dp[n][m]);
}
return 0;
}