hdu 4586 (機率+期望)

來源:互聯網
上載者:User

標籤:數學   期望   

http://acm.hdu.edu.cn/showproblem.php?pid=4586


大致題意:有一個骰子有n個面,擲到每一個面的機率是相等的,每一個面上都有相應的錢數。其中當你擲到m個面之一時,你有多擲一次的機會。問最後所得錢數的期望。


思路:設投擲第一次的期望是p,那麼第二次的期望是m/n*p,第三次的期望是 (m/n)^2*p......第N次的期望是(m/n)^(N-1)*p。

那麼這些期望之和便是答案。之前也是想到這,但不知道如何處理無限的情況。當時腦卡了,這不是赤裸裸的等比數列嗎?

設q = m/n,公比就是q,本題中等比數列之和為p*(1-q^N)/(1-q)。分三種情況討論:當p為0時,輸出0.00;當q等於1時,說明可以無限的投擲下去,輸出inf;當q < 1時,N無窮大時,1-q^N地區1,那麼原式變為p/(1-q)。


#include <stdio.h>#include <iostream>#include <map>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL long long#define _LL __int64#define eps 1e-8#define PI acos(-1.0)using namespace std;int n,m;int vis[210];int a[210];int sum,cnt;double p,q;int main(){while(~scanf("%d",&n)){sum = 0;cnt = 0;memset(vis,0,sizeof(vis));for(int i = 1; i <= n; i++){cin >> a[i];sum += a[i];}p = (sum*1.0)/n;scanf("%d",&m);int x;for(int i = 1; i <= m; i++){cin >> x;if(vis[x]) continue;cnt++;vis[x] = 1;}q = (cnt*1.0)/n;if(fabs(p) < eps)cout << "0.00" << endl;else if(fabs(q-1) < eps)cout << "inf" << endl;elseprintf("%.2lf\n",p/(1-q));}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.