You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors.
If you choose the ith door, it can either take you back to the same position where you begun in xi minutes, or can take you out of the maze after xi minutes. If you come back to the same position, you can't remember anything. So, every time you come to the beginning position, you have no past experience.
Now you want to find the expected time to get out of the maze.
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 number of doors. The next line contains n space separated integers. If the ith integer (xi) is positive, you can assume that the ith door will take you out of maze after xi minutes. If it's negative, then the ith door will take you back to the beginning position after abs(xi) minutes. You can safely assume that 1 ≤ abs(xi) ≤ 10000.
Output
For each case, print the case number and the expected time to get out of the maze. If it's impossible to get out of the maze, print 'inf'. Print the result in p/qformat. Where p is the numerator of the result and q is the denominator of the result and they are relatively prime. See the samples for details.
Sample Input | Output for Sample Input |
3
1 1
2 -10 -3
3 3 -6 -9 | Case 1: 1/1 Case 2: inf Case 3: 18/1 |
PROBLEM SETTER: JANE ALAM JAN
设在一次试验中,事件A首次发生的概率为p(0<p<1),则在伯努利试验序列中,事件A在第 k 次试验中才首次发生的概率为
。
在第n次伯努利试验中,ξ表示是事件A第一次成功的试验的第次,详细的说是:前ξ-1次皆失败,第ξ次成功。如果事件A发生的概率是p,则不发生的概率q=1-p,n次独立重复试验中,第k次试验是事件A的第一次成功的概率是:P(ξ=k)=
(k=0,1,2,3…n),那么就说ξ 服从参数p的几何分布,其中p称为成功概率。
(1)几何分布的期望Eξ=
;
(2)几何分布的方差Dξ=
。
期望=可以将你送出去时间和/N+不可以将你送出去时间之和/N+为负数之和*期望/N
也可以这样:可以看做伯努利实验,一次可以出去的期望是N/k=E,那么E*平均可以出去的时间sum/N->sum/k就是答案。
sum为各个时间的绝对值。
参考:https://blog.csdn.net/duan_1998/article/details/73027356
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int a[110];
int gcd(int a,int b)
{
return (b==0)?a:gcd(b,a%b);
}
int main()
{
int n;
int i,j,t;
scanf("%d",&t);
for(int p = 1;p<=t;p++)
{
scanf("%d",&n);
int num = 0;
int sum = 0;
for(i = 0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>0)
num++;
sum += abs(a[i]);
}
printf("Case %d: ",p);
if(num==0) printf("inf\n");
else
{
int g = gcd(sum,num);
printf("%d/%d\n",sum/g,num/g);
}
}
return 0;
}