Lightoj 1027 - A Dangerous Maze 【期望】

1027 - A Dangerous Maze
Time Limit: 2 second(s) Memory Limit: 32 MB

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 inxi minutes, or can take you out of the maze afterxi 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 containsn space separated integers. If theith integer(xi) is positive, you can assume that theith door will take you out of maze afterxi minutes. If it's negative, then theith door will take you back to the beginning position afterabs(xi) minutes. You can safely assume that1 ≤ 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 inp/q format. Where p is the numerator of the result andq 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 


题意:你身在一个迷宫里面。如今在你面前有N个门。每一个门要么把你传送出迷宫,要么把你传送到原来的位置且你的记忆也会回到初始的时候。如今给出每一个门传送的时间t。若t为正数,说明该门花费时间t能够将你传送出迷宫,若 t 为负数,说明该门花费时间 t 将你传送到原来的位置。已知你选择每一个门的概率是同样的。问你出迷宫所花费时间的期望。输出结果写出分数形式。且分子分母互质,若不能出迷宫输出inf。



思路:设花费时间 出迷宫的期望为E。

每一个选择仅仅有两种情况——设当前门花费时间的绝对值为 T

一:选择的门能够直接把你传送出去。期望为1 / N * T。

二:选择的门把你传送到原来的位置,期望为1 / N * T。又回到初始状态,则出去的期望为1 / N * (T + E)。


设全部能够将你传送出去的门的时间值 总和为sum1,全部能够将你传送回去的门的时间值 总和为sum2。
设全部能够将你传送出去的门的数目为door1,全部能够将你传送回去的门的数目为door2。
得到等式 

E = 1 / N * (sum1)  + 1 / N * (sum2 + door2 * E)。

化简得 E = (sum1 + sum2) / (N-door2); 当然若door2等于N。说明不可能出迷宫。



AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
int GCD(int a, int b)
{
   return b == 0 ? a : GCD(b, a%b);
}
int a[110];
int main()
{
    int t;
    int N;
    int k = 1;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &N);
        int sum1 = 0, sum2 = 0;
        int door1 = 0, door2 = 0;
        for(int i = 1; i <= N; i++)
        {
            scanf("%d", &a[i]);
            if(a[i] > 0)
            {
                sum1 += a[i];
                door1++;
            }
            else
            {
                sum2 += abs(a[i]);
                door2++;
            }
        }
        int up = sum1 + sum2;
        int down = N - door2;
        printf("Case %d: ", k++);
        if(door2 == N)
            printf("inf\n");
        else
            printf("%d/%d\n", up / GCD(up, down), down / GCD(up, down));
    }
    return 0;
}



posted @ 2017-07-13 11:17  mfmdaoyou  阅读(612)  评论(0编辑  收藏  举报