HDU4089-Activation (probability DP)

Source: Internet
Author: User
Activation Time Limit: 20000/10000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1332 accepted submission (s): 501


Problem descriptionafter 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 when 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 uugly 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 shocould be 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
 
The iteration finally feels a little bit better!
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <string>#include <algorithm>#include <queue>#include <cmath>using namespace std;const double eps = 1e-7;const int maxn = 2000+10;int n,m,k;double dp[maxn][maxn];double p1,p2,p3,p4;double A[maxn];double Pow[maxn];int main(){    while(~scanf("%d%d%d%lf%lf%lf%lf",&n,&m,&k,&p1,&p2,&p3,&p4)){        if(p4<eps) {            printf("0.00000\n");            continue;        }        double p21 = p2/(1-p1);        double p31 = p3/(1-p1);        double p41 = p4/(1-p1);        Pow[0] = 1;        for(int i = 1; i <= n; i++) Pow[i] = Pow[i-1]*p21;        dp[1][1] = p41/(1-p21);        for(int i = 2; i <= n; i++){            A[1] = p41;            for(int j = 2; j <= i; j++){                A[j] = p31*dp[i-1][j-1];                if(j<=k) A[j] += p41;            }            double t = A[1]*Pow[i-1];            for(int j = 2; j <= i; j++){                t += A[j]*Pow[i-j];            }            dp[i][i] = t/(1-Pow[i]);            dp[i][1] = p21*dp[i][i]+p41;            for(int j = 2; j < i; j++)                dp[i][j] = p21*dp[i][j-1]+A[j];        }        printf("%.5lf\n",dp[n][m]);    }    return 0;}


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.