HDU 4089 Activation (probability dp good problem + puzzle)

Source: Internet
Author: User

Activation Time limit:20000/10000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 1842 Accepted Submission (s): 689

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's at home and ready to begin his journey.
But before starting the game, he must first activate the product on the official site. There is too many passionate fans that the activation server cannot deal with all the requests at the same time, so all t He players must wait in queue. Each time, the server deals with the request of the first player in the the queue, and the result is the one of the following, Each have a probability:
1. Activation failed:this happens with the probability of P1. The queue remains unchanged and the server would 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 he connection with the server. The server would then remove his request from the queue. After that, the player would 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 would leave the queue and enjoy the game himself.
4. Service unavailable:this happens with the probability of P4. Something just happened and the server are down. The website must shutdown the server at once. All the requests is still in the queue would never be dealt.
Tomato thinks it sucks if the the server is under while he is still waiting in the queue and there are no more than K-1 guys be Fore him. And he wants to know the probability, this ugly thing happens.
To make it clear, we say three things could happen to Tomato:he succeeded activating the game; The server is under while the he was in the queue and there be no more than K-1 guys before him; The server is under while the he was in the queue and there be at least K guys before him.
Now is to calculate the probability of the second thing. 
Inputthere is no more than test cases. Each case on one line, contains three integers and four real numbers:n, M (1 <= M <= N <=), K (k >= 1), P1, p2, p3, p4 (0 <= p1, p2, p3, P4 <= 1, p1 + p2 + p3 + P4 = 1), indicating there is N guys in the queue (the Posi tions is numbered from 1 to N), and at the beginning Tomato was at the Mth position, with the probability P1, p2, p3, P4 m entioned above. 
OutputA real number in one line for each case, the probability that the ugly thing happens.
The answer should is rounded to 5 digits after the decimal point. Sample Input
2 2 1 0.1 0.2 0.3 0.43 2 1 0.4 0.3 0.2 0.14 2 3 0.16 0.16 0.16 0.52
 Sample Output
0.304270.232800.90343
 Source Asia Beijing Regional Contest

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4089

The main topic: N people queue to activate a thing, the initial target is ranked in the first m
There are 4 scenarios:
1. Registration fails, but does not affect queue order, probability is P1
2. Connection failed, the first team to the end of the line, the probability of P2
3. The registration is successful, the team first leaves the queue, the probability is P3
4. Server crashes, activation stop, probability is P4
The probability that the target is within K-bit and the server may crash

Problem Analysis: The state of the problem and the transfer equation is not difficult to think, difficult to simplify and recursive, the following gives a detailed analysis:
DP[I][J] Indicates that there is an I person in the team, the probability that the target will occur at the first J time
DP[I][1] = DP[I][1]*P1+DP[I][I]*P2+P4 (when the target is ranked first and there is an I person in the team may be the target registration failure is still the first, may link failure to run to the end of the queue, may also server crashes)
DP[I][J] = DP[I][J]*P1+DP[I][J-1]*P2+DP[I-1][J-1]*P3+P4 (j<=k) (when the target is ranked J, and before K, 1. The first person may fail to register the first and the target position remains unchanged, 2. The first person may fail to connect to the end of the team, when the target position moves forward, 3. The first person may register successfully and leave the queue, when the target position moves forward, and the team is one less, 4. The server may crash)
DP[I][J] = DP[I][J]*P1+DP[I][J-1]*P2+DP[I-1][J-1]*P3 (j>k) (the only difference between this and j<=k is that there is no P4, well understood, because when the target is ranked after K, the server does not crash, Otherwise the event will not happen, we are the probability that the event may occur in the recurrence
The problem of state and transfer equation is solved, and then we have to solve how to recursion, first we will get the equation of State transfer:
Make:
p21 = p2/(1-P2)
P31 = p3/(1-P1)
p41 = p4/(1-P1);
The
DP[I][1] = dp[i][i]*p21+p41
DP[I][J] = dp[i][j-1]*p21+dp[i-1][j-1]*p31+p41; (j<=k)
DP[I][J] = dp[i][j-1]*p21+dp[i-1][j-1]*p31; (j>k)

From the recursive we can know when to recursive Dp[i][j] dp[i-1][j-1] has been calculated, we may wish to use c[j] to represent the constant term dp[i][j]
i.e. Dp[i][j] = Dp[i][j-1]*p21 + c[j], then c[j] = dp[i-1][j-1]*p31 + p41, c[1] = p41
So we first use a recursive C array to get, and then ask Dp[i][i]
DP[I][1] = P21*dp[i][i] + c[1];
DP[I][2] = p21*dp[i][1] + c[2];
DP[I][3] = p21*dp[i][2] + c[3];
...
Dp[i][i] = P21*dp[i][i-1] + c[i];
Add up the above formula Dp[i][i] = p21* (dp[i][i-1]) + c[i] = p21* (P21*dp[i][i-2] + c[i-1]) + c[i] = ... = p21* (p21*...dp[i][1] + c[2]) +. .. Dp[i][1]=p21*dp[i][i] + c[1] take to the upper dp[i][i] =(P21) ^i*dp[i][i] + (p21) ^ (i-1) c[1] + (p21) ^ (i-2) c[2] + ... (P21) ^0c[i]Re-move to degenerate Dp[i][i] =∏ (p21) ^ (i-j) C[j]/(1-(P21) ^i) so we can first preprocess out (P21) ^i, here the problem is almost done, calculate the dp[i][i] we can get dp[i][1]
DP[I][1]=DP[I][I]*P21+P41, so that the values for recursion are obtained, directly according to dp[i][j] =dp[i][j-1]*p21 + c[j]You can just push it.
The initial value dp[1][1]=dp[1][1]*p21+p41 is DP[1][1]=P4/(1-P1-P2), but also note that the accuracy of the problem, when the P4 is less than 1e-5, when it is not an event, because the accuracy of the title requirement is 5 digits after the decimal point
It's an interesting question ~

#include <cstdio>int Const MAX = 2005;double Const EPS = 1e-5;double Dp[max][max], C[max], P[max];int main () {int    N, M, K;    Double P1, p2, P3, P4; while (scanf ("%d%d%d%lf%lf%lf%lf", &n, &m, &k, &p1, &AMP;P2, &AMP;P3, &AMP;P4)! = EOF) {if        (P4 < EPS)            {printf ("0.00000\n");        Continue        } Double P21 = p2/(1-P1);        Double p31 = p3/(1-P1);        Double p41 = P4/(1-P1);        Double tmp;        P[0] = 1;        for (int i = 1; I <= n; i++) p[i] = p[i-1] * P21;        DP[1][1] = P4/(1-P1-P2);                    for (int i = 2, I <= N; i++) {for (int j = 1; J <= I; j + +) {if (J <= k)               C[J] = dp[i-1][j-1] * p31 + p41;            else c[j] = dp[i-1][j-1] * P31;            } tmp = 0;            for (int j = 1; J <= I; j + +) tmp + = p[i-j] * C[j]; dp[I][i] = tmp/(1-p[i]);            DP[I][1] = P21 * Dp[i][i] + p41;        for (int j = 2; J < I; j + +) Dp[i][j] = P21 * Dp[i][j-1] + c[j];    } printf ("%.5f\n", Dp[n][m]); }}






HDU 4089 Activation (probability dp good problem + puzzle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.