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. vue2.0 v-model指令

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 手机酷派4G5316 5313s 黑砖 求转成功 9008端口 9006端口 少走弯路选对镜像

    首先要有资料 里面有教程  http://pan.baidu.com/s/1bpjxP6n 1.用其他手机 or u 盘往sd卡放进“强制进入下载模式的文件” 2. 驱动 3.刷机工具 下载镜像   ...

  3. myslq 5.7 root 默认密码

    sudo sumysqld_safe --skip-grant-tables --skip-networking & UPDATE mysql.user SET password=PASSWO ...

  4. CAD参数绘制半径标注(网页版)

    主要用到函数说明: _DMxDrawX::DrawDimRadial 绘制一个半径标注.详细说明如下: 参数 说明 DOUBLE dCenterX 被标注的曲线的中点X值 DOUBLE dCenter ...

  5. CSS hover 改变另外一个元素状态

    Part.1 问题 我们写页面时也不少遇到这个问题,在没有使用任何预处理语言前提下,当hover 一个元素的时候怎么改变其它的元素? 这里我把它分为两种情况(除自身以外) hover时 1: 改变本身 ...

  6. 单页vue路由router

    Vue.js + vue-router 可以很简单的实现单页应用. <router-link> 是一个组件,该组件用于设置一个导航链接,切换不同 HTML 内容. to 属性为目标地址, ...

  7. 并发2-Synchronized

    一.Synchronized的概念 是利用锁的机制来实现同步的. 锁机制有如下两种特性: 互斥性:即在同一时间只允许一个线程持有某个对象锁,通过这种特性来实现多线程中的协调机制,这样在同一时间只有一个 ...

  8. Android中notifyDataSetInvalidated()和notifyDataSetChanged()有什么区别

     看下源码中对于这两个方法   public void notifyDataSetChanged () 该方法内部实现了在每个观察者上面调用onChanged事件.每当发现数据集有改变的情况,或者读取 ...

  9. Class加载顺序

    原文:https://blog.saymagic.cn/2017/07/01/class-common-question.html 类的初始化顺序是怎样的? 我们尝试从class文件中找到答案.来看这 ...

  10. 色码表 Color code table

    最近打算更新设计博客页面,需要用到CSS色码表,查了一些资料现转载此处以备以后使用,点击此处查看原文,另外还发现了几个不错的网站: color-hex HTML颜色代码 色碼表 色碼表英文為 Colo ...