ZOJ 3329:One Person Game 概率DP求期望(有环)

One Person Game

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754

题意:

玩一个掷骰子的游戏,同时掷三个筛子,每次掷筛子都会得到分数(三个筛子掷得的数的合),规则如下:

                                             初始分数为0,如果一号骰子掷得a且二号骰子掷得b,同时三号筛子掷得c,则分数归零

                                            当分数大于n时游戏结束

求直到游戏结束掷骰子的次数的期望

题解:

设E[i]为初始分数为0时所求的期望,则E[n+1]=0,E[0]即所求答案

可以知道 E[i]=∑(E[i+k]*p[k])+P0*E[0]+1 ,p[k]掷得三个骰子的得分合为k的概率,p0为归零的概率

由于所有的E[i]都与E[0]有关,因此不能直接由这条公式得到答案

可以将E[0]当做一个常数,设E[i]=a[i]*E[0]+b[i]

则 E[i]=∑((a[i+k]*E[0]+b[i+k])*p[k])+P0*E[0]+1

可得E[i]=(∑(Pk*a[i+k])+p0)*E[0]+∑(Pk*b[i+k])+1

由此可知:

  a[i]=∑(Pk*a[i+k])+p0

  b[i]=∑(Pk*b[i+k])+1

因为E[i>n]=0 所以E[i>n]=a[i>n]*E[0]+b[i>n]=0---->a[i>n]=b[i]>n=0

由E[0]=a[0]*E[0]+b[0]

可知  E[0]=(1-a[0])/b[0]

注意:求p[k]时要注意归零的情况

             

代码

#include<stdio.h>
#include<string.h>
double P[19],s[502],d[502],p0;
int main()
{
  int T,n,K1,K2,K3,a,b,c;
  scanf("%d",&T);
  while(T--)
  {
    scanf("%d%d%d%d%d%d%d",&n,&K1,&K2,&K3,&a,&b,&c);
    memset(P,0,sizeof(P));
    memset(s,0,sizeof(s));
    memset(d,0,sizeof(d));
    s[n+1]=d[n+1]=0.0;
    p0=1/(K1*K2*K3*1.0);
    for(int i=1;i<=K1;++i)
    for(int j=1;j<=K2;++j)
    for(int k=1;k<=K3;++k)
    if(i!=a||j!=b||k!=c)

    P[i+j+k]+=p0;

    for(int i=n;i>=0;--i)
    {
      for(int j=3;j<=K1+K2+K3;++j)
      if(i+j<=n+1)
      {
        s[i]+=s[i+j]*P[j];
        d[i]+=d[i+j]*P[j];
      }
      else break;
      s[i]+=p0,d[i]++;
    }
    printf("%.15f\n",d[0]/(1.0-s[0]));
  }
}

  

时间: 2024-10-12 19:17:36

ZOJ 3329:One Person Game 概率DP求期望(有环)的相关文章

[ACM] ZOJ 3329 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, namely Die1, Die2 and Die3. Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the

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>

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

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

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

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

zoj 3822 Domination 【概率DP 求期望】

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar

概率DP——求期望

一般求期望类题目都是倒着来做的,dp[i]表示i状态下要达到要求状态的期望值,于是dp[0]就是我们要找的答案,我们可以通过此来推状态转移方程,可以得到dp[i]=Σ(dp[k>i]*p[k])+ 1,由于由k状态转移到i状态会多一步操作,这多的一步乘以每种概率再求和刚好等于1,所以转移公式后面有一个加一. POJ 2096题目链接:http://poj.org/problem?id=2096 一个软件有s个子系统,会产生n种bug某人一天发现一个bug,这个bug属于一个子系统,属于一个分类每