hdu 4586 Play the Dice(概率dp)

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 604 0 0 0 01 3

Sample Output

3.500.00

Source

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

大致题意:有一个骰子有n个面,掷到每一个面的概率是相等的,每一个面上都有相应的钱数。其中当你掷到m个面之一时,你有多掷一次的机会。问最后所得钱数的期望。

 

思路:设投掷第一次的期望是p,那么第二次的期望是m/n*p,第三次的期望是 (m/n)^2*p......N次的期望是(m/n)^(N-1)*p

那么这些期望之和便是答案。之前也是想到这,但不知道如何处理无限的情况。当时脑卡了,这不是赤裸裸的等比数列吗?

q = m/n,公比就是q,本题中等比数列之和为p*(1-q^N)/(1-q)。分三种情况讨论:当p0时,输出0.00;当q等于1时,说明可以无限的投掷下去,输出inf;当q < 1时,N无穷大时,1-q^N区域1,那么原式变为p/(1-q)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<map>
 5 #include<algorithm>
 6 #include<set>
 7 #include<math.h>
 8 #include<stdlib.h>
 9 #include<cmath>
10 using namespace std;
11 #define eps 1e-10
12 #define N 206
13 int a[N];
14 int vis[N];
15 int main()
16 {
17     int n;
18     int m;
19     while(scanf("%d",&n)==1)
20     {
21         int sum=0;
22         int i;
23         for(i=1;i<=n;i++)
24         {
25             scanf("%d",&a[i]);
26             sum+=a[i];
27         }
28         double p=sum*1.0/n;
29         
30         memset(vis,0,sizeof(vis));
31         
32         scanf("%d",&m);
33         int cnt=0;
34         for(i=0;i<m;i++)
35         {
36             int x;
37             scanf("%d",&x);
38             if(vis[x]) continue;
39             vis[x]=1;
40             cnt++;
41         }
42         double q=cnt*1.0/n;
43         if(fabs(p)<eps)
44           printf("0.00
");
45         else if(fabs(q-1)<eps)
46           printf("inf
");
47         else 
48           printf("%.2lf
",p/(1-q));
49     }
50     return 0;
51 }
View Code
原文地址:https://www.cnblogs.com/UniqueColor/p/4731090.html