ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【LOJ 2017】:数学期望

2021-06-11 11:34:16  阅读:147  来源: 互联网

标签:期望 LOJ t2 ++ t1 int const 2017 x1


传送门

题目描述

给你 n n n扇门,每一扇门如果是正数,则可以通过 t t t时间出去,否则经过 − t -t −t时间回到原地,问你走出去的数学期望是多少

分析

学习一下数学期望
假设数学期望为 E E E,走出去的时间为 t 1 t1 t1,回到原地的时间为 t 2 t2 t2,走出去的概率为 p 1 p1 p1,回到原地的概率为 p 2 p2 p2,那么
E = p 1 ∗ t 1 + p 2 ∗ ( t 2 + E ) E = p1 * t1 + p2 * (t2 + E) E=p1∗t1+p2∗(t2+E)

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
    char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
    while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}

int main() {
    int T;
    read(T);
    int res = 0;
    while(T--){
        printf("Case %d: ",++res);
        int sum1 = 0,sum2 = 0,t1 = 0,t2 = 0;
        int n;
        read(n);
        for(int i = 1;i <= n;i++){
            int x;
            read(x);
            if(x > 0) sum1++,t1 += x;
            else if(x < 0) sum2++,t2 -= x;
        }
        int x = t1 + t2;
        int x1 = sum1;
        int p = gcd(x,x1);
        if(!x1) printf("inf\n");
        else printf("%d/%d\n",x / p,x1 / p);
    }

}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

标签:期望,LOJ,t2,++,t1,int,const,2017,x1
来源: https://blog.csdn.net/tlyzxc/article/details/117809983

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有