ZOJ 3329 One Person Game 概率DP 期望 难度:2

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

本题分数为0的概率不确定,所以不能从0这端出发.

设E[i]为到达成功所需的步数,明显i>n时E[i]=0,当0<i<=n时E[i]=sigma(E[i+k]*pk)+E[0]*p0,(k是可以投出的除了恰为a,b,c以外的骰子之和),

在这个公式里,E[i]和E[0]都是未知的,设E[0]=x,则

E[i]=sigma(E[i+k]*pk)+x*p0+1,

因为比i大的所有j,满足E[j]的一次项和零次项都是已知的,明显,可以用x来表示所有E[i],

设E[i]的一次项部分为a[i],常数项部分为b[i],逐渐递推,可以得到E[0]=a[0]*E[0]+b[0],这时E[0]可解出,即为答案

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=505;
const int maxk=7;
double p[maxk*3],dpa[maxn],dpb[maxn];
int n,k1,k2,k3,a,b,c;
int sumk;
double mulk;
void init(){
        sumk=k1+k2+k3;
        mulk=k1*k2*k3;
        memset(p,0,sizeof(p));
        p[0]=1/mulk;
        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]+=1/(mulk);}

                }
            }
        }
}
void calc(){
    for(int i=n;i>=0;i--){
        dpa[i]=p[0];
        dpb[i]=1;
        for(int j=3;j<=sumk&&i+j<=n;j++){
            dpa[i]+=dpa[i+j]*p[j];
            dpb[i]+=dpb[i+j]*p[j];
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int ti=1; ti<=T; ti++)
    {
        scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
        init();
        calc();
        printf("%.15f\n",dpb[0]/(1-dpa[0]));
    }

    return 0;
}
时间: 2024-10-14 07:24:29

ZOJ 3329 One Person Game 概率DP 期望 难度:2的相关文章

[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

zoj 3329 One Person Game 概率dp

先吐槽几句真心给数学跪了 题意: 有三个均匀的骰子,分别有k1,k2,k3个面,初始分数是0, 当掷三个骰子的点数分别为a,b,c的时候,分数清零,否则分数加上三个骰子的点数和, 当分数>n的时候结束.求需要掷骰子的次数的期望. 题解: 设 E[i]表示现在分数为i,到结束游戏所要掷骰子的次数的期望值. 显然 E[>n] = 0; E[0]即为所求答案; E[i] = ∑Pk*E[i+k] + P0*E[0] + 1; (Pk表示点数和为k的概率,P0表示分数清零的概率) 由上式发现每个 E[

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 K3faces. All the d

2017 ICPC Asia Urumqi A.coins (概率DP + 期望)

题目链接:Coins Description Alice and Bob are playing a simple game. They line up a row of nn identical coins, all with the heads facing down onto the table and the tails upward. For exactly mm times they select any kk of the coins and toss them into the

Atcoder Beginner Contest 144 F- Fork the Road(概率DP/期望DP)

Problem Statement There is a cave consisting of NN rooms and MM one-directional passages. The rooms are numbered 11 through NN . Takahashi is now in Room 11 , and Room NN has the exit. The ii -th passage connects Room sisi and Room titi (sisi < titi

zoj 3640 Help Me Escape (概率dp 递归求期望)

题目链接 Help Me Escape Time Limit: 2 Seconds      Memory Limit: 32768 KB Background     If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at the door. And unto thee shall be his desire, and thou shalt rule over him.  

hdu 3853 LOOPS(概率 dp 期望)

Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help her friend Madoka save the world. But because of the plot of the Boss Incubator, she is trapped in a labyrinth called LOOPS. The planform of the LOOPS

ZOJ 3640 Help Me Escape 概率dp

有一个吸血鬼被困了,有n条路可以逃出去,每条路有一个难度c[],他初始的战斗力是f,对于第i条路,若f > c[i]他花t[i]天就能出去,否则,他就停留一天,同时战斗力增加c[i]然后再选一条路走出去,他走每条路的概率是相同的.问他逃出去的天数的期望. 设dp[i]表示在战斗力为i时逃出去的期望值,那么可推出状态方程 dp[i] = 1/n * t[j](c[j] > i),dp[i] = 1/n * (1+dp[ i+c[j] ] )( c[j] <= i). 需要注意的是终态的确定

UVA 11427 (概率DP+期望)

题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 题目大意:每晚打游戏.每晚中,赢一局概率p,最多玩n局,如果最后不能保证胜率大于p,则从此不玩.问打游戏的天数的期望. 解题思路: 首先分析每天晚上的. 设f[i][j]为前i天,已经赢j局的概率. 由全概率公式,那么当天晚上完蛋的概率q=f[n][0]+f[n][1]+.....f[n][终止条件]. 至于为什么从完蛋(输)的角度考虑,主要是由于n局的