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
大致题意:有一个骰子有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)。分三种情况讨论:当p为0时,输出0.00;当q等于1时,说明可以无限的投掷下去,输出inf;当q < 1时,N无穷大时,1-q^N区域1,那么原式变为p/(1-q)。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
#include<set>
#include<math.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
#define eps 1e-10
#define N 206
int a[N];
int vis[N];
int main()
{
int n;
int m;
while(scanf("%d",&n)==1)
{
int sum=0;
int i;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
double p=sum*1.0/n; memset(vis,0,sizeof(vis)); scanf("%d",&m);
int cnt=0;
for(i=0;i<m;i++)
{
int x;
scanf("%d",&x);
if(vis[x]) continue;
vis[x]=1;
cnt++;
}
double q=cnt*1.0/n;
if(fabs(p)<eps)
printf("0.00\n");
else if(fabs(q-1)<eps)
printf("inf\n");
else
printf("%.2lf\n",p/(1-q));
}
return 0;
}
hdu 4586 Play the Dice(概率dp)的更多相关文章
- hdu 4586 Play the Dice 概率推导题
A - Play the DiceTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- HDU 4599 Dice (概率DP+数学+快速幂)
题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n ...
- HDU 5781 ATM Mechine (概率DP)
ATM Mechine 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 Description Alice is going to take ...
- hdu 4405 Aeroplane chess (概率DP)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Throwing Dice(概率dp)
C - Throwing Dice Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Lig ...
- HDU 4050 wolf5x(动态规划-概率DP)
wolf5x Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- hdu 3076 ssworld VS DDD (概率dp)
///题意: /// A,B掷骰子,对于每一次点数大者胜,平为和,A先胜了m次A赢,B先胜了n次B赢. ///p1表示a赢,p2表示b赢,p=1-p1-p2表示平局 ///a赢得概率 比一次p1 两次 ...
- HDU 4336——Card Collector——————【概率dp】
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- SPOJ Favorite Dice(概率dp)
题意: 一个骰子,n个面,摇到每一个面的概率都一样.问你把每一个面都摇到至少一次需要摇多少次,求摇的期望次数 题解: dp[i]:已经摇到i个面,还需要摇多少次才能摇到n个面的摇骰子的期望次数 因为我 ...
随机推荐
- Mongodb 条件查询
1.1 查询出所有数据的指定键(name ,age ,country) db.persons.find({},{name:1,age:1,country:1,_id:0}) 2.查询条件 2.查询条件 ...
- lesson4:利用jmeter来压测数据库
本文讲述了如何利用jmeter来压测数据库,事例中选取了mysql作为测试数据库,其它的数据库也是一样,只需要更换驱动程序即可. 准备工作:a.mysql数据库安装,请自行百度:b.jdbc驱动包,请 ...
- 你需要知道的九大排序算法【Python实现】之基数排序
八.基数排序 基本思想:基数排序(radix sort)属于"分配式排序"(distribution sort),又称"桶子法"(bucket sort)或bi ...
- 2014ACMICPC亚洲区域赛牡丹江现场赛之旅
下午就要坐卧铺赶回北京了.闲来无事.写个总结,给以后的自己看. 因为孔神要保研面试,所以仅仅有我们队里三个人上路. 我们是周五坐的十二点出发的卧铺,一路上不算无聊.恰巧邻床是北航的神犇.于是下午和北航 ...
- NSUserDefaults读取和写入自定义对象
NSUserDefaults可以存取一些短小的信息. 比如存入再读出一个字符串到NSUserDefaults: - NSString *string = [NSString stringWithStr ...
- mybatis之mapper.xml分析
select: id:方法名,在同一个mapper.xml中,要保持唯一 parameterType:指定输入的参数类型,不是必须的,如果不指定,mybatis会自动识别(推荐指定). resultT ...
- Polygon对象
Polylgon对象是由一个或多个Ring对象的有序集合,它可以是由单个Ring 对象构成,也可以使用多个Ring组成.Polygon通常用来代表有面积的多边形矢量对象,如行政区,建筑物等.
- oracle服务开机自启动
1.修改oracle系统配置文件::/etc/oratab vi /etc/oratab orcl:/opt/oracle/product/10.2.0/db_1:Y 2.在 /etc/init.d/ ...
- edit编辑框相关
从Edit Control获取值,然后通过MessageBox输出出来 void CNowaMagic_MFCDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代 ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!
因为项目中使用了AJAX技术,jar包为:json-lib.jar,在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题: 如hibernate延迟加载错误,这都是些老问题了,一看就知 ...