1027 - A Dangerous Maze
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 doors.

If you choose the ith door, it can either take you back to the same position where you begun inxi minutes, or can take you out of the maze afterxi 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 containsn space separated integers. If theith integer(xi)
is positive, you can assume that theith door will take you out of maze afterxi minutes. If it's negative, then theith door will take you back to the beginning position afterabs(xi)
minutes. You can safely assume that1 ≤ 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 inp/q format. Where
p is the numerator of the result andq 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 


题意:你身在一个迷宫里面。如今在你面前有N个门。每一个门要么把你传送出迷宫,要么把你传送到原来的位置且你的记忆也会回到初始的时候。如今给出每一个门传送的时间t。若t为正数,说明该门花费时间t能够将你传送出迷宫,若 t 为负数,说明该门花费时间 t 将你传送到原来的位置。已知你选择每一个门的概率是同样的。问你出迷宫所花费时间的期望。输出结果写出分数形式。且分子分母互质,若不能出迷宫输出inf。



思路:设花费时间 出迷宫的期望为E。

每一个选择仅仅有两种情况——设当前门花费时间的绝对值为 T

一:选择的门能够直接把你传送出去。期望为1 / N * T。

二:选择的门把你传送到原来的位置,期望为1 / N * T。又回到初始状态,则出去的期望为1 / N * (T + E)。


设全部能够将你传送出去的门的时间值 总和为sum1,全部能够将你传送回去的门的时间值 总和为sum2。
设全部能够将你传送出去的门的数目为door1,全部能够将你传送回去的门的数目为door2。
得到等式 

E = 1 / N * (sum1)  + 1 / N * (sum2 + door2 * E)。

化简得 E = (sum1 + sum2) / (N-door2); 当然若door2等于N。说明不可能出迷宫。



AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a%b);
}
int a[110];
int main()
{
int t;
int N;
int k = 1;
scanf("%d", &t);
while(t--)
{
scanf("%d", &N);
int sum1 = 0, sum2 = 0;
int door1 = 0, door2 = 0;
for(int i = 1; i <= N; i++)
{
scanf("%d", &a[i]);
if(a[i] > 0)
{
sum1 += a[i];
door1++;
}
else
{
sum2 += abs(a[i]);
door2++;
}
}
int up = sum1 + sum2;
int down = N - door2;
printf("Case %d: ", k++);
if(door2 == N)
printf("inf\n");
else
printf("%d/%d\n", up / GCD(up, down), down / GCD(up, down));
}
return 0;
}


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

    A Dangerous Maze You are in a maze; seeing n doors in front of you in beginning. You can choose any ...

  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(期望)题解

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

  5. LightOJ 1027 A Dangerous Maze(期望)

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

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

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

  7. LightOj 1027 A Dangerous Maze【概率】

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

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

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

  9. LightOJ - 1027 Dangerous Maze 期望

    你在迷宫中;开始时在你面前看到n扇门.你可以选择你喜欢的任何门.所有门的选择门的概率是相等的. 如果您选择第i个门,它可以让您回到您在xi(xi小于0)分钟内开始的相同位置,也可以在xi(xi大于0) ...

随机推荐

  1. Android学习——蓝牙通讯

    蓝牙蓝牙,是一种支持设备短距离通信(一般10m内,且无阻隔媒介)的无线电技术.能在包括移动电话.PDA.无线耳机.笔记本电脑等众多设备之间进行无线信息交换.利用“蓝牙”技术,能够有效的简化移动通信终端 ...

  2. ALTER USER - 改变数据库用户帐号

    SYNOPSIS ALTER USER name [ [ WITH ] option [ ... ] ] where option can be: [ ENCRYPTED | UNENCRYPTED ...

  3. mysql5.7 this is incompatible with sql_mode=only_full_group_by错误

    解决办法: https://blog.csdn.net/qq_42175986/article/details/82384160 前言: 一.原理层面 这个错误发生在mysql 5.7 版本及以上版本 ...

  4. CAD参数绘制椭圆弧(com接口)

    在CAD设计时,需要绘制椭圆弧,用户可以设置椭圆弧基本属性. 主要用到函数说明: _DMxDrawX::DrawEllipseArc 绘制椭圆弧.详细说明如下: 参数 说明 DOUBLE dCente ...

  5. console.log()与console.dir()

    console.log()可以取代alert()或document.write(),在网页脚本中使用console.log()时,会在浏览器控制台打印出信息. console.dir()可以显示一个对 ...

  6. javascript——js string 转 int 注意的问题——parseInt(转)

    <script>     var   str='1250' ;  alert( Number(str) );  //得到1250 alert(parseInt(str));  //得到12 ...

  7. LAME的“命令行”

    VBR 编码 (强烈推荐) Alt Preset Extreme (平均256kbps) 我们有时在网上可以看到".LAME-APX." 就是这种形式,我们也可以在文件名中包含这个 ...

  8. IOS 3D UI --- CALayer的transform扩展

    http://www.cocoachina.com/bbs/read.php?tid=117061 例子代码可以在 http://download.csdn.net/detail/worldmatri ...

  9. 23Spring使用JdbcTemplate和JdbcDaoSupport

    首先需要添加c3p0包和jdbc包 数据库: CREATE DATABASE IF NOT EXISTS `spring` /*!40100 DEFAULT CHARACTER SET utf8 */ ...

  10. Tomcat处理HTTP请求原理

    一.Tomcat是什么? Tomcat是一个Web应用服务器,同时也是一个Servlet/JSP容器.Tomcat作为Servlet容器,负责处理客户端请求,把请求传送给Servlet,并将Servl ...