You are in a maze; seeing n doors in front of you inbeginning. You can choose any door you like. The probability for choosing adoor is equal for all doors.
If you choose the ith door, it can eithertake you back to the same position where you begun in ximinutes, or can take you out of the maze after xi minutes. Ifyou come back to the same position, you can't remember anything. So, every timeyou come to the beginning position, you have no past experience.
Now you want to find the expected time to get out of themaze.
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 the ith integer (xi)is positive, you can assume that the ith door will take youout of maze after xi minutes. If it's negative, then the ithdoor 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 timeto 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 theresult and q is the denominator of the result and they are relativelyprime. 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 |
确实是有无限种可能,一直往下递归,然后我就不会了。
我们直接用 E当做结果。
E=1/k(a1+a2+...+an)+1/k(A1+E)+1/k*(A2+E)+...+1/k*(A3+E)+1/k*(Am+E);
a?表示的是选择正值的时间,因为一下子就出去了。
A?表示的是负数的绝对值,因为在负数的地方又会回到选择点,所以他出去的时间还得加上E。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
typedef long long ll;
using namespace std;
const int maxn=100+10;
int val[maxn];
int gcd(int x,int y){
return y==0?x:gcd(y,x%y);
}
int main()
{
int T;
scanf("%d",&T);
int cnt=1;
while(T--) {
int k;
scanf("%d",&k);
int m=0,n=0;
for(int i=1; i<=k; i++) {
scanf("%d",&val[i]);
if(val[i]>0) n++;
else
m++;
}
printf("Case %d: ",cnt++);
if(n==0)
printf("inf\n");
else{
int sn=0,sm=0;
for(int i=1;i<=k;i++){
if(val[i]>0)
sn+=val[i];
else
sm+=(-val[i]);
}
int sum=sn+sm;
int dif=k-m;
if(sum<dif)
swap(sum,dif);
int mx=gcd(sum,dif);
printf("%d/%d\n",sum/mx,dif/mx);
}
}
return 0;
}