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个面的摇骰子的期望次数 因为我 ...
随机推荐
- (转)苹果消息推送服务器 php 证书生成
1.准备好 aps_developer_identity.cer , push.p12这两个证书文件 2. 生成证书如下: openssl x509 -in aps_developer_identit ...
- iOS 压缩与裁剪图片问题
我们假设要在截图中的举行图片展示区显示图片,由于原图片的宽高比例与图片显示窗口的宽高比例不一定相同,所以,直接将图片扔进去会改变图片的宽高比例,展示效果不好. 这时你可能想到设置UIImageView ...
- IOS设计模式学习(18)模板方法
1 前言 模板方法模式是面向对象软件设计中一种非常简单的设计模式.其基本思想是在抽象类的一个方法定义“标准”算法.在这个方法中调用的基本操作由子类重载予以实现.这个方法成为“模板”.因为方法定义的算法 ...
- Java 泛型数组
Java 不支持泛型数组.也就是说, List<String>[] ls = new ArrayList<String>[10]; 是不支持的,而 List<String ...
- [Angular 2]ng-class and Encapsulated Component Style2
Many Components require different styles based on a set of conditions. Angular 2 helps you style you ...
- mvc自带的异步表单提交
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- JavaScript学习笔记(三十八) 复制属性继承
复制属性继承(Inheritance by Copying Properties) 让我们看一下另一个继承模式—复制属性继承(inheritance by copying properties).在这 ...
- 用jQuery与JSONP轻松解决跨域访问的问题【转】
原文地址:http://www.jb51.net/article/46463.htm 好在,有jquery帮忙,跨域问题似乎没那么难缠了.这次也借此机会对跨域问题来给刨根问底,结合实际的开发项目,查阅 ...
- 使用“bulk insert ”进行批量插入数据
本文转自csdn中文章,再次感谢他给我们分享. Bulk Insert命令详细 BULK INSERT以用户指定的格式复制一个数据文件至数据库表或视图中.语法: BULK INSERT [ [ 'da ...
- Java并发编程与技术内幕:线程池深入理解
摘要: 本文主要讲了Java当中的线程池的使用方法.注意事项及其实现源码实现原理,并辅以实例加以说明,对加深Java线程池的理解有很大的帮助. 首先,讲讲什么是线程池?照笔者的简单理解,其实就是一组线 ...