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;}