Description
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
Output
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
题意如下
有1到n个玩家在服务器队列中等待进入游戏
每次服务器会有以下的4种情况
i) 服务器故障导致什么都没发生
ii) 当前玩家被转移到队列最后面
iii) 成功登陆游戏
iiii) 服务器奔溃 所有人还在队列里
对应的概率 为 p1、p2、p3,p4
问当服务器奔溃时候在玩家A前面的人不超过k-1个人的概率
·规定 dp【i,j】 表示服务器奔溃时候队列有i个人 A现在在j位置
那么当j==1时候
可以知道其中一种情况是 dp【i,j】=dp【i,j】*p1+dp【i,i】*p2+p4;
j<k的时候 dp【i,j】=dp【i,j】*p1+dp【i,j】*p2+dp【i-1,j-1】*p3+p4;
j>=k的时候 dp【i,j】=dp【i,j】*p1+dp【i,j】*p2+dp【i-1,j-1】*p3;
因为p1的情况会出现无限循环
然后 根据 级数可以把这种情况转化为1/(1-p1)
所以我们令 p21=p2/(1-p1),p31=p3/(1-p1),p41=p4/(1-p1);
所以状态转移方程可以转化成
j==1 dp【i,j】=dp【i,i】*p21+p41;
j<k的时候dp【i,j】=dp【i,j】*p21+dp【i-1,j-1】*p31+p41;
j>=k的时候 dp【i,j】=dp【i,j】*p21+dp【i-1,j-1】*p31;
可以循环i=1->n 递推求解dp[i].在求解dp[i]的时候dp[i-1]就相当于常数了。
在求解dp【i,1~i】时等到下列i个方程
j==1: dp【i,1】=dp【i,i】*p21+c【1】;
j<=k: dp【i,j】 =dp【i,j-1】*p21+c【j】;
k<j=i: dp【i,j】 =dp【i,j】+c【j】;
其中c[j]都是常数了。上述方程可以解出dp[i]了。
首先是迭代得到 dp[i][i].然后再代入就可以得到所有的dp[i]了。
貌似后台改数据了 需要用滚动数组来优化空间
ACcode:
#include<stdio.h>
#define maxn 2020
#define eps 1e-5
double c[maxn];
double pp[maxn];
double dp[2][maxn];
int main(){
int n,m,k;
double p1,p2,p3,p4;
while(~scanf("%d%d%d%lf%lf%lf%lf",&n,&m,&k,&p1,&p2,&p3,&p4)){
if(p4<eps){puts("0.00000");continue;}
double p21=p2/(1-p1),p31=p3/(1-p1),p41=p4/(1-p1);
pp[0]=1.0;
for(int i=1;i<=n;i++) pp[i]=p21*pp[i-1];
dp[1][1]=p41/(1-p21);
c[1]=p41;
for(int i=2;i<=n;i++){
for(int j=2;j<=k;j++)c[j]=p31*dp[(i+1)&1][j-1]+p41;
for(int j=k+1;j<=i;j++) c[j]=p31*dp[(i+1)&1][j-1];
double tmp=c[1]*pp[i-1];
for(int j=2;j<=k;j++)tmp+=c[j]*pp[i-j];
for(int j=k+1;j<=i;j++)tmp+=c[j]*pp[i-j];
dp[i&1][i]=tmp/(1-pp[i]);
dp[i&1][1]=p21*dp[i&1][i]+c[1];
for(int j=2;j<i;j++)dp[i&1][j]=p21*dp[i&1][j-1]+c[j];
}
printf("%.5lf\n",dp[n&1][m]);
}
return 0;
}