zoukankan      html  css  js  c++  java
  • 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 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 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个门,如果t是正数,需要t的时间出去,如果是负数,需要-t的时间返回,回去之后啥也忘了,每次尝试相互独立。
    num:出去的门的数量
    tot: 所有门的时间和
    出去的概率:(P_{out}=frac{num}{n})

    期望的尝试次数 (K=frac{1}{p_{out}})

    一次尝试的期望的花费:(Cost=frac{tot}{n})

    所以,(ans=k*Cost)
    特判inf

    #include<bits/stdc++.h>
    #include<stdio.h>
    #include<algorithm>
    #include<ctype.h>
    using namespace std;
    template<class T> inline bool read(T &x){
        x=0;register char c=getchar();register bool f=0;
        while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
        while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
        if(f)x=-x;
        return true;
    }
    template<class T>inline void print (T x){
        if(x<0)putchar('-'),x=-x;
        if(x>9)print (x/10);
        putchar('0'+x%10);
    }
    #define Init(a,v) memset(a,v,sizeof(a))
    typedef long long ll;
    const ll MAXN=1e1+8,inf=0x3f3f3f3f,mod=1e9+7;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    int main() {
        int t,cas=1;
        read(t);
        while(t--){
            ll n,num_out=0,tot_time=0,x;
            read(n);
            for(int i=0;i<n;++i){
                read(x);
                if(x>0){
                    num_out++;
                    tot_time+=x;
                }else tot_time-=x;
            }
            if(num_out==0)printf("Case %d: inf
    ",cas++);
            else{
                ll g=gcd(tot_time,num_out);
                printf("Case %d: %lld/%lld
    ",cas++,tot_time/g,num_out/g);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    源码安装mysql-5.7.13一周的冤枉路总结。满满的都是泪啊
    一键安装Apache服务脚本
    源码编译安装LAMP
    Vue侦听器 watch
    Vue计算属性 computed
    Vue表单的值绑定和修饰符
    js编码解码decodeURI(URIstring)与decodeURIComponent(URIstring)的区别
    常用的JS表单验证
    js正则表达式匹配手机号中间四位以及匹配姓名第一个字符,将其替换为*
    Vue按键修饰符,鼠标按钮修饰符
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14155970.html
Copyright © 2011-2022 走看看