lightoj 1030 概率dp

1030 - Discovering Gold
Time Limit: 2 second(s)Memory Limit: 32 MB

You are in a cave, a long cave! The cave can be representedby 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 youthrow a perfect 6 sided dice. If you get X in the dice afterthrowing, you add X to your position and collect all the gold from thenew position. If your new position is outside the cave, then you keep throwingagain until you get a suitable result. When you reach the Nthposition you stop your journey. Now you are given the information about thecave, you have to find out the expected number of gold you can collectusing 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 linecontains N space separated integers. The ith integerof this line denotes the amount of gold you will get if you come to the ithcell. You may safely assume that all the given integers will be non-negativeand no integer will be greater than 1000.

Output

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

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


题意: 有n个格子, 每个格子有对应的金子数目, 从第一个格子开始,每次扔一个骰子, 骰子朝上的点数决定你向前走的步数, 如果超过n,那么一直扔, 直到小于n, 到最后到达n点, 求所得的期望金子的总数目.


分析: 求期望的话可以倒着来, 从n开始到1, dp[i+j](j>=1 && j<=6)可以转移到dp[i],当i+j<=n时, 扔骰子是等概率的, 因此dp[i] = dp[i+j]/6, 但是当i+j>n的时候就不是1/6的概率了,要看j的最大值能取到多少, 然后就是1/j;  


#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;char c;int f=1;
    while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res*f;
}
const int N=100010,MOD=1e9+7;

int a[123];
double dp[123];
int main()
{
    int T=in(),ca=1;
    while(T--)
    {
        int n=in();
        for(int i=0;i<n;i++)
        {
            a[i]=in();
        }
        dp[n-1]=a[n-1];
        for(int i=n-2;i>=0;i--)
        {
            dp[i]=a[i];
            int k=0;
            for(int j=i+1;j<=i+6;j++)
            {
                if(j==n) break;
                k++;
            }
            for(int j=i+1;j<=i+6;j++)
            {
                if(j<n)
                {
                    dp[i]+=dp[j]*1.0/k;
                }
            }
        }
        printf("Case %d: %.9lf\n",ca++,dp[0]);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值