Light OJ 1030 - Discovering Gold(期望)

1030 - Discovering Gold

PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold.

Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If you get X in the dice after throwing, you add X to your position and collect all the gold from the new
position. If your new position is outside the cave, then you keep throwing again until you get a suitable result. When you reach the Nth position you stop your journey. Now you are given the information about the cave, you have
to find out the expected number of gold you can collect using the given procedure.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the dimension of the cave. The next line contains N space separated integers. The ith integer of this line denotes the amount
of gold you will get if you come to the ith cell. You may safely assume that all the given integers will be non-negative and no integer will be greater than 1000.

Output

For each case, print the case number and the expected number of gold you will collect. Errors less than 10-6 will be ignored.

Sample Input

Output for Sample Input


3

1

101

2

10 3

3

3 6 9


Case 1: 101.0000000000

Case 2: 13.000

Case 3: 15

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 10005

double dp[N];

int main()
{
	int i,j,t,n,ca=0;
	sf(t);
	while(t--)
	{
		mem(dp,0);
		sf(n);
		fre(i,1,n+1)
		  scanf("%lf",&dp[i]);
		for(i=n-1;i>=1;i--)
			 for(j=1;j<=6;j++)
			   dp[i]+=dp[i+j]/min(6,n-i); //这个地方很重要,因为如果超出了n就是无效的
	  pf("Case %d: %.6lf\n",++ca,dp[1]);
	}
   return 0;
}
时间: 2024-11-08 23:10:41

Light OJ 1030 - Discovering Gold(期望)的相关文章

[水+期望dp] light oj 1030 Discovering Gold

题意: 给n个点,每个点都有一个财宝. 你从1这个点开始出发,假设你在i这个点,每次随机走1~min(6,n-i)步. 每到达一个点就拿走财宝. 问最后拿到财宝的期望. 思路: 水的题目. dp[n]=v[n] 然后逐个往前推. 就是注意一下步数是 1~min(6,n-i) 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #inc

Light OJ 1030 - Discovering Gold(概率dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的地方, 每个格子中都有价值为v[i]的宝藏. 有一个6面的骰子,数字为从1-6, 每次摇一次骰子, 得到的数字x后, 你可以到达距离当前位置大x的位置, 并且得到那个位置的宝藏. 如果要走的位置在n的外面, 那么在此摇骰子, 直到找到一个合适的数字.到达n位置的时候结束. 现在想知道走到n位置的能够

lightoj 1030 Discovering Gold[ 期望 ]

B - Discovering Gold Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold. Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If yo

LightOJ 1030 Discovering Gold(期望 概率)

正推,到达i的概率为p[i],要注意除了1和n外,到达i的概率并不一定为1 概率表达式为p[i] += p[j] / min(n - j, 6) 从j带过来的期望为exp[i] += exp[j] / min(n - j, 6) 又到达i时有价值val[i],到达i的概率为p[i],故exp[i] += val[i] * p[i] #include<cstdio> #include<iostream> #include<cstdlib> #include<cstr

Light oj 1030 概率DP

D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1030 Description You are in a cave, a long cave! The cave can be represented by a 1

LightOJ 1030 Discovering Gold(期望)

Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold. Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If you get X in the dice a

LightOJ 1030 Discovering Gold (概率/期望DP)

题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 \times N\) grid. Each cell of the cave can contain any amount of gold. Initially you are in position \(1\). Now each turn you throw a perfect \(6\) s

LightOJ 1030 Discovering Gold (期望)

https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次摇骰子,他就前进几步,但有一种情况例外,如果当前位置+色子数 > N,那么他就会重新摇色子. 走到N这个位置的话,意味着游戏结束了. 问游戏结束时,这个人得到金币的期望. 思路:这里给出两种做法,一种是正序求解,一种是逆序求解. ①正序求解: 这种做法是从前往后计算每个格子的概率,假设我们现在处于第

LightOJ 1030 Discovering Gold 数学期望计算

题目大意:给出长度为n的一条隧道,每个位置都有一定数量的财宝.给你一枚骰子,roll到几点就前进几步,如果即将到达的地方超过了这条隧道长度,就重新roll一次,走到n点结束.求这个过程能收获多少财宝. 题目思路:很明显问题是求期望值的. 期望值公式: E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) (p为概率,x为某一点价值). 具体看代码 #include<cstdio> #include<stdio.h> #include<cstdl