HDU 4586 A - Play the Dice 找规律

A – Play the Dice
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87326#problem/A

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 6
0
4 0 0 0 0
1 3

Sample Output

3.50
0.00

HINT

 

题意

一个n面的骰子,每一面有分值,并且扔到某些面的时候,可以再扔一次,然后问你最后得分的期望是多少

题解

首先,不要去想什么概率DP,那样就走远了

手算一组期望会发现这其实是一个等比数列,和哪些面再扔一次也没有任何关系

直接推就好了

 

唯一要注意的就是,如果骰子本身的分值和为0 的话,那就肯定输出0,不用输出inf

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1)

using namespace std;
const int maxn = 2e2 + 50;
const int limit = 1e2;
int flag[maxn];
int p[maxn];
int n , m ;
double dp[maxn];
double pe;

int main(int argc,char *argv[])
{
  while(~scanf("%d",&n))
  {
      memset(flag,0,sizeof(flag));
      for(int i = 0 ; i < n ; ++ i) scanf("%d",p+i);
      scanf("%d",&m);
      for(int i = 0 ; i < m ; ++ i)
      {
           int x;
           scanf("%d",&x);
           x--;
           flag[x] = 1;
      }
      int er = 1;
      for(int i = 0 ; i < n ; ++ i)
      {
          if (p[i] != 0) er = 0;
      }
      if (er == 1)
      {
          printf("0.00\n");
      }
      else if (m == n) printf("inf\n");
      else
      {
          pe = (double)1/(double)n;
          memset(dp,0,sizeof(dp));
          for(int i = 0 ; i < n ; ++ i) dp[1] += pe *(double)p[i];
          double ans = (n*dp[1])/((double)(n-m));
          printf("%.2lf\n",ans);
      }
  }
  return 0;
}

 

    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/4721845.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞