LightOJ 1027 Dangerous Maze

经典概率,主要找递推式。

给你n个门,每次选一个,如果为正x就x秒后结束,否则-x秒后还要留在这里,求期望。

ANS=P_POS*POS_AVERAGE+P_NEG*(NEG_AVERAGE+ANS);

借出Y即可。

#include <iostream>
#include <functional>
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

int main()
{
    long long T,ncas=1;
    scanf ("%d",&T);
    while (T--)
    {
        long long n,a[110],sum=0,p1=0,sum_p1=0,p2=0,sum_p2=0;
        scanf ("%lld",&n);
        for (long long i=0;i<n;i++)
        {
            scanf ("%lld",&a[i]);
            if (a[i]>0) {p1++;sum_p1+=a[i];}
            else {p2++;sum_p2+=(-a[i]);}
        }
        long long u,v;
        u=sum_p1+sum_p2;
        v=n-p2;
        if (p1!=0)
        {
            long long mmm=__gcd(u,v);
            printf ("Case %lld: %lld/%lld\n",ncas++,u/mmm,v/mmm);
        }
        else printf ("Case %lld: inf\n",ncas++);
    }
    return 0;
}
时间: 2024-10-01 05:33:09

LightOJ 1027 Dangerous Maze的相关文章

[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

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

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

LightOj 1027 A Dangerous Maze【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1027 题意: 你面前有n个门,每个对应一个数字,若为正xi,代表xi分钟后你会从它走出迷宫,负数则说明你会在-xi分钟后回到出发点且失去记忆.求出去的时间的期望. 代码: #include <iostream> #include <stdio.h> #include <math.h> #include <string> #include

LightOJ - 1027 A Dangerous Maze 概率

题目大意:迷宫里面有n扇门,每扇门有相应的值,假设第i扇门的值为xi,如果xi > 0,那么xi分钟后,走该扇门就可以走出迷宫了,如果xi < 0,表示走了该扇门之后,需要abs(xi)分钟后才能回到原点,问走出迷宫的期望是多少 解题思路:假设有k扇门(正值用x表示,负值用y表示)期望是d的话 那么d = 1 / k * (x1 + x2 + x3 + .. xn) + 1 / k * (abs(y1) + abs((y2) + - + abs(ym) + m * d) 表示有1/k的概率选到

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中呢? 也是不行的, 因为都是素数呢, 不是一样会溢出? 自己在半个小时后才

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