Dangerous Maze LightOJ - 1027(期望概率)

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/q format. 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

3



1

1



2

-10 -3



3

3 -6 -9

Sample Output

Case 1: 1/1

Case 2: inf

Case 3: 18/1

题目大意:
大概就是每次选择一个门,如果是正数,则花费ai时间就逃离,如果是负数,则花费-a[i]回到原地,求花费时间的期望

思路

感觉期望的题都是把和的期望换成期望的和,那么显然这题可以列出等式
E(x) = E[x1+x2+x3+x4…..+xn) 其中 xi就等于,在第i个门所花费的时间期望,
继而等于 E(x1) + E(x2) + E(x3)+E(x4)+…….+E(xn)
那么考虑每一个门的期望,当门是正数时,就是1/n*a[i]
当门是负数时期望是1/n*(a[i]+x) 其中x为期望 ,如果是无限种可能的大概都可以这么搞
然后化简就行了

accode

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
const int maxn = 1e5+53;
const LL mod = 998244353;
int T;
int n;
LL a[maxn];
int main()
{
    scanf("%d",&T);
    int ka = 1;
    while(T--){
        scanf("%d",&n);
        LL sum = 0;
        LL cnt = 0;
        for(int i = 1;i<=n;i++){
            scanf("%lld",&a[i]);
            sum+=abs(a[i]);
            if(a[i]>=0){
                cnt++;
            }
        }
        printf("Case %d: ",ka++);
        if(cnt<=0){
            puts("inf");
            continue;
        }
         LL gcd = __gcd(sum,cnt);
         printf("%lld/%lld\n",sum/gcd,cnt/gcd);
    }
}

猜你喜欢

转载自blog.csdn.net/w571523631/article/details/81392976