LightOJ 1027 A Dangerous Maze(期望)题解

题意:n扇门,每扇门后都有一个值x,如果x<0会让你等待-x再重新回到这里选择门,x>0你经过x时间就会被传送走,问你被传送走的期望

思路:假设被传送走的期望为E,那么对于x<0来说,贡献的期望为1 / n * (-x + E),对于x>0贡献的期望为1 / n * x,所以E等于这两者之和,列等式,化简之后可求得E

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 100 + 10;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int gcd(int a, int b){
    return b == 0? a : gcd(b, a % b);
}
int main(){
    int n, t, ca = 1, x;
    scanf("%d", &t);
    while(t--){
        int sum = 0, cannt = 0;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++){
            scanf("%d", &x);
            sum += x > 0? x : -x;
            if(x < 0) cannt++;
        }
        printf("Case %d: ", ca++);
        if(cannt == n) printf("inf\n");
        else{
            int Gcd = gcd(sum, n - cannt);
            printf("%d/%d\n", sum / Gcd, (n - cannt) / Gcd);
        }
    }
    return 0;
}

 

posted @ 2019-03-07 10:55  KirinSB  阅读(152)  评论(0编辑  收藏  举报