LightOJ1027 A Dangerous Maze(期望)

题目大概说你正在起点,面前有$n$个门,每个门有一个数字$x$,正数表示开这个门$x$分钟后会出去,负数表示开这个门$-x$分钟后会回到起点。选择门的概率是一样的且每次选择互不影响。问出去的时间期望是多少。

$d[0]$表示从起点出发出去的期望,$d[i](1\leqslant i\leqslant n)$表示选择第i个门出去的期望;

不妨设$1\dots j$的门是正数,$j+1\dots n$的门是负数,那么:

$$d[i]=x[i](1\leqslant i\leqslant j)$$

$$d[i]=x[i]+d[0](j+1\leqslant i\leqslant n)$$

由于每个门被选择的概率均等,那么:

$$d[0]=\frac{\sum_{i=1}^{j}x[i]+\sum_{i=j+1}^{n}(x[i]+d[0])}{n}$$

$$d[0]=\frac{\sum_{i=1}^{n}x[i]}{n-j}$$

这样计算出$d[0]$就是要的结果了。队友教的。

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 int gcd(int a,int b){
 5     if(b==0) return a;
 6     return gcd(b,a%b);
 7 }
 8 int main(){
 9     int t,n,a;
10     scanf("%d",&t);
11     for(int cse=1; cse<=t; ++cse){
12         scanf("%d",&n);
13         int tot=0,cnt=0;
14         for(int i=1; i<=n; ++i){
15             scanf("%d",&a);
16             if(a<0) tot-=a,++cnt;
17             else tot+=a;
18         }
19         int x=tot,y=n-cnt;
20         if(y==0) printf("Case %d: inf\n",cse);
21         else printf("Case %d: %d/%d\n",cse,x/gcd(x,y),y/gcd(x,y));
22     }
23     return 0;
24 }
时间: 2024-12-19 12:35:09

LightOJ1027 A Dangerous Maze(期望)的相关文章

lightoj-1027 - A Dangerous Maze(数学期望)

1027 - A Dangerous Maze PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBYou 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 do

LightOJ1027---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

lightoj 1027 A Dangerous Maze 期望

设答案为r,cnt为x[i] >=0的个数 那么r = 1/n *  (Σx[i](x[i] >= 0) + ∑(r - x[i])(x[i] < 0)) 然后把r移项到一起解方程, 得到r = ∑|x[i]| / cnt,同除gcd.记得特判下x[i]均为负数的情况即可. 1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 int T,cas,n,tot,gcd,cnt; 5 i

light OJ 1027 A Dangerous Maze (期望)

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

[LOJ 1027] Dangerous Maze

A - A Dangerous Maze Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description 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

[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 positio

(期望)A Dangerous Maze(Light OJ 1027)

http://www.lightoj.com/volume_showproblem.php?problem=1027 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 ca

LightOJ 1027 A Dangerous Maze(期望)

https://cn.vjudge.net/problem/LightOJ-1027 题意:有n扇门,每扇门有个时间ti,选择正数的门可以在ti后带你走出迷宫,负数的门会在ti后带你回到起点,然后重新选择,每扇门被选中的概率是一样的,求期望. 思路: 做这种期望的问题必须得列一个方程来求解,不然无限写下去的话算不出. 设走出去的期望的E,对于第三个案例就是 E = 1/3 * 3  + 1/3( 6 + E) + 1/3 (9 + E),(选择第一扇门就直接出去,第二扇门先6分钟回去,然后再花期

LightOJ 1027 A Dangerous Maze 概率期望

题目链接: https://vjudge.net/problem/LightOJ-1027 题目描述: 有N个门, 每个门的选择是等概率的, 如果选择到正数, 我将在正数秒后逃出迷宫, 如果是负数就重新选, 问逃离的期望时间是多少 解题思路: 我这道题犯蠢了, 我认为是.....说不太明白, 看程序吧, 一下子就能看懂, 其中涉及到了一个约分, 但是我没想到就算是long long 也会溢出, 那么我将约分放进每次mul中呢? 也是不行的, 因为都是素数呢, 不是一样会溢出? 自己在半个小时后才