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 ximinutes, 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

3

1

1

2

-10 -3

3

3 -6 -9

Sample Output

Case 1: 1/1

Case 2: inf

Case 3: 18/1

题目的大意是,有n扇门,一些会在xi秒后带你立刻迷宫,一些会让你回到-xi秒前,选择每扇门的概率相同,求离开迷宫所需时间的期望.

这是期望数学的入门题.

我们设出去的期望为E,选正数的概率为P1,之后平均花T1的时间出去;选负数的概率是P2,之后平均花T2的时间出去。

则E=P1*T1+P2*(T2+E);

解得:E=(P1T1+P2T2)/ (1-P2)=(P1T1+P2T2)/ P1

我们再设正权和为S1,负权和为S2,正权数为N1,负权数为N2,总数为N,则:

T1*N1/N+T2*N2/N      (S1+S2)/N    S1+S2

E=-----------------------------=----------------=----------

N1/N                      N1/N            N1

如果N1为0,那永远走不出去,输出inf就行了.

问题就解决了.

 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 using namespace std;
 int n;
 ?x:gcd(y,x%y);}
 int main(){
     int T; scanf("%d",&T);
     ; Ts<=T; Ts++){
         scanf("%d",&n);
         ,sum_neg=,num_pos=,num_neg=;
         ; i<=n; i++){
             int x; scanf("%d",&x);
             ) sum_pos+=x,num_pos++; else sum_neg-=x,num_neg++;
         }
         printf("Case %d: ",Ts);
         ){printf("inf\n"); continue;}
         int x=sum_pos+sum_neg,y=n-num_neg,K=gcd(x,y);
         printf("%d/%d\n",x/K,y/K);
     }
     ;
 }

[LightOJ 1027] A Dangerous Maze的更多相关文章

  1. LightOJ - 1027 A Dangerous Maze —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1027 1027 - A Dangerous Maze    PDF (English) Statistics For ...

  2. Lightoj 1027 - A Dangerous Maze 【期望】

    1027 - A Dangerous Maze PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Y ...

  3. LightOJ 1027 - A Dangerous Maze(求期望)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1027 题意:又一个迷宫,有n个门,每个门又一个值num,如果num>0 说明在n ...

  4. LightOj 1027 A Dangerous Maze【概率】

    题目链接:http://www.lightoj.com/volume_showproblem.php? problem=1027 题意: 你面前有n个门,每一个相应一个数字,若为正xi.代表xi分钟后 ...

  5. LightOJ 1027 A Dangerous Maze(期望)题解

    题意:n扇门,每扇门后都有一个值x,如果x<0会让你等待-x再重新回到这里选择门,x>0你经过x时间就会被传送走,问你被传送走的期望 思路:假设被传送走的期望为E,那么对于x<0来说 ...

  6. LightOJ 1027 A Dangerous Maze(期望)

    https://cn.vjudge.net/problem/LightOJ-1027 题意:有n扇门,每扇门有个时间ti,选择正数的门可以在ti后带你走出迷宫,负数的门会在ti后带你回到起点,然后重新 ...

  7. LightOJ 1027 A Dangerous Maze (数学期望)

    题意:你面前有 n 个门,每次你可以选择任意一个进去,如果xi是正数,你将在xi后出去,如果xi是负数,那么xi后你将回来并且丢失所有记忆,问你出去的期望. 析:两种情况,第一种是直接出去,期望就是 ...

  8. LightOJ - 1395 A Dangerous Maze (II) —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1395 1395 - A Dangerous Maze (II)    PDF (English) Statistic ...

  9. Light OJ 1027 - A Dangerous Maze (数学-期望)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1027 题目大意: 一个迷宫, 有n个门,选择一个门花费为|ai|, 如果选择的 ...

随机推荐

  1. UVA1025 城市里的间谍

    #include<iostream> #include<cstdio> #include<memory.h> using namespace std; #defin ...

  2. python学习 day014打卡 内置函数二&递归函数

    本节主要内容: 1.lambda匿名函数 2.sorted() 3.filter() 4.map() 5.递归函数 6.二分法 一.lambda匿名函数 为了解决一些简单的需求而设计的一句话函数 # ...

  3. appium长按按钮

    public static AndroidDriver driver; /长按操作:waitAction的参数单位是ms/ public static void longClick(String id ...

  4. H5手机移动端调起浏览器(qq浏览器,uc浏览器)自带分享功能实例

    H5手机移动端调起浏览器(qq浏览器,uc浏览器)自带分享功能实例 (转载:https://blog.csdn.net/weixin_38787928/article/details/86741227 ...

  5. colgroup和col的区别

    转载自:http://blog.csdn.net/carefree31441/article/details/3291397 colgroup和col一般出现在表格当中定义表格单独列的任意属性col能 ...

  6. eclipse创建web项目web.xml配置文件笔记

    1.使用eclipse创建web项目时,如果直接finish的话就没有默认生成web.xml配置文件,此时在你的项目下是看不到web.xml配置文件的,如果要查看的话可以如下操作: 右键你的项目,然后 ...

  7. 学习笔记31—endnote设置

    修改文献引用设置: JournalArticle: Author. (Year). Title. [Translated Title]. [Reviewed Item]. Journal|, Volu ...

  8. 华硕N55SF 折腾记

    家里给老妈用的N55SL是2011年买的.这么多年了,最近一身毛病,最近来回折腾很多次.吐槽记录如下 1不定时花屏.闪屏,然后偶尔硬盘掉盘 (找不到硬盘,开机BIOS里).主要是使用一段时间之后发热, ...

  9. ubuntu 配置sublime text3

    1安装st3(http://blog.csdn.net/zhaoyu106/article/details/52858962) sudo apt-get update sudo apt-get ins ...

  10. U8工具栏特别小是怎么回事

    用友的工具栏特别窄了,填制凭证里的保存.增加凭证等按钮因为工具栏特别窄都看不清了 解决方法:正常机器下的system32下面的mscomctl.ocx文件替换到有问题的机器下,您的系统应该是XP的,这 ...