题目链接: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\) 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 \(N^{th}\) 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 \(i^{th}\) integer of this line denotes the amount of gold you will get if you come to the \(i^{th}\) 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

Solution

题意

有 \(N\) 个格子,每个格子有价值为 \(val[i]\) 的金子,初始你在第一个格子。

每次抛一个 \(6\) 面的骰子,抛到的数为 \(X_i\),就往前走 \(X_i\) 个格子,如果超过格子 \(N\),就重新抛,走到格子 \(N\) 就结束。求拿到金子的价值的期望。

思路

可以用 概率 DP期望 DP 解决。

概率DP

求出到达每个格子的概率,然后乘上每个格子的价值再累加起来就行。

设 \(dp[i]\) 为到格子 \(i\) 的概率,则 \(dp[i + j] = dp[i + j] + dp[i] / k\ (1\le j\le k,\ k = min(6, n - i))\)。

期望DP

设 \(dp[i]\) 为格子 \(i\) 到 \(N\) 的能获得黄金的期望,则 \(dp[N] = val[N]\)。

状态转移方程为 \(dp[i] = \frac{1}{k} \sum_{j=1}^kdp[i+j]\ (k = min(6, n - i))\)。

Code

概率DP

#include <bits/stdc++.h>
using namespace std;
const int maxn = 110; int v[maxn];
double dp[maxn]; int main() {
int T;
scanf("%d", &T);
int kase = 0;
while(T--) {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &v[i]);
}
memset(dp, 0, sizeof(dp));
dp[1] = 1.0;
for(int i = 1; i <= n; ++i) {
int k = min(6, n - i);
for(int j = 1; j <= k; ++j) {
dp[i + j] += dp[i] * 1.0 / k;
}
}
double ans = 0.0;
for(int i = 1; i <= n; ++i) {
ans += dp[i] * v[i];
}
printf("Case %d: %.7lf\n", ++kase, ans);
}
return 0;
}

期望DP

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

LightOJ 1030 Discovering Gold (概率/期望DP)的更多相关文章

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

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

  3. LightOJ 1030 Discovering Gold (期望)

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

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

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

  5. LightOJ - 1030 Discovering Gold —— 期望

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

  6. LightOj:1030-Discovering Gold(期望dp模板)

    传送门:http://www.lightoj.com/volume_showproblem.php?problem=1030 Discovering Gold Time Limit: 2 second ...

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

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

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

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

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

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

随机推荐

  1. CSS实现文字阴影的效果

    CSS中有两种阴影效果,一种是DropShadow(投影),另一种是Shadow(阴影).1.DropShadow语法:{FILTER:DropShadow(Color=color,OffX=offX ...

  2. python 装饰器 第八步:使用类来作为装饰器参数

    #第八步:使用类作为装饰器参数 #装饰器使用的操作类 class Wish: #祈求方法 def before(): print('饭前洗洗手') #还愿方法 def after(): print(' ...

  3. 深入理解javascript原型和闭包(3)——prototype原型 (转载)

    深入理解javascript原型和闭包(3)——prototype原型   既typeof之后的另一位老朋友! prototype也是我们的老朋友,即使不了解的人,也应该都听过它的大名.如果它还是您的 ...

  4. 热修复设计之AOT/JIT&dexopt 与 dex2oat (一)

    阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680本篇文章将先从AOT/JIT&dexopt 与 dex2o ...

  5. C++中动态内存申请的结果

    1,问题: 1,动态内存申请一定成功吗? 1,不一定成功: 2,常见的动态内存分配代码: 1,C 代码: * sizeof(int)); if( p != NULL ) { // ... ... } ...

  6. C++中的类与封装

    1,类的组合: 1,类不是孤立存在的,类之间都会有一些关系,组合就是类的基本关系之一: 2,电脑一般而言是由 CPU.内存.主板.键盘和硬盘等部件组合而成: 3,学习电脑组装需要多少时间?学习电脑组装 ...

  7. Python做个小游戏

    Ps.可去知乎搜索“雨露浅歌”大神,他写的帖子里有详细讲解和源码. 游戏概述.玩法:通过键盘的↑键来控制小球往上走,当松开↑键时,小球以一定速度向下掉,小球每越过一根棒加1000分,越过一个飞镖加20 ...

  8. VMware Converter Standalone迁移概要

    VMware Converter 迁移工具使用:1.基本概念 1.1基本组件: converter standalone server:包含server和worker两个服务,这两个服务经常一起安装 ...

  9. WPF绑定のRelativeSource

    在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel ...

  10. vue 点击当前元素改变样式

    template  <ul>    <li v-for="(relation,index) in relations" v-bind:id="relat ...