题干:
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 nspace 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
题目大意:
在n个门前选择一扇门出去, 然后如果第i扇门的 Xi值是正的话,你会花费Xi时间后出去 , 如果Xi是负数的话你会花费-Xi时间后回到老地方,并且忘记了刚才的选择, 选择一扇门的概率是等概的。求出去的期望。
解题报告:
设任意选择一个门,选择到正权值的概率是P1,负权值的概率是P2,正权值的平均值是v1,负权值的平均值是v2,设要求的期望是E,那么,就可以找到一个方程来:
其实是找到了和如何回归到原来的状态,从而可以写出方程。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int a[MAX];
int main()
{int t,iCase=0;cin>>t;while(t--) {int zheng=0,fu=0,z=0,f=0,n;scanf("%d",&n);for(int i = 1; i<=n; i++) {scanf("%d",a+i);if(a[i] > 0) zheng += a[i],z++;else fu += -a[i],f++;;}printf("Case %d: ",++iCase);if(z == 0) {puts("inf");continue;}// zheng+fu/zint g = __gcd(zheng+fu,z);printf("%d/%d\n",(zheng+fu)/g,z/g);}return 0 ;
}