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;
}

 

转载于:https://www.cnblogs.com/qscqesze/p/4721845.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值