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