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 dice are fair dice, so the probability of rolling each value, 1 toK1,
K2, K3 is exactly 1 /K1, 1 /
K2 and 1 / K3. You have a counter, and the game is played as follow:

  1. Set the counter to 0 at first.
  2. Roll the 3 dice simultaneously. If the up-facing number of Die1 isa, the up-facing number of
    Die2 is b and the up-facing number ofDie3 is
    c, set the counter to 0. Otherwise, add the counter by the total value of the 3 up-facing numbers.
  3. If the counter‘s number is still not greater than n, go to step 2. Otherwise the game is ended.

Calculate the expectation of the number of times that you cast dice before the end of the game.

Input

There are multiple test cases. The first line of input is an integer T (0 <T <= 300) indicating the number of test cases. Then
T test cases follow. Each test case is a line contains 7 non-negative integersn,
K1, K2, K3,a,
b, c (0 <= n <= 500, 1 < K1,K2,
K3 <= 6, 1 <= a <= K1, 1 <=b <=
K2, 1 <= c <= K3).

Output

For each test case, output the answer in a single line. A relative error of 1e-8 will be accepted.

Sample Input

2
0 2 2 2 1 1 1
0 6 6 6 1 1 1

Sample Output

1.142857142857143
1.004651162790698


题意:3个骰子,分别为k1,k2,k3面,每次三个骰子掷到a,b,c时,分数置为0,否则将分数加到计数器上,当计数器的分数大于n时,游戏结束,问游戏 结束的步数的数学期望

DP[i] = SUM(DP[i+k]*Pk)+DP[0]*p0+1
因为要求的是DP[0] 因此可以看成 一个常数
DP[i] = A[i]*DP[0] + B[i]
将DP[i+k]带入式子1,化简得
DP[i] = DP[0]*(sum(A[i+k])*Pk+p0)+sum(B[i+k]*Pk)+1
得出
A[i] = sum(A[i+k])*Pk+p0
B[i] = sum(B[i+k])*Pk+1
DP[0] = B[0]/(1-A[0])
算概率的时候要注意a,b,c的情况不能算入P(a+b+c)中

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 500+10;
int n,k1,k2,k3,a,b,c;
double A[maxn],B[maxn];
double P[20];
int main(){

    int ncase;
    cin >> ncase;
    while(ncase--){
        scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
        double p0 = 1.0/(k1*k2*k3);
        for(int i = 3; i <= k1+k2+k3; i++)
            P[i] = 0.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;

        memset(A,0,sizeof  A);
        memset(B,0,sizeof B);
        for(int i = n; i >= 0; i--){
            A[i] = p0;
            B[i] =  1;
            for(int k = 3; k <= k1+k2+k3; k++){
                if(i+k<=n){
                    A[i] += A[i+k]*P[k];
                    B[i] += B[i+k]*P[k];
                }
            }
        }
        printf("%.8lf\n",B[0]/(1-A[0]));
    }
    return 0;
}





ZOJ3329-One Person Game(概率DP求数学期望),布布扣,bubuko.com

时间: 2024-10-15 01:35:33

ZOJ3329-One Person Game(概率DP求数学期望)的相关文章

[2013山东ACM省赛] The number of steps (概率DP,数学期望)

The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms -). Now she st

bzoj-3450 Easy概率DP 【数学期望】

Description 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:(我们来简化一下这个游戏的规则有n次点击要做,成功了就是o,失败了就是x,分数是按comb计算的,连续a个comb就有a*a分,comb就是极大的连续o.比如ooxxxxooooxxx,分数就是2*2+4*4=4+16=20.Sevenkplus闲的慌就看他打了一盘,有些地方跟运气无关要么是o要么是x,有些地方o或者x各有50%的可能性,用?号来表示.比如oo?xx就是一个可能的输入.那么WJMZBM

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 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

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