1. 程式人生 > >Activation HDU - 4089(概率dp)

Activation HDU - 4089(概率dp)

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.

InputThere 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.OutputA 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
令 dp[i][j] 表示一共有 i 個人,並且 tomato 在第 j 個位置時,達到最終狀態的概率。
那麼有三種情況:
1.j == 1 : dp[i][j] = p1 * dp[i][j] + p2 * dp[i][i] + p4
2.2 <= j <= k : dp[i][j] = p1 * dp[i][j] + p2 * dp[i][j-1] + p3 * dp[i-1][j-1] + p4
3.k < j <= i : dp[i][j] = p1 * dp[i][j] + p2 * dp[i][j-1] + p3 * dp[i-1][j-1]
移項化簡得
1.j == 1 : dp[i][j] = p21 * dp[i][i] + p41
2.2 <= j <= k : dp[i][j] = p21 * dp[i][j-1] + p31 * dp[i-1][j-1] + p41
3.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).
此時當計算到 i 時, dp[i-1][] 的值已經全部求出來了,所以可以把後面部分看成常數
1.j == 1 : dp[i][j] = p21 * dp[i][i] + c[j] c[j] = p41
2.2 <= j <= k : dp[i][j] = p21 * dp[i][j-1] + c[j] c[j] = p31 * dp[i-1][j-1] + p41
3.k < j <= i : dp[i][j] = p21 * dp[i][j-1] + c[j] c[j] = p31 * dp[i-1][j-1]
然後 dp[i][i] 通過 2、3 式推到 dp[i][1], 然後在用 1 代入 dp[i][1], 求出 dp[i][i], 其式是
dp[i][i] = p21^(i) * dp[i][i] + Σ(p21^(i-x) * c[x])
然後就可以把 dp[i][] 的值都求出來,最後的答案就是dp[n][m]
  1 /*
  2           .
  3          ';;;;;.
  4         '!;;;;;;!;`
  5        '!;|&#@|;;;;!:
  6       `;;!&####@|;;;;!:
  7      .;;;!&@$$%|!;;;;;;!'.`:::::'.
  8      '!;;;;;;;;[email protected]###&|;;|%!;!$|;;;;|&&;.
  9      :!;;;;[email protected]
&%|;;;;;;;;;|!::!!:::;!$%;!$%` '!%&#########@$!:.
10 ;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$; 11 ;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%` 12 `!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&; 13 :@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@| 14 ;|::;;;;;;;;;;;;|&&$|;;[email protected]&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%. 15 `!!;;;;;;;!!!!;;;;;[email protected]@@&&&&&@$!;!%|;;;;!||!;;;;;!|%%%!;;%@|. 16 %&&$!;;;;;!;;;;;;;;;;;|$&&&&&&&&&@@%!%%;!||!;;;;;;;;;;;;;$##! 17 !%;;;;;;!%!:;;;;;;;;;;!$&&&&&&&&&&@##&%|||;;;!!||!;;;;;;;$&: 18 ':|@###%;:;;;;;;;;;;;;!%$&&&&&&@@$!;;;;;;;!!!;;;;;%&!;;|&%. 19 [email protected]|;;;;;;;;;;;;;;;;;;|%|$&&$%&&|;;;;;;;;;;;;!;;;;;!&@@&' 20 .:%#&!;;;;;;;;;;;;;;!%|$$%%&@%;;;;;;;;;;;;;;;;;;;!&@: 21 .%$;;;;;;;;;;;;;;;;;;|[email protected]&|;;;;;;;;;;;;;;;;;;;;%@%. 22 !&!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|@#; 23 `%$!;;;;;;;;;;;[email protected]|;;;;;;;;;;;;;;;;;;;;;;;;!%[email protected]#@|. 24 .|@%!;;;;;;;;;!$&%||;;;;;;;;;;;;;;;;;!%[email protected]#|. 25 ;&$!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;%#####|. 26 |##$|!;;;;;;::'':;;;;;;;;;;;;;!%[email protected]#@; 27 ;@&|;;;;;;;::'''''':;;;;;;;|$&@###@|` 28 .%##@|;;;;:::''''''''''::;!%&##$' 29 `$##@[email protected]@&|!!;;;:'''''::::;;;;;|&#%. 30 ;&@##&$%!;;;;;;::''''''''::;!|%[email protected]#@&@@: 31 .%@&$$|;;;;;;;;;;:'''':''''::;;;%@#@@#%. 32 :@##@###@$$$$$|;;:'''':;;!!;;;;;;!$#@@#$;` 33 `%@$$|;;;;;;;;:'''''''::;;;;|%$$|!!&###&' 34 |##&%!;;;;;::''''''''''''::;;;;;;;[email protected]&:`!' 35 :;[email protected]$|;;;;;;;::''''''''''':;;;;;;;;!%&@$: [email protected]#$' 36 |##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%: '%%!%&; 37 |&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||` '%$|!%&; 38 |@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&: .|%;:!&%` 39 [email protected]&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$; `%&%!!$&: 40 '$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##; !$!;:!$&: 41 |#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%' `%$|%%;|&$' 42 |&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&; .:%&$!;;;:[email protected]! 43 `%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@! 44 !&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%` 45 '%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%[email protected]@|' 46 .!%%&%'`|$; :|$#%|@#&;%#%. 47 */ 48 #include <map> 49 #include <set> 50 #include <list> 51 #include <ctime> 52 #include <cmath> 53 #include <stack> 54 #include <queue> 55 #include <string> 56 #include <vector> 57 #include <cstdio> 58 #include <bitset> 59 #include <cstdlib> 60 #include <cstring> 61 #include <iostream> 62 #include <algorithm> 63 #define lowbit(x) x & (-x) 64 #define mes(a, b) memset(a, b, sizeof a) 65 #define fi first 66 #define se second 67 #define pii pair<int, int> 68 #define INOPEN freopen("in.txt", "r", stdin) 69 #define OUTOPEN freopen("out.txt", "w", stdout) 70 71 typedef unsigned long long int ull; 72 typedef long long int ll; 73 const int maxn = 2e3 + 10; 74 const int maxm = 1e5 + 10; 75 const int mod = 1e9 + 7; 76 const ll INF = 1e18 + 100; 77 const int inf = 0x3f3f3f3f; 78 const double pi = acos(-1.0); 79 const double eps = 1e-5; 80 using namespace std; 81 82 int n, m, k; 83 int cas, tol, T; 84 85 double c[maxn]; 86 double pp[maxn]; 87 double dp[maxn][maxn]; 88 89 int main() { 90 double p1, p2, p3, p4; 91 while(~scanf("%d%d%d%lf%lf%lf%lf", &n, &m, &k, &p1, &p2, &p3, &p4)) { 92 if(fabs(p4) <= eps) { 93 printf("0.00000\n"); 94 continue; 95 } 96 double p21 = p2 / (1 - p1); 97 double p31 = p3 / (1 - p1); 98 double p41 = p4 / (1 - p1); 99 pp[0] = 1.0; 100 for(int i=1; i<=n; i++) 101 pp[i] = pp[i-1] * p21; 102 dp[1][1] = p41 / (1 - p21); 103 c[1] = p41; 104 for(int i=2; i<=n; i++) { 105 for(int j=1; j<=i; j++) { 106 if(j == 1) c[j] = p41; 107 else if(j <= k) c[j] = p31 * dp[i-1][j-1] + p41; 108 else c[j] = p31 * dp[i-1][j-1]; 109 } 110 double tmp = 0; 111 for(int j=1; j<=i; j++) { 112 tmp += pp[i-j] * c[j]; 113 } 114 dp[i][i] = tmp / (1.0 - pp[i]); 115 dp[i][1] = p21 * dp[i][i] + c[1]; 116 for(int j=2; j<i; j++) { 117 dp[i][j] = p21 * dp[i][j-1] + c[j]; 118 } 119 } 120 for(int i=1; i<=n; i++) { 121 for(int j=1; j<=i; j++) { 122 printf("%f%c", dp[i][j], j==i ? '\n' : ' '); 123 } 124 } 125 printf("%.5f\n", dp[n][m]); 126 } 127 return 0; 128 }
View Code