HDU-4089 Activation (概率DP求概率)

题目大意:一款新游戏注册账号时,有n个用户在排队。每处理一个用户的信息时,可能会出现下面四种情况:

1.处理失败,重新处理,处理信息仍然在队头,发生的概率为p1;

2.处理错误,处理信息到队尾重新排队,发生的概率为p2;

3.处理成功,队头信息处理成功,出队,发生的概率为p3;

4.服务器故障,队伍中所有信息丢失,发生的概率为p4;

小明现在在队伍中的第m个位置,问当他前面的信息条数不超过k-1时服务器故障的概率。

题目分析:这道题的状态转移方程不难写。定义状态dp(i,j)表示在有 i 个人的队伍中,他排在第 j 个位置时到达要求状态的概率。则状态转移方程为:

dp(i,1)=p1*dp(i,1)+p2*dp(i,i)+p4

dp(i,j)=p1*dp(i,j)+p2*(i,j-1)+p3*dp(i-1,j-1)+p4  (2<=j<=k)

dp(i,j)=p1*dp(i,j)+p2*(i,j-1)+p3*dp(i-1,j-1)  (k<j<=i)

整理一下,并另p21=p2/(1-p1),p31=p3/(1-p1),p41=p4/(1-p1),则得到:

dp(i,1)=p21*dp(i,i)+p41

dp(i,j)=p21*dp(i,j-1)+p31*dp(i-1,j-1)+p41  (2<=j<=k)

dp(i,j)=p21*dp(i,j-1)+p31*dp(i-1,j-1)  (k<j<=i)

这样就可以通过递推求解。

为了书写方便,把上面的三个转移方程用两个方程表示出来:

dp(i,1)=p21*dp(i,i)+c(1)

dp(i,j)=p21*dp(i,j-1)+c(j)  (2<=j<=i)

dp(i,i)可以通过迭代得到:

(1-p21^i)dp(i,i)=∑(p21^(i-j))*c(j)  (1<=j<=i)

ps:得加特判,否则会WA!!。。。

代码如下:

# include<iostream>
# include<cstdio>
# include<cmath>
# include<cstring>
# include<algorithm>
using namespace std;

const double eps=1e-5;

int n,m,k;
double p1,p2,p3,p4;
double dp[2005][2005];

int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        scanf("%lf%lf%lf%lf",&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);
        dp[1][1]=p41/(1-p21);
        for(int i=2;i<=n;++i){
            dp[i][i]=0;
            for(int j=1;j<=i;++j){
                if(j==1) dp[i][i]+=pow(p21,i-j)*p41;
                else if(j>=2&&j<=k) dp[i][i]+=pow(p21,i-j)*(p31*(dp[i-1][j-1])+p41);
                else dp[i][i]+=pow(p21,i-j)*p31*dp[i-1][j-1];
            }
            dp[i][i]/=(1-pow(p21,i));
            for(int j=1;j<i;++j){
                if(j==1) dp[i][j]=p21*dp[i][i]+p41;
                else if(j>=2&&j<=k) dp[i][j]=p21*dp[i][j-1]+p31*dp[i-1][j-1]+p41;
                else dp[i][j]=p21*dp[i][j-1]+p31*dp[i-1][j-1];
            }
        }
        printf("%.5lf\n",dp[n][m]);
    }
    return 0;
}

  

时间: 2024-10-29 03:50:03

HDU-4089 Activation (概率DP求概率)的相关文章

CoderForce 148D-Bag of mice (概率DP求概率)

题目大意:美女与野兽在玩画鸽子的游戏.鸽子在用黑布遮住的笼子里,白色的有w只,黑色的有b只,每次拿出一只作画,谁先画到白色的鸽子谁就赢.美女首先画,因为野兽太丑,它每次画的时候都会吓跑一只鸽子,所有出笼子的鸽子都不在进去.求美女赢得概率.(设定假如没有人画到白色鸽子,算野兽赢). 题目分析:这道题不难,很显然的概率DP.这是我第一次写概率DP,纪念一下... 代码如下: # include<iostream> # include<cstdio> # include<vecto

HDU 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

HDU 4050 wolf5x (概率DP 求期望)

题意:有N个格子,1~N,起点在0,每个格子有一个状态(0,1,2,3),每次可以跨[a,b]步, 问走完N个格子需要步数的期望,每次尽量走小的步数,即尽量走a步,不能则走a+1,-- 状态0意味着你不能踏进对应的网格. 状态1意味着你可以??步入网格用你的左腿. 状态2意味着你可以??步入网格用你的右腿. 状态3意味着你可以进入网格用任何你的腿,而接下来的步骤中,您可以使用任何的腿;即你不需要遵循上述规则. 思路:借鉴了各路大神的思想理解了下. dp[i][j] :表示走到第 i 个格子在 j

HDU4336-Card Collector(概率DP求期望)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2195    Accepted Submission(s): 1034 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

ZOJ3329-One Person Game(概率DP求数学期望)

One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very simple and interesting one-person game. You have 3 dice, namelyDie1, Die2 and Die3. Die1 hasK1 faces. Die2 has K2 faces.Die3 has K3 faces. All the dic

HDU3853-LOOPS(概率DP求期望)

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 1864    Accepted Submission(s): 732 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help h

HDU4405-Aeroplane chess(概率DP求期望)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1182    Accepted Submission(s): 802 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab

POJ 2096 Collecting Bugs(概率DP求期望)

传送门 Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 4333 Accepted: 2151 Case Time Limit: 2000MS Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu

Codeforces 235B Let&#39;s Play Osu! (概率dp求期望+公式变形)

B. Let's Play Osu! time limit per test:2 seconds memory limit per test:256 megabytes You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us deno