[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|, 如果选择的 ...
随机推荐
- Lintcode415-Valid Palindrome-Medium
Given a string, determine if it is a palindrome, considering only alphanumeric(字母和数字) characters and ...
- PHP 常见的数据加密技术
单项散列加密技术(不可逆的加密) 把任意长的输入字符串变化为固定长的输出串的一种函数 MD5 string md5 ( string $str [, bool $raw_output = false ...
- 什么是 MIME TYPE?
文章来源: http://baike.baidu.com/item/MIME https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_H ...
- git 修改文件夹名字后如何提交
将文件夹名字从 v1.0.1 修改为 v1.0.2 git add --ignore-removal "v1.0.2/xsxsx"
- Android JNI 数组操作
JNI 中有两种数组操作,基础数据类型数组和对象数组,JNI 对待基础数据类型数组和对象数组是不一样的. 基本数据类型数组 对于基本数据类型数组,JNI 都有和 Java 相对应的结构,在使用起来和基 ...
- 学习笔记29—Linux基础
一.简单命令: 1.进入文件夹heyi目录:cd ./heyi 2.查看该目录下内容:ls 二.linux压缩和解压缩命令大全 tar命令 解包:tar zxvf FileName.tar 打包:ta ...
- linux基础01-bash特性
(1) ls -l:长格式文件类型: -:普通文件 (f) d: 目录文件 b: 块设备文件 (block) c: 字符设备文件 (character) l: 符号链接文件(symbolic link ...
- js判断字符串与字符串相互包含,以及数组是否包含某个元素;
需求:判端一个字符串是否包含另一个字符串? 实现: var str = "adc"; 判断str 中是否包含 "c" if( str.indexOf(&quo ...
- Asp.net core 学习笔记 ( ViewComponent 组件 )
refer : https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components core 和 Angular 的 comp ...
- Windows Updateエラー 80072EE2
遗憾的是,下面的做完了,还是没用! 然后有试了把防火墙打开(原来是关闭),IE的代理也是手动入力(原来是自动寻找). 竟然成了. Windows ファイアウォールはこのバージョンの Windows に ...