Activation
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3977 Accepted Submission(s): 1337
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.
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.
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
解题思路摘自kuangbin博客
题意:有n个人排队等着在官网上激活游戏。Tomato排在第m个。
对于队列中的第一个人。有一下情况:
1、激活失败,留在队列中等待下一次激活(概率为p1)
2、失去连接,出队列,然后排在队列的最后(概率为p2)
3、激活成功,离开队列(概率为p3)
4、服务器瘫痪,服务器停止激活,所有人都无法激活了。
求服务器瘫痪时Tomato在队列中的位置<=k的概率
题解:概率DP
设dp[i][j]表示i个人排队,Tomato排在第j个位置,达到目标状态的概率(j<=i)
dp[n][m]就是所求
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==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 其中:
p21=p2/(1-p1);
p31=p3/(1-p1)
p41=p4/(1-p1)
可以循环i=1->n 递推求解dp[i][1~i].在求解dp[i][1~i]的时候dp[i-1][1~i-1]就相当于常数了。
j==1: c[j]=p41;
2<=j<=k: c[j]=p31*dp[i-1][j-1]+p41;
k 在求解dp[i][1~i]时等到下列i个方程
j==1: dp[i][1]=p21*dp[i][i]+c[1];
2<=j<=k: dp[i][j]=p21*dp[i][j-1]+c[j];
k 其中c[j]都是常数了。上述方程可以解出dp[i][1~i]了。
由于n太大不能用高斯消元(实际上也不需要用),我们首先化简迭代式:
dp[i][i] = p21dp[i][j-1]+c[j]
= p21(p21dp[i][j-2]+c[j-1])+c[j]
= p21(p21(p21dp[i][j-3]+c[j-2])+c[j-1])+c[j]
....
=p21^(i-1)dp[i][1]+p21^(i-2)c[2]+...+p21c[i-1]+c[i]
=p21^(i-1)(p21dp[i][i]+c[1])+p21^(i-2)c[2]+...+p21c[i-1]+c[i]
=p21^idp[i][i]+p21^(i-1)c[1]+p21^(i-2)c[2]+...+p21c[i-1]+c[i]
这样就得到了只含dp[i][i]的等式,其他全为常数,因此可以解出dp[i][i]
(也是通过这题我知道不一定所有方程一定要高斯消元求解)
然后再代入计算dp[i][1],这样就可以把所有dp[i][j]计算出来了。
有2点要注意的:
①杭电内存给的很小,不能直接开2000*2000的数组,要用滚动数组来写
②特判一下p4,应该直接输出0,这样就不会崩溃了
#include
using namespace std;
const int MX = 2e3 + 5;
double dp[2][MX],c[MX];
int main(){
int n,m,k;
//freopen("in.txt","r",stdin);
while(~scanf("%d%d%d",&n,&m,&k)){
double p1,p2,p3,p4;
scanf("%lf%lf%lf%lf",&p1,&p2,&p3,&p4);
if(p4<1e-5){
printf("0.00000\n");
continue;
}
dp[1][1]=p4/(1-p2-p1);
double p21=p2/(1-p1);
double p31=p3/(1-p1);
double p41=p4/(1-p1);
int cur=1,pre=0;
for(int i=2;i<=n;i++){
cur^=1;pre^=1;
c[1]=p41;
for(int j=2;j<=k;j++) c[j]=p31*dp[pre][j-1]+p41;
for(int j=k+1;j<=i;j++) c[j]=p31*dp[pre][j-1];
double p=1;
dp[cur][i]=0;
for(int j=i;j>=1;j--) {
dp[cur][i]+=p*c[j];
p*=p21;
}
dp[cur][i]/=(1-p);
dp[cur][1]=p21*dp[cur][i]+c[1];
for(int j=2;j