题目链接:https://vjudge.net/problem/LightOJ-1027

1027 - A Dangerous Maze
    PDF (English) Statistics Forum
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 in xi minutes, 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 ithdoor 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

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

题意:

有n扇门,一扇门要么能在特定时间内把人带出迷宫,要么能在特定时间内把人带会原地,并使人失去记忆(即不知道这扇门是不能带出迷宫的),每扇门被选择的几率是相等的。问走出迷宫的平均时间。

题解:

设期望为EX,有t1扇“正门”,t2扇“负门”,其中t1+t2 = n 。第i扇门所花费的时间为ai

1.当选择的门为正门时,它可以直接走出迷宫,因此:1/n*ai,1/n为选择这扇门的几率,且所有的门被选择的几率也为1/n。

2.当选择的门为负门时,它先花费了ai的时间,然后又重新选择,重新选择然能走出迷宫的时间即为平均时间,因此:1/n*(ai+EX)。

3.综上,EX = ∑1/n*ai + ∑1/n*(ai+EX),其中第一个i的范围为:1<=i<=t1,第二个i:1<=i<=t2。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 1e6+100;
18
19 int gcd(int a, int b)
20 {
21     return b==0?a:gcd(b,a%b);
22 }
23
24 int main()
25 {
26     int T, n, kase = 0;
27     scanf("%d", &T);
28     while(T--)
29     {
30         scanf("%d", &n);
31         int numP = 0, sum = 0;
32         for(int i = 1; i<=n; i++)
33         {
34             int val;
35             scanf("%d", &val);
36             sum += abs(val);
37             if(val>0) numP++;
38         }
39         if(numP==0)
40             printf("Case %d: inf\n", ++kase);
41         else
42             printf("Case %d: %d/%d\n", ++kase, sum/gcd(sum, numP), numP/gcd(sum, numP));
43     }
44 }

View Code

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8442427.html

LightOJ - 1027 A Dangerous Maze —— 期望相关推荐

  1. LightOj 1027 A Dangerous Maze

    一个迷宫有n扇门,走第i扇门时间为xi,若xi为正,则走出迷宫,若xi为负,则回到原来位置并忘记已走过的门.问走出迷宫的时间期望,若不能走出迷宫输出inf,否则以分数形式输出p/q. 题目链接 我们设 ...

  2. LightOJ 1395 A Dangerous Maze (II) 期望DP

    A Dangerous Maze (II) LightOJ - 1395 这个题是 LightOJ 1027 A Dangerous Maze 基础概率DP 的加强版. 有 nnn 个门,其中有些门通 ...

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

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

  4. LightOJ 1395 A Dangerous Maze (II) (概率dp)

    题意:给出n扇门,每扇门都给出一个数x,若为正数,则表示在x时间后走出迷宫,若为负数,则表示在x时间后回到起点,你会记得最后k扇你走过的门(不会再走),求最后的期望时间. 题解:概率dp 这题的进阶版 ...

  5. [A Dangerous Maze LightOJ - 1027 ][概率题]

    A Dangerous Maze LightOJ - 1027 题目大意:就是你有nnn个门每次你都会随机选一个门,这个门对应得数值如果是负的那么你将会在aia_iai​的时间后回到原来位置,如果是正 ...

  6. A - A Dangerous Maze

    Lightoj 1027 A - A Dangerous Maze //题目大意:一个迷宫有n个门,每个对应一个值,正值表示经过这么多秒后直接出迷宫,负值代表这么多秒后回到最开始的地方,问最后出去的期 ...

  7. 【LightOJ - 1027】A Dangerous Maze(概率dp,数学期望)

    题干: You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like ...

  8. LIGHTOJ 1027(概率 - 期望)

    /*题意:一个迷宫有n扇门,每次你可以任意选一扇门,每一扇门都有一个值xi如果xi > 0 ,表示可以走出迷宫,走出迷宫需要的时间为xi; 否则 回到原来的位置,用了xi的时间:问你走出迷宫所需 ...

  9. LightOJ - 1027 数学期望

    题意:有n扇门,每扇门有一个值x,大于0代表x分钟后出去,小于0代表x分钟后回到原地,求出去的时间的期望 题解:假设出去的总时间为sum1,回来的总时间为sum2,出去的门个数为out,进来的门的个数 ...

最新文章

  1. 平均 14926 元!2021 年 5 月程序员工资统计出炉
  2. 【完结】12篇文章告诉你深度学习理论应该学到什么水平
  3. 开发管理 (3) -项目启动会议
  4. mysql修改忘记了root密码忘记了,mysql忘记root密码后,重新设置、修改root密码
  5. 安卓蓝牙键盘按键映射_多设备无缝切换 雷柏XK100无线蓝牙轻薄键盘评测
  6. CRM和C4C product category hierarchy的可编辑性控制逻辑
  7. [Leetcode][第题][JAVA][两个数组的交集 II1][双指针][HashMap]
  8. 《Python Cookbook 3rd》笔记(5.12):测试文件是否存在
  9. 计算机防火墙不能更改,win7系统更新防火墙设置不能更改的解决方法
  10. html 把文字显示控制,控制字体加粗显示的html标签是哪个
  11. Android 系统(77)---MVC,MVP,MVVM的区别
  12. linux 基础 —— 网络管理
  13. IE10-浏览器实现placeholder效果
  14. DevExpress控件的GridControl控件小结
  15. 一般的病毒通过注册表自启动的方式不断完善中。。。。
  16. 打造人脉关系网,成就事业
  17. 程序猿 网站 | 常用 技术学习网站
  18. java经典算法(二)---zws
  19. BUAA(2021春) 北京地铁乘坐线路查询——Dijkstra和Floyd双解法
  20. Maven:命令大全。

热门文章

  1. 锁存器的工作原理_数字电路学习笔记(十):更多锁存器和触发器
  2. 云服务器有i5的性能吗,i5云服务器
  3. 语言教案 小小计算机,小班《小小手机本领大》语言教案
  4. Spring Cloud Alibaa
  5. unity检测范围内敌人_Unity实现视野范围外死亡敌人的分数显示在屏幕内
  6. 运维必知的23个经验教训,值得收藏!
  7. 提高国内访问GitHub速度的9种方案~
  8. C#开发模式——单例模式
  9. Android按键响应的几种方式、安卓页面的跳转、页面跳转传参、页面自动跳转、Activity(页面)的生命周期
  10. ux设计_为企业UX设计更好的数据表