[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 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的更多相关文章
- LightOJ - 1027 A Dangerous Maze —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1027 1027 - A Dangerous Maze PDF (English) Statistics For ...
- Lightoj 1027 - A Dangerous Maze 【期望】
1027 - A Dangerous Maze PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Y ...
- LightOJ 1027 - A Dangerous Maze(求期望)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1027 题意:又一个迷宫,有n个门,每个门又一个值num,如果num>0 说明在n ...
- LightOj 1027 A Dangerous Maze【概率】
题目链接:http://www.lightoj.com/volume_showproblem.php? problem=1027 题意: 你面前有n个门,每一个相应一个数字,若为正xi.代表xi分钟后 ...
- LightOJ 1027 A Dangerous Maze(期望)题解
题意:n扇门,每扇门后都有一个值x,如果x<0会让你等待-x再重新回到这里选择门,x>0你经过x时间就会被传送走,问你被传送走的期望 思路:假设被传送走的期望为E,那么对于x<0来说 ...
- LightOJ 1027 A Dangerous Maze(期望)
https://cn.vjudge.net/problem/LightOJ-1027 题意:有n扇门,每扇门有个时间ti,选择正数的门可以在ti后带你走出迷宫,负数的门会在ti后带你回到起点,然后重新 ...
- LightOJ 1027 A Dangerous Maze (数学期望)
题意:你面前有 n 个门,每次你可以选择任意一个进去,如果xi是正数,你将在xi后出去,如果xi是负数,那么xi后你将回来并且丢失所有记忆,问你出去的期望. 析:两种情况,第一种是直接出去,期望就是 ...
- LightOJ - 1395 A Dangerous Maze (II) —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1395 1395 - A Dangerous Maze (II) PDF (English) Statistic ...
- Light OJ 1027 - A Dangerous Maze (数学-期望)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1027 题目大意: 一个迷宫, 有n个门,选择一个门花费为|ai|, 如果选择的 ...
随机推荐
- SQL中 根据行号设置每行数据的排序数值
根据行号自动把当前行号插入到某列中 实现排序 update tempTable set DisplayOrder = right( CAST(rownum as NVARCHAR),5) from( ...
- Eclispe中编辑xml配置文件时不会提示也不能自动调整格式
创建了一个xml文件后,发现编辑起来和原来的那些有所不同,不会提示补全.也不能自动调整格式???woc? 哈哈哈哈哈,“我最恨你像个石头一样” 后来发现是编辑器被改了!!! 右键xml文件然后open ...
- anusplina 4.36版本使用提示 说明
1),必须要注意的是,你生成的dat文件中,第一列必须是气象站点编号:第二列必须是经度数值,而且是投影坐标下,以 米 为单位:第三列必须是纬度了:第四列必须是海拔了:之后就是你自己的数据,比如平均温度 ...
- 《剑指offer》第五十三题(数组中数值和下标相等的元素)
// 面试题53(三):数组中数值和下标相等的元素 // 题目:假设一个单调递增的数组里的每个元素都是整数并且是唯一的.请编程实 // 现一个函数找出数组中任意一个数值等于其下标的元素.例如,在数组{ ...
- Django框架中,使用celery实现异步
作用:在使用框架时,在视图函数中实现异步构成: 任务task:一段耗时并与响应结果无关的代码,如发短信 工人worker:新进程,用于执行任务代码 代理人broker:调用任务时,将任务添加到队列中, ...
- WebStorm Error : program path not specified
1.出现这个错误是由于没有设置Node.js路径引起的. 2.下载安装Node.js. 3.设置对应的路径,设置后点一下Enable按钮即可. 以上,完.
- Error updating database:线程异常
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: See http://www.slf4 ...
- jquey 小记
1. $.each(array, [callback]) 遍历[常用] 解释: 不同于例遍jQuery对象的$().each()方法,此方法可用于例遍任何对象. 回调函数拥有两个参数: 第一个为对象的 ...
- 关于ORA-00979 不是 GROUP BY 表达式错误的解释
ORA-00979 不是 GROUP BY 表达式”这个错误,和我前面介绍的另外一个错误ORA-00937一样使很多初学oracle的人爱犯的. 我在介绍使用聚合函数中用group by来分组数据时特 ...
- Squid代理配置
更改squid错误页面时间不对的问题解压源码包,进入此路径/usr/local/squid/share/errors/zh-cn(需要更改该目录下的所有文件find -type f |xargs se ...