日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【LightOJ - 1030】Discovering Gold(概率dp,数学期望,期望的线性性)

發(fā)布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LightOJ - 1030】Discovering Gold(概率dp,数学期望,期望的线性性) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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*N的格子里,每個格子都有相應的金幣數(shù),走到相應格子的話,就會得到該格子的金幣。?
現(xiàn)在有一個人在1這個位置,手里有一顆色子,色子搖到幾,他就前進幾步,但有一種情況例外,如果當前位置+色子數(shù) > N,那么他就會重新?lián)u色子。?
走到N位置,意味著游戲結束了。?
問游戲結束時,這個人得到金幣的期望。

解題報告:

首先根據(jù)期望的線性性轉化題意:考慮每個點被選中的概率,走到每個點的概率乘以該點的權值 的和。

dp[i]表示走到i這個點的概率是多少?!拔覟槿巳恕狈ㄞD移。(最后的dp[n]肯定==1)

最后乘以權值就是答案。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int n,a[MAX]; double dp[MAX]; int main() {int t,iCase=0;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) scanf("%d",a+i);printf("Case %d: ",++iCase);for(int i = 1; i<=n+6; i++) dp[i]=0;dp[1]=1;for(int i = 1; i<=n; i++) { // for(int j = 1; j<=6; j++) { // if(i-j >= 1) dp[i] += dp[i-j]; // }int up = min(n-i,6); for(int j = 1; j<=up; j++) dp[i+j] += dp[i]/up;}double ans = 0;for(int i = 1; i<=n; i++) ans += dp[i] * a[i];printf("%.8f\n",ans);}return 0 ; }

思路2:(鏈接)

這題要倒著推,由N推向1?
設dp[k]為到達k這個位置時得到金幣的期望,m為該點和N這個位置的距離,gold[k]為k這個位置的金幣數(shù),因為走的位置不能超過N,所以要取min(m,6)?
那么dp[k] = 1 / min(m,6) * (dp[k + 1] + dp[k+2] + … + dp[min(m,6)]) + gold[k]。

(其實是因為這題巧了,所以dp[k]可以等概率的由后面的點轉移而來,但是第一種方法更具有普遍性)

#include<cstdio> #include<cmath> #include<algorithm> using namespace std; using namespace std; const int N = 110; int gold[N]; double dp[N]; int n;double solve() {for(int i = 0; i < n - 1; i++) dp[i] = 0;int step;dp[n-1] = gold[n-1];for(int i = n - 2; i >= 0; i--) {dp[i] = gold[i];step = min(6, n-1-i);for(int j = 1; j <= step; j++)dp[i] += 1.0 / step * dp[i+j];}return dp[0]; }int main() {int test, cas = 1;scanf("%d", &test);while(test--) {scanf("%d", &n);for(int i = 0; i < n; i++)scanf("%d", &gold[i]);printf("Case %d: %.7lf\n", cas++, solve());}return 0; }

?

總結

以上是生活随笔為你收集整理的【LightOJ - 1030】Discovering Gold(概率dp,数学期望,期望的线性性)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩视频一二三区 | 国产视频综合在线 | 男人天堂成人网 | 日本黄页网站免费大全 | 天天综合网入口 | 一级片麻豆 | 成年人在线视频网站 | 欧美成人a交片免费看 | 欧美日韩综合在线 | 黄色视屏网站 | 在线视频亚洲欧美 | avtt国产| 97av在线| 最新国产精品 | 欧美成人国产精品一区二区 | 色999日韩| 综合激情四射 | 88av.com | 欧美人与禽猛交乱配 | 奇米四色7777 | 精品国产乱码久久 | 国产少妇在线 | 国产精品第72页 | 精品一区二区三区成人免费视频 | 男女偷爱性视频刺激 | 欧美偷拍综合 | 国产精品videossex国产高清 | av网址免费 | 青青草偷拍视频 | 亚洲视频一区在线播放 | 在线免费观看黄色片 | 白白色2012年最新视频 | 精品国产青草久久久久96 | 黄色链接视频 | 日韩视频精品在线 | 中文字幕有码无码人妻av蜜桃 | 国产欧美综合在线 | 日韩av一区二区在线观看 | 成人免费毛片网站 | 中文字幕av网站 | 中文字幕3区 | 蜜乳av一区| 中文在线国产 | 麻豆视频免费网站 | 紧身裙女教师三上悠亚红杏 | 在线观看免费观看 | 国产老女人乱淫免费可以 | 成人影视免费 | 天天插天天干天天操 | 国产一卡在线 | 欧美理伦少妇2做爰 | 亚洲最大的av网站 | 特级西西人体 | 在线观看av网页 | 亚洲乱码久久 | 欧美成人综合一区 | 国产视频在线观看网站 | 国产精品51 | 中文久久字幕 | 99在线观看免费 | 欧美日韩国产专区 | 秋霞福利视频 | 国产精品美女高潮无套 | 午夜国产一区二区三区 | 一二三区视频 | 亚洲精品1234 | 少妇一级淫片免费观看 | 久久久久久国产精品三级玉女聊斋 | 欧美日韩激情在线 | 特级毛片爽www免费版 | 国产超级av | 国产一级大片在线观看 | 中文字幕伦理 | 国产精品久久久久久久久久 | 天使色吧 | 亚洲精品国产精品乱码视色 | 超碰伊人网 | 欧美一级影院 | 清清草视频 | 天堂网一区 | 亚洲一区天堂 | 一本色道久久综合狠狠躁的推荐 | 长腿校花无力呻吟娇喘的视频 | 色就是欧美 | 一区二区三区不卡视频在线观看 | 啪啪的网站 | 日日操天天操夜夜操 | 乱淫的女高中暑假调教h | 欧美黄色a级大片 | 嫩草视频在线免费观看 | 91精品国产综合久久国产大片 | 一区二区三区欧美日韩 | 韩国一区二区三区四区 | 97蜜桃网| a一级黄色 | 星铁乱淫h侵犯h文 | 张柏芝54张无删码视频 | 国产乱xxⅹxx国语对白 | 爱情岛论坛永久入口 |