LightOJ1027---A Dangerous Maze (期望)

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 n space 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

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

首先设走出去的期望为E, 走一次就出去的概率为p, 走一次出去的平均时间为T1,走一次回到原点的概率为q,平均时间为T2

则有公式:

E=p?T1+q?(T2+E)

如果p不为0 就有解

(1?q)?E=p?T1+q?T2

p?E=p?T1+(1?p)?T2

设可以出去的门的时间和为sumT1, 回到原点的门的时间和为sumT2, 可以出去的门有m扇

所以上式变为:

mn?E= mn ? sumT1m + n?mn ? sumT2n?m

所以: mn?E= sumT1+sumT2n

所以: E= sumT1+sumT2m

/*************************************************************************
    > File Name: A.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年05月17日 星期日 15时23分53秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}

int main() {
    int t;
    int icase = 1;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        int all = 0;
        int cnt = 0;
        int u;
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &u);
            all += abs(u);
            if (u > 0) {
                ++cnt;
            }
        }
        if (!cnt) {
            printf("Case %d: inf\n", icase++);
        }
        else {
            u = gcd(all, cnt);
            printf("Case %d: %d/%d\n", icase++, all/ u, cnt / u);
        }
    }
    return 0;
}
时间: 2024-07-31 00:12:57

LightOJ1027---A Dangerous Maze (期望)的相关文章

lightoj-1027 - A Dangerous Maze(数学期望)

1027 - A Dangerous Maze PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBYou 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 do

LightOJ1027 A Dangerous Maze(期望)

题目大概说你正在起点,面前有$n$个门,每个门有一个数字$x$,正数表示开这个门$x$分钟后会出去,负数表示开这个门$-x$分钟后会回到起点.选择门的概率是一样的且每次选择互不影响.问出去的时间期望是多少. $d[0]$表示从起点出发出去的期望,$d[i](1\leqslant i\leqslant n)$表示选择第i个门出去的期望: 不妨设$1\dots j$的门是正数,$j+1\dots n$的门是负数,那么: $$d[i]=x[i](1\leqslant i\leqslant j)$$ $

lightoj 1027 A Dangerous Maze 期望

设答案为r,cnt为x[i] >=0的个数 那么r = 1/n *  (Σx[i](x[i] >= 0) + ∑(r - x[i])(x[i] < 0)) 然后把r移项到一起解方程, 得到r = ∑|x[i]| / cnt,同除gcd.记得特判下x[i]均为负数的情况即可. 1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 int T,cas,n,tot,gcd,cnt; 5 i

light OJ 1027 A Dangerous Maze (期望)

1027 - A Dangerous Maze PDF (English) Statistics Forum 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

[LOJ 1027] Dangerous Maze

A - A Dangerous Maze Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description 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

[LightOJ 1027] A Dangerous Maze

A Dangerous Maze 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 positio

(期望)A Dangerous Maze(Light OJ 1027)

http://www.lightoj.com/volume_showproblem.php?problem=1027 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 ca

LightOJ 1027 A Dangerous Maze(期望)

https://cn.vjudge.net/problem/LightOJ-1027 题意:有n扇门,每扇门有个时间ti,选择正数的门可以在ti后带你走出迷宫,负数的门会在ti后带你回到起点,然后重新选择,每扇门被选中的概率是一样的,求期望. 思路: 做这种期望的问题必须得列一个方程来求解,不然无限写下去的话算不出. 设走出去的期望的E,对于第三个案例就是 E = 1/3 * 3  + 1/3( 6 + E) + 1/3 (9 + E),(选择第一扇门就直接出去,第二扇门先6分钟回去,然后再花期

LightOJ 1027 A Dangerous Maze 概率期望

题目链接: https://vjudge.net/problem/LightOJ-1027 题目描述: 有N个门, 每个门的选择是等概率的, 如果选择到正数, 我将在正数秒后逃出迷宫, 如果是负数就重新选, 问逃离的期望时间是多少 解题思路: 我这道题犯蠢了, 我认为是.....说不太明白, 看程序吧, 一下子就能看懂, 其中涉及到了一个约分, 但是我没想到就算是long long 也会溢出, 那么我将约分放进每次mul中呢? 也是不行的, 因为都是素数呢, 不是一样会溢出? 自己在半个小时后才