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. javascript创建函数的20种方式汇总

    http://www.jb51.net/article/68285.htm 工作中常常会创建一个函数来解决一些需求问题,以下是个人在工作中总结出来的创建函数20种方式,你知道多少? function ...

  2. 模块、包及常用模块(time/random/os/sys/shutil)

    一.模块 模块的本质就是一个.py 文件. 导入和调用模块: import module from module import xx from module.xx.xx import xx as re ...

  3. Lintcode35-Reverse Linked List-Easy

    35. Reverse Linked List Reverse a linked list. Example Example1:For linked list 1->2->3, the r ...

  4. Centos 7下添加新用户并授权

    1.创建一个 xiaoyang 用户 [root@VM_81_181_centos ~]# adduser xiaoyang 2.为创建的用户设置密码 [root@VM_81_181_centos ~ ...

  5. centos7 修改密码

    Centos7破解密码的方法   Centos7忘记密码   在工作或者自己练习的时候我们难免会大意忘掉自己的root密码,有些同学忘掉密码竟然第一选择是重装系统,工作中可万万使不得! 本篇博客将讲解 ...

  6. JavaSE习题 第六章 字符串和正则表达式

    Make efforts eveyday 问答题 1.对于字符串 String s1=new String("ok"); String s2=new String("ok ...

  7. javaSE习题 第一章 JAVA语言概述

    转眼就开学了,正式在学校学习SE部分,由于暑假放视频过了一遍,略感觉轻松,今天开始,博客将会记录我的课本习题,主要以文字和代码的形式展现,一是把SE基础加强一下,二是课本中有很多知识是视频中没有的,做 ...

  8. Android开发代码规范总结

    本篇开始总结Android开发中的一些注意事项,提高代码质量(仅供参考): 1.  Activity间的数据通信,对于数据量比较大的,避免使用 Intent + Parcelable 的方式,可以考虑 ...

  9. SpringBoot 文件上传、下载、设置大小

    本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...

  10. nodejs导出excel

    //导出Excel var nodeExcel = require("excel-export"); var fs = require("fs"); var c ...