HDU 4586 Play the Dice(概率+期望)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4586

Problem Description

There is a dice with n sides, which are numbered from 1,2,...,n and have the equal possibility to show up when one rolls a dice. Each side has an integer ai on it. Now here is a game that you can roll this dice once, if the i-th side is up, you will get ai
yuan. What‘s more, some sids of this dice are colored with a special different color. If you turn this side up, you will get once more chance to roll the dice. When you roll the dice for the second time, you still have the opportunity to win money and rolling
chance. Now you need to calculate the expectations of money that we get after playing the game once.

Input

Input consists of multiple cases. Each case includes two lines.

The first line is an integer n (2<=n<=200), following with n integers ai(0<=ai<200)

The second line is an integer m (0<=m<=n), following with m integers bi(1<=bi<=n), which are the numbers of the special sides to get another more chance.

Output

Just a real number which is the expectations of the money one can get, rounded to exact two digits. If you can get unlimited money, print inf.

Sample Input

6 1 2 3 4 5 6
0
4 0 0 0 0
1 3

Sample Output

3.50
0.00

Source

2013
ACM-ICPC南京赛区全国邀请赛——题目重现

转载:

题意:一个骰子有 n 面,每一面有不同的数字a[i],再给一个m,给m个面,这些面如果被骰到,可以继续多骰一次,问你骰一次得到数字的期望是多少?

解题思路:开始我在推公式,但是由于思维方式不对,算的 有点麻烦,其实转化一个思路就可以了。我们要算骰骰子得出的概率,我开始想的就是sum1是(n-m)非特殊面的和,sum2是特殊面的和,那么骰一次的概率就是

sum1*[(n-m)/n],骰两次的概率是(sum2*(m/n)+sum1*(m/n)*[(n-m)/n],第三次是sum2*(m/n)*(m/n)+sum1*(m/n)*(m/n)*[(n-m)/n] 如此这样递推可以将最终的解求出。最终可以得到的解是(sum1+sum2)+(sum1+sum2)*(m/n)+(sum1+sum2)*(m/n)^2+....+...  记sum1+sum2=sum这样是无穷等比数列,可以使用公式得到p=sum*[(n)/(n-m)],然后这是总期望,还需要除以面数n,得到答案为sum/(n-m).

其实也可以换一条思路,由于是无限,我们可以看作是

只考虑第一次,获得的金币的平均值为sum/n.sum为所有色子的面的金币值相加。

对于运气好,摇中了可以再来一次,该轮就能获得m/n*(sum/n)

运气好,又再来一次,该轮能获得(m/n)^2*(sum/n)

无穷无尽的摇下去,一共能获得sum/n*(1+q + q^2+`````+q^k + ````)其实就是个等比数列,其中公比为q = m/n

将式子化简,就能得到E = sum/(n-m) 或者 E = a1/(1-q),其中a1 = sum/n。所以当sum = 0时为0,n = m时为inf。其余就为sum/(n-m)。

不过这个题目需要特判,如果sum = 0首先输出0.00,然后是n == m,输出inf。因为题目会卡你sum==0且n==m这组答案。

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    int n, m;
    int a[247], b[247];
    while(~scanf("%d",&n))
    {
        int sum = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        scanf("%d",&m);
        for(int i = 0; i < m; i++)
        {
            scanf("%d",&b[i]);
        }
        double a1, q;
        a1 = sum*1.0/n;
        q = m*1.0/n;
        if(a1 == 0)
        {
            printf("0.00\n");
        }
        else if(q == 1)
        {
            printf("inf\n");
        }
        else
        {
            printf("%.2lf\n",a1/(1-q));
        }
    }
    return 0;
}
时间: 2024-08-03 19:28:33

HDU 4586 Play the Dice(概率+期望)的相关文章

HDU 4586 Play the dice(概率题,推公式)

Problem Description: There is a dice with n sides, which are numbered from 1,2,...,n and have the equal possibility to show up when one rolls a dice. Each side has an integer ai on it. Now here is a game that you can roll this dice once, if the i-th

hdu 4586 Play the Dice 数学 概率

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4586 题意: 给一个n面的骰子,每一面有一个分数,掷到的话可以得到那个分数 其中有m个面,当你掷到这些面的时候可以再掷一次 求得分的数学期望 思路: 每轮得分的期望 乘以 轮数的期望 每轮得分的期望 = 各个面的平均分 = sum / n; 轮数期望 = 1 + m/n + (m/n)^2 + ... = n - m / n 所以ans = sum / (n - m) ,其中 n!=m 要特判一些情

hdu 4586 Play the Dice (概率+等比数列)

Play the Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1328    Accepted Submission(s): 429 Special Judge Problem Description There is a dice with n sides, which are numbered from 1,2,...

hdu 4586 Play the Dice

题意:有n个格子,每个格子有一个价值ai,每次翻开该格子都会获得该格子的价值,并且每个格子的翻开的概率是相同的,其中m个格子翻开之后可以获得额外的bi次翻动机会,求一个人的获得价值的期望 sum=a1+a2+....+an,每次翻开一个格子的获得价值a=sum/n,每次可以获得额外翻动机会的概率为q=m/n,那么获得价值的期望为a*(q^0+q^1+....+q^INF)=sum/(n-m) if(sum==0)puts("0"); else (n==m) puts("inf

hdu 4586 (概率+期望)

http://acm.hdu.edu.cn/showproblem.php?pid=4586 大致题意:有一个骰子有n个面,掷到每一个面的概率是相等的,每一个面上都有相应的钱数.其中当你掷到m个面之一时,你有多掷一次的机会.问最后所得钱数的期望. 思路:设投掷第一次的期望是p,那么第二次的期望是m/n*p,第三次的期望是 (m/n)^2*p......第N次的期望是(m/n)^(N-1)*p. 那么这些期望之和便是答案.之前也是想到这,但不知道如何处理无限的情况.当时脑卡了,这不是赤裸裸的等比数

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 4386 Play the Dice 解题报告

hdu 4586---Play the dice 解题思路:概率 题目描述:一个骰子有n面,每面朝上的概率相同,并且每一面上面都有一个数字,其中有m面是彩色的,代表掷到彩色面的时还可以继续掷下去,问最终掷得的数字的期望是多少? 解题方法: 方法一:只考虑单独掷每一次的情况,可以发现,每次掷到的期望是和先前无关的,假设a=sum/n(每掷一次的期望都是a),比如:掷第一次的时候期望是a,掷第二次的时候期望便是(m/n)*a,因为有(m/n)的概率能够掷第二次..依次可以继续下去,等比求和即可. u

hdu 4418 高斯消元求期望

Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1480    Accepted Submission(s): 327 Problem Description Agent K is one of the greatest agents in a secret organization called Men in B

hdu 5955 Guessing the Dice Roll 【AC自动机+高斯消元】

hdu 5955 Guessing the Dice Roll [AC自动机+高斯消元] 题意:给出 n≤10 个长为 L≤10 的串,每次丢一个骰子,先出现的串赢,问获胜概率. 题解:裸的AC自动机,求匹配到终止结点的概率,用 高斯消元?一开始不知道怎么建方程组,直接举个例子吧: Input: 1 2 2 1 1 2 1 图解: x0原本概率就是1,然后还要加上其他结点走向它的概率,,这样最后算下来是大于1的,现在还是觉得怪怪的... 1 #include <cstdio> 2 #inclu