HDU 4586Play the Dice 概率dp+等比数列 级数

来源:互联网 发布:泰国 试管婴儿 知乎 编辑:程序博客网 时间:2024/04/19 21:18
C - Play the Dice
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 4586
Appoint description: 

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 a i(0<=a i<200) 
The second line is an integer m (0<=m<=n), following with m integers b i(1<=b i<=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

 



给你一个n面的骰子每个面有一个值然后其中有不同值代表你能获得的钱

然后有m个特殊的面——当你骰到这一面的时候可以获得一个新的机会

问你能得到钱的期望,

骰第一次     sum/n

骰第二次     sum/n*(m/n)

骰第三次     sum/n*(m/n)*(m/n)

骰第四次     sum/n*(m/n)*(m/n)*(m/n)

.

.

.

.

骰第k次     sum/n*(m/n)^k

   sum/n*(1+q+q^2+q^3+……+q^k)    q=m/n  k=inf    

所以           sum/n*(1/(1-m/n))  =  (sum/n)*(n/n-m)  =sum/n-m

那么 n=m时候期望无限大

notice sum等于0的时候直接输出 0.00///我不知道为什么

ACcdoe:

#include <cstdio>#include <cmath>#define maxn 202#define eps 1e-8int p[maxn];int main(){    int n,m;    while(~scanf("%d",&n)){        int sum=0;        for(int i=1;i<=n;++i){            scanf("%d",&p[i]);            sum+=p[i];        }        scanf("%d",&m);        int t;        for(int i=1;i<=m;++i)scanf("%d",&t);        if(sum==0){                printf("0.00\n");                continue;        }        if(n==m)            puts("inf");        else printf("%0.2lf\n",(double)sum/(n-m));    }    return 0;}


2 1