1. 程式人生 > >解題報告 之 HDU 4089 Activation

解題報告 之 HDU 4089 Activation

解題報告 之 HDU 4089 Activation


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


     
      0.30427 0.23280 0.90343 
      
     
    
      
     
    
   
  

Source

2011 Asia Beijing Regional Contest
題目大意:有N個人排隊以啟用遊戲,而叔叔站是第M個人。大家依次啟用,當前第一個人有p1的概率啟用失敗,則他還是排在隊首等待下一次啟用;有p2的概率連線失敗,則他到隊尾重新排隊;有p3啟用成功,則他離開隊伍;有p4伺服器崩潰,則整個過程結束。問當伺服器崩潰時,叔叔前面的人少於K個的概率。
分析:一道較難也很有意思的概率DP。用dp[i][j]表示有i個人,叔叔在第j個時,達到目標情況的概率。那麼容易寫出狀態轉移方程: j == 1,            dp[i][j](dp[i][1])= p1* dp[i][j]+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+1<=j<=i,      dp[i][j] = p1*dp[i][j] + p2*dp[i][j-1]+p3*dp[i-1][j-1]
移項後得到: j == 1,            dp[i][j] = p2/(1-p1)*dp[i][i] + p4/(1-p1) 2<=j<=K,       dp[i][j] = p2/(1-p1)*dp[i][j-1]+p3/(1-p1)*dp[i-1][j-1]+p4/(1-p1) K+1<=j<=i,      dp[i][j] = p2/(1-p1)*dp[i][j-1]+p3/(1-p1)*dp[i-1][j-1]
令:p21=p2/(1-p1) , p31=p3/(1-p1)  , p41=p4/(1-p1)  j == 1,            dp[i][j] =p21 *dp[i][i] + p41  -->  c[j]=p41 2<=j<=K,       dp[i][j] = p21*dp[i][j-1]+p31*dp[i-1][j-1]+p41  -->  c[j]=dp[i-1][j-1]+p41 K+1<=j<=i,      dp[i][j] = p21*dp[i][j-1]+p31*dp[i-1][j-1]  -->  c[j]=p31*dp[i-1][j-1] (c[j]的意思是,當我們處理 i 時,對於每個 j 的常數項,或者是常數,或者是i-1行的與 i 行無關)
至於我們大致看出了遞推的趨勢,但有一個致命的問題,就是dp[i][1]是由dp[i][i]表示的。 所以我們需要先求出dp[i][1],再進行遞推。 這裡我們採用方程組的觀點,注意dp[i][i]可以用dp[i][j-1]和一些常數表示,而 dp[i][j-1] 又可以用  dp[i][j-2]和常數表示, ……, 一直跌待下去最終可以用dp[i][1]和常數表示dp[i][1],即可以解出dp[i][1]。
由於每一次迭代都要乘上一個p21,所以常數項的係數可以根據迭代次數算出來。 ** 注意不能開二維陣列,會爆記憶體,所以必須要採用滾動陣列。 ** 需要特判 p4 < 1e-5 的情況,此時直接輸出0,不然分母會接近於0而被判為Na值導致超時(這個點我也不是很懂。。。)
上程式碼:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

const int MAXN = 2010;
double dp[3][MAXN];
double pp[MAXN], c[MAXN];
//pp表示p21^p的值,後面迭代求dp[i][i]用;c表示位置為j時的常數項,每一步都要遞推更新c。

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 ) == 7)
	{
		if(p4<1e-5)
		{
			printf( "0.00000\n" );
			continue;
		}

		double p21 = p2 / (1.0 - p1), p31 = p3 / (1.0 - p1), p41 = p4 / (1.0 - p1);
		pp[0] = 1.0;
		for(int i = 1; i <= n; i++) pp[i] = pp[i - 1] * p21;

		c[1] = p41;
		dp[1][1] = p41 / (1.0 - p21);

		for(int i = 2; i <= n; i++)
		{
			//更新c陣列
			for(int j = 2; j <= k; j++) c[j] = p31*dp[1][j - 1] + p41;
			for(int j = k + 1; j <= i; j++) c[j] = p31*dp[1][j - 1];

			//迭代法求dp[i][i]
			double tem = c[1] * pp[i - 1];
			for(int j = 2; j <= k; j++) tem += c[j] * pp[i - j];
			for(int j = k + 1; j <= i; j++) tem += c[j] * pp[i - j];
			dp[2][i] = tem / (1.0 - pp[i]);

			//遞推求dp[i][..]
			dp[2][1] = p21*dp[2][i] + c[1];
			for(int j = 2; j <= i; j++) dp[2][j] = p21*dp[2][j - 1] + c[j];

			for(int j = 0; j <= n; j++)
			{
				dp[1][j] = dp[2][j];
			}
		}
		printf( "%.5lf\n", dp[1][m] );
	}
	return 0;
}

就是醬紫的,,,叔叔叔叔靠你辣。陶神陶神靠你辣~ kuangbin 大法好。