lightoj 1030 Discovering Gold 概率dp

题目:


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


分析:这道题是求期望的,有两种解法,第一是直接求期望,求期望从后往前推,第二是求概率,进一步求期望,求概率从前往后推。

方法1:(以下内容为转载,原帖链接:@see_you_later

//格子和值都是一样,所以下述的话,值就是格子,格子就是值。。。
比如这样的9个格子,我们总底往上来
对于第9个格子,因为只有9,能取的期望就是9;
对于第8个格子,8是一定要取的,而9也是一定回取的,所以对于第8个格子,期望就是17;
对于第7个格子,7是一定要取的,对于后面可能是直接取了9,或者先取8再取9,情况是满足,对于每种情况概率是1/2,所以就是7+9/2+(8+9)/2=20;
PS:
上面的情况,在7后面的时候,我们可能取9,或者先取8,那么其实就是拿了第8个格子的期望和第9个格子期望,期望就是能取的值,然后*概率,全部情况的总和就是新的期望,有人会奇怪那7呢?我们的前提是对于第7格一定拿7啊;
对于第6个格子,那么就是6一定要拿的,然后会拿7,拿8,拿9,他们的期望*概率的总和+他能取的值就是6的第6个格子的期望;
...以此类推;
对于概率的其实一想更简单...
我们一开始就在1,概率就是1,然后扔一个骰子对于每个面的概率就是1/6,那么dp[i]代表概率,每次对能到达的地方更新概率,最后期望就是值乘以概率的总和+1,1是一定要取的哦~
附上自己的代码:

#include<cstdio>
#define min(a,b) (a<b?a:b)
const int MAXN=100+5;
double EX[MAXN];
int a[MAXN];
//EX[i]表示从i格子开始,能获得的gold的期望
int main(){
    int T;scanf("%d",&T);
    int CASE=0;
    while(T--){
        int N;scanf("%d",&N);
        for(int i=1;i<=N;++i)
            scanf("%d",a+i);
        for(int i=N;i>=1;i--){
            EX[i]=a[i];double temp=0.0;
            for(int j=i+1;j<=min(i+6,N);++j){
                temp+=EX[j];
            }
            if(i!=N)EX[i]+=temp/min(N-i,6);
        }
        printf("Case %d: ",++CASE);
        printf("%.8lf\n",EX[1]);
    }
}

方法2:(感觉自己还没理解,方法上面也提到了,但感觉自己还没理解,先留下flag吧)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值