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)。分三种情况讨论:当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)的更多相关文章

  1. hdu 4586 Play the Dice 概率推导题

    A - Play the DiceTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  2. HDU 4599 Dice (概率DP+数学+快速幂)

    题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n ...

  3. HDU 5781 ATM Mechine (概率DP)

    ATM Mechine 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 Description Alice is going to take ...

  4. hdu 4405 Aeroplane chess (概率DP)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. Throwing Dice(概率dp)

    C - Throwing Dice Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Lig ...

  6. HDU 4050 wolf5x(动态规划-概率DP)

    wolf5x Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  7. hdu 3076 ssworld VS DDD (概率dp)

    ///题意: /// A,B掷骰子,对于每一次点数大者胜,平为和,A先胜了m次A赢,B先胜了n次B赢. ///p1表示a赢,p2表示b赢,p=1-p1-p2表示平局 ///a赢得概率 比一次p1 两次 ...

  8. HDU 4336——Card Collector——————【概率dp】

    Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. SPOJ Favorite Dice(概率dp)

    题意: 一个骰子,n个面,摇到每一个面的概率都一样.问你把每一个面都摇到至少一次需要摇多少次,求摇的期望次数 题解: dp[i]:已经摇到i个面,还需要摇多少次才能摇到n个面的摇骰子的期望次数 因为我 ...

随机推荐

  1. Android中的windowSoftInputMode属性详解

    这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下     在前面的一篇文章中 ...

  2. 开源中国安卓client源代码学习(一) 渐变启动界面

    开源中国安卓client源代码学习(一) 渐变启动界面 准备学习安卓开发, 看到网上有人推荐开源中国安卓client的源代码, 说里面包括了大部分技术, 于是准备好好研究研究. 特开通此系列博客来记录 ...

  3. Linux下的进程控制块——task_struct

    在Linux中具体实现PCB的是 task_struct数据结构,以下实现摘自github 我想说它真的很长很长...... ↓ struct task_struct { volatile long ...

  4. 用Ant实现Java项目的自动构建和部署(转)

    Ant是一个Apache基金会下的跨平台的构件工具,它可以实现项目的自动构建和部署等功能.在本文中,主要让读者熟悉怎样将Ant应用到Java项目中,让它简化构建和部署操作. 一.            ...

  5. Function.prototype.bind

    解析Function.prototype.bind 简介 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. b ...

  6. Go 解析JSON

    JSON(Javascript Object Notation)是一种轻量级的数据交换语言,以文字为基础,具有自我描述性且易于让人阅读.尽管JSON是JavaScript的一个子集,但JSON是独立于 ...

  7. php测试题整理(0519)

    1.B/S架构和C/S架构: B/S架构是依托于浏览器的网络系统,C/S架构是基于客户端的. B/S架构: 随着Internet和WWW的流行,以往的主机/终端和C/S都无法满足当前的全球网络开放.互 ...

  8. PHP 中的注释

    // 这是 PHP 单行注释 /* 这是 PHP 多行 注释 */ <?php $txt1="Learn PHP"; $txt2="w3cschool.cc&quo ...

  9. CentOS 5上Apache配置虚拟主机范例

    昨天实践了下在CentOS 5上通过Apache直接配置虚拟主机,服务器没有安装面板软件,所以只能通过SSH远程连接操作了.Apache安装在/etc/httpd目录下,这个即是Apache的根目录, ...

  10. MySQL 查询结果保存为CSV文件

    MySQL支持将查询结果直接导出为文本格式,格式如下: into outfile '导出的目录和文件名'                  指定导出的目录和文件名 fields terminated ...