HDU 4586 (probability + expectation)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4586


Question: There is a dice with N faces. The probability of throwing a dice to each of them is equal, and each of them has a corresponding amount of money. When you throw M faces, you have the chance to throw more than once. Ask the expectation of the final amount of money.


Idea: if the first expectation for throwing is P, the second expectation is M/N * P, and the third expectation is (M/N) ^ 2 * p ...... the expectation for n times is (M/N) ^ (N-1) * P.

The sum of these expectations is the answer. I thought of this before, but I don't know how to deal with infinite situations. At that time, the brain got stuck. Isn't this a naked proportional series?

Set q = M/N, and the public ratio is Q. In this question, the medium ratio is p * (1-Q ^ N)/(1-Q ). Three cases are discussed: When P is 0, the output is 0.00; when Q is equal to 1, it means that the throw can be infinite, and the output is INF; when Q is less than 1, n is infinite, 1-Q ^ n region 1, then the original formula is 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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.