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

3

1

101

2

10 3

3

3 6 9

Sample Output

Case 1: 101.0000000000

Case 2: 13.000

Case 3: 15

题解:

起始位置是1,从1走到n,给你一个骰子(6个面),按点数走,收集每一点上的金子,如果你将要走到的位置在n之内,就继续扔,往前走,如果在n之外,就一直扔到合适的位置为止,求到达n点时的期望

这个题是一个求期望的题,那么值得注意的是,当扔在n之外的情况是无效的,所以我们在位置i<n−6i<n−6的时候 此时的概率应该为 1/(n-i),当我们在算权值的时候,我们发现对于位置i来说,它可以到i+1,i+2,……,i+6i+1,i+2,……,i+6 这些点,而这些点的权值又与他们后面6个点相关,因此我们倒过来从最后一个点开始求,最后一个点是一定会取的,于是我们就用一个dp数组把他计算一下;

参考代码为:

 #include<bits/stdc++.h>
using namespace std;
int T,n;
double dp[]; int main()
{
scanf("%d",&T);
for(int k=;k<=T;k++)
{
scanf("%d",&n);
memset(dp,,sizeof dp);
for(int i=;i<=n;i++) scanf("%lf",dp+i);
for(int i=n-;i>=;i--)
{
for(int j=;j<=;j++) dp[i]+=dp[i+j]/(1.0*min(,n-i));
}
printf("Case %d: %.10lf\n",k,dp[]);
} return ;
}

LightOj-1030 Discovering Gold (期望DP)的更多相关文章

  1. LightOJ - 1030 Discovering Gold —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1030 1030 - Discovering Gold    PDF (English) Statistics For ...

  2. LightOJ 1030 - Discovering Gold - [概率DP]

    题目链接:https://cn.vjudge.net/problem/LightOJ-1030 You are in a cave, a long cave! The cave can be repr ...

  3. LightOj 1030 - Discovering Gold(dp+数学期望)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 题意:在一个1*n 的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得 ...

  4. LightOJ 1030 Discovering Gold(期望 概率)

    正推,到达i的概率为p[i],要注意除了1和n外,到达i的概率并不一定为1 概率表达式为p[i] += p[j] / min(n - j, 6) 从j带过来的期望为exp[i] += exp[j] / ...

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

    题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...

  6. 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 o ...

  7. LightOJ 1030 Discovering Gold(概率DP)题解

    题意:1~n每格都有金子,每次掷骰子,掷到多少走几步,拿走那格的金子,问你金子的期望 思路:dp[i]表示从i走到n金子的期望,因为每次最多走1<=x<=6步,所以dp[i] = a[i] ...

  8. LightOJ 1030 Discovering Gold (期望)

    https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次 ...

  9. LightOJ 1030 Discovering Gold 数学期望计算

    题目大意:给出长度为n的一条隧道,每个位置都有一定数量的财宝.给你一枚骰子,roll到几点就前进几步,如果即将到达的地方超过了这条隧道长度,就重新roll一次,走到n点结束.求这个过程能收获多少财宝. ...

  10. Light OJ 1030 - Discovering Gold(概率dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...

随机推荐

  1. 网站搭建 - 虚拟机的安装 - Linux 本地网站搭建第一步

    搭建网站-1-域名申请参见公众号 生物信息系统(swxxxt) 搭建网站-域名绑定见稍后的一章,就是直接点解析,然后就完事了,可以不看的. 首先准备材料: 先装虚拟机,会要求重启,那就重启吧,安装界面 ...

  2. [javascript] 编写一个计算器,实现加减法

    1.代码 <script> function sum(){ //加法 var value1 = document.getElementById("num1").valu ...

  3. [java] 计算时间复杂度

    一.精算: 1.所有的声明,都不计时间: 2.赋值语句占1个时间单位(下称:单位),比如sum=0: 3.return占1个单位,比如,reeturn sum.     我怀疑范围一个链表头,不止占1 ...

  4. HTML_本地存储

    在HTML5当中,新增了很多的存储方式,这里我先介绍两种,方便我们的使用和操作,具体新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问 ...

  5. Obtaining the backtrace - libunwind

    Sometimes when working on a large project, I find it useful to figure out all the places from which ...

  6. 数位dp介绍

    不了解dp的可以先看一下dp 数位dp含义: 数位:一个数有个位,十位,百位,千位等等,数的每一位都是数位. 数位dp归为计数dp,是在数位上进行操作的dp. 数位dp的实质是一种快速枚举的方式,它满 ...

  7. .Net Core 3.0 使用 Serilog 把日志记录到 SqlServer

    Serilog简介 Serilog是.net中的诊断日志库,可以在所有的.net平台上面运行.Serilog支持结构化日志记录,对复杂.分布式.异步应用程序的支持非常出色.Serilog可以通过插件的 ...

  8. 本地存储常用方式 localStorage, sessionStorage,cookie 的区别 和 服务器存储session

    本地存储:把一些信息存储到客户端本地(主要目的有很多,其中有一个就是实现多页面之间的信息共享)       1. 离线缓存(xxx.manifest)  H5处理离线缓存还是存在一些硬伤的,所以真实项 ...

  9. 《手把手教你》系列练习篇之6-python+ selenium自动化测试(详细教程)

    1. 简介 前面文章我们了解了如何获取元素的text属性值,和判断元素是否显示在页面(is_displayed()方法),本文我们来学习下,判断一个控件是否被选中状态. 2. 验证控件是否被选中 还是 ...

  10. java 运算符&表达式

    1. java中,模运算符%可以获取整数除法的余数,同样适用于浮点类型数据.double y = 23.56; y%5;(即y mod 5 =3.56) [c/c++中,要求%两侧均为整数数据.] 2 ...