[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|, 如果选择的 ...
随机推荐
- CPU指令集设计RISC和CISC
CPU指令集 硬件实现具有速度快,成本高,灵活性差,软件实现与之相反.因此出现频率高的基本功能首选硬件实现.指令集的不同反映了设计原理.制造技术和系统类别. RISC 精简指令集计算机(Reduced ...
- sublime text 中文显示异常
本文链接:sublime text 中文显示异常 http://www.cnblogs.com/daysme/p/7549857.html 前言 sublime text 3143 用了一年时候,最近 ...
- FB01与F-02的区别(转载)
FB01与F-02的区别(转载) FB01 : a) ''Post Document'' b) No doc type as SA automatically comes. c) No automat ...
- 创建react项目
npm搭建React项目 React官网提供最简便的方法是使用create-react-app npx create-react-app my-app cd my-app npm start 也可以自 ...
- 《spring boot 实战》读书笔记
前言:虽然已经用spring boot开发过一套系统,但是之前都是拿来主义,没有系统的,全面的了解过这套框架.现在通过学习<spring boot实战>这本书,希望温故知新.顺便实现自己的 ...
- dbm.error: need 'c' or 'n' flag to open new db
#coding=utf-8 import shelve with shelve.open("shelve.ini","w") as f: f["k1& ...
- 服务器不能设置内容类型HTTP头信息后发送
昨天一个用户反映,导出excel失败,我测试了一下,数据量小没有问题,数据量稍微大就会出现这个问题.咨询度娘也没有找到合适的解决方法,突然想到系统中,其他模块有下载excel附件的功能,但是从没有出现 ...
- Integer的最大值
来自:https://blog.csdn.net/qq_33611068/article/details/77369050 有这样一道题: 编程测试,遍历 0 到 int所能表示最大的正数,将消耗的时 ...
- 学习笔记42—Win7下安装Linux双系统
1.下载Linux镜像:http://mirrors.163.com/ubuntu-releases/18.04.1/ 方法一: 1.用软通牒软件将Linux的镜像写入空的优盘中, 具体如下: 1) ...
- P10.3 usestock0.cpp
stock.h #ifndef STOCK_H #define STOCK_H #include <string> class Stock //类声明 { private: std::st ...