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 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-6will be ignored.

Sample Input

Sample Output
Case : 101.0000000000
Case : 13.000
Case :

题目链接:http://lightoj.com/volume_showproblem.php?problem=1030

*********************************************

题意:给出T 组实例,每组实例给出n,接着n个数,分别代表i位置有黄金的数量。你掷骰子前进,掷到几就走几步,如果即将到达的位置存在的话。走到了哪个位置就可以得到那个位置的黄金。让你求你到达最后一个位置时收集黄金的期望。

分析:dp保存没点的概率,注意骰子只有6步,所以注意大于6的情况;

数学期望 = 每一点的概率 * 到该点的值。

AC代码:

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
#include<stack>
using namespace std;
#define N 12000
#define INF 0x3f3f3f3f double dp[N];
int a[N]; int main()
{
int n,T,k=,i,j,x; scanf("%d", &T); while(T--)
{
scanf("%d", &n);
memset(dp,,sizeof(dp));
double sum=0.0; for(i=;i<=n;i++)
scanf("%d", &a[i]); dp[]=; for(i=;i<=n;i++)
{
x=(n-i<?n-i:);
for(j=;j<=x;j++)
dp[i+j]+=dp[i]*1.0/x; sum+=dp[i]*a[i];
} printf("Case %d: %.10f\n", k++,sum);
} return ;
}

LightOJ 1030 Discovering Gold(期望)的更多相关文章

  1. LightOJ - 1030 Discovering Gold —— 期望

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

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

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

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

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

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

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

  5. LightOJ 1030 Discovering Gold (期望)

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

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

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

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

  8. LightOJ 1030 Discovering Gold

    期望,$dp$. 设$ans[i]$为$i$为起点,到终点$n$获得的期望金币值.$ans[i]=(ans[i+1]+ans[i+2]+ans[i+3]+ans[i+4]+ans[i+5]+ans[i ...

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

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

随机推荐

  1. mybatis的insert返回主键

    <insert id="insert" useGeneratedKeys="true" keyProperty="id" parame ...

  2. 01背包dp+并查集 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...

  3. 利用pscp命令实现linux与windows文件互传

    windows==>linux(单个文件) PrivateKey.ppk(私钥)可以是相对路径或者绝对路径pscp -i D:\PrivateKey.ppk D:\xxx.xx root@123 ...

  4. Python笔记1-20151021

    一.字符串和字符编码 字符 ASCII Unicode UTF-8 A 01000001 00000000 01000001 01000001 中 x 01001110 00101101 111001 ...

  5. jquery-选择checkbox的多种策略

    checkbox属性: var val = $("#checkAll").val();// 获取指定id的复选框的值 var isSelected = $("#check ...

  6. js几种基本数据类型及之间转换与java的不同、js数组一些常见操作

    js的三大组成部分及各自作用: 1.ECMAScript:规范了js的基本语法和功能 2.DOM:js操作页面元素的API 3.BOM:js操作浏览器部分功能的API 如果通过<script s ...

  7. Android中的Fragment页面切换和selector选择器

    效果如图: 提示:下面是用的整个的图片 下面看代码: //--------------------这是主页面布局文件----------------------- <?xml version=& ...

  8. 搭建Linux3.4.2内核编辑环境

    (1)准备工作:1. 准备虚拟机2.安装工具链sudo tar xjf arm-linux-gcc-4.3.2.tar.bz2 -C /设置环境变量:sudo vi /etc/environment ...

  9. 数据库文件导入导出操作,以及赋予权限SQL语句

    1.导出数据库xxxx和tlog(经过测试,没有问题)# /data/mysql/bin/mysqldump -u root -ppassword qq9x | gzip > /home/xxx ...

  10. Linux下编译C代码,出现tan函数报错的情况

    undefined reference to `tan' 但是已经包含了头文件 <math.h>了,可还是报错,说是找不到tan 这个问题的原因不是很清楚, 但是网上给出的方案,就是编译的 ...