kuangbin专题二十一:概率&期望

时间:2021-08-07
本文章向大家介绍kuangbin专题二十一:概率&期望,主要包括kuangbin专题二十一:概率&期望使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

LightOJ1027 A Dangerous Maze

思路:简单概率。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 105;

int gcd(int x, int y) {
    return x == 0 ? y : gcd(y % x, x);
}

int main(){
    int T;
    cin >> T;
    for(int t = 1; t <= T; t++) {
        int n;
        cin >> n;
        int tot = 0, neg = 0;
        for(int i = 0; i < n; i++) {
            int x;
            cin >> x;
            if(x < 0) x = -x, neg++;
            tot += x;
        }

        cout << "Case " << t << ": ";
        if(neg == n) cout << "inf" << endl;
        else {
            int y = gcd(tot, n - neg);
            cout << tot / y << "/" << (n - neg) / y << endl;
        }
    }
    return 0;
}
View Code

LightOJ1030 Discovering Gold

思路:不能重复计算,需要倒序。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 105;
int gold[maxn];
double e[maxn];


int main(){
    int T;
    cin >> T;
    for(int t = 1; t <= T; t++) {
        int n;
        cin >> n;

        memset(e, 0, sizeof(e));
        for(int i = 0; i < n; i++) cin >> gold[i];

        e[n-1] = gold[n-1];
        for(int i = n - 2; i >= 0; i--) {
            e[i] = gold[i];
            int len = min(6, n - 1 - i);
            for(int j = 1; j <= len; j++)
                e[i] += e[i + j] / len;
        }

        printf("Case %d: %.7f\n", t, e[0]);
    }
    return 0;
}
View Code

LightOJ1038 Race to 1 Again

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 5;
double ary[maxn];

int main(){
    int T;
    cin >> T;

    for(int i = 2; i < maxn; i++) {
        double tot = 0;
        int c = 0;
        for(int j = 1; j * j <= i; j++) {
            if(i % j == 0) {
                tot += ary[j];
                c++;
                if(j != i / j) {
                    tot += ary[i / j];
                    c++;
                }
            }
        }
        ary[i] = (tot + c) / (c - 1);
    }

    for(int t = 1; t <= T; t++) {
        int n;
        cin >> n;

        printf("Case %d: %.6lf\n", t, ary[n]);
    }
    return 0;
}
View Code

LightOJ1079 Just another Robbery

思路:概率dp。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e4 + 5;
float dp[maxn];

int gain[maxn];
float risk[maxn];

int main(){
    int T;
    cin >> T;

    for(int t = 1; t <= T; t++) {
        memset(dp, 0, sizeof(dp));
        dp[0] = 1;
        float P;
        int n;
        cin >> P >> n;

        int sum = 0;
        for(int i = 1; i <= n; i++){
            cin >> gain[i] >> risk[i];
            sum += gain[i];
        }

        for(int i = 1; i <= n; i++) {
            for(int j = sum; j >= gain[i]; j--){
                dp[j] = max(dp[j], dp[j - gain[i]] * (1 - risk[i]));
            }
        }

        for(int i = sum; i >= 0; i--)
            if(dp[i] >= (1 - P)) {
                printf("Case %d: %d\n", t, i);
                break;
            }
    }

    return 0;
}
View Code

LightOJ1104 Birthday Paradox

思路:算无相同生日概率。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 1e5 + 5;

int main(){
    int T;
    cin >> T;

    for(int t = 1; t <= T; t++) {
        int n;
        cin >> n;

        int cnt = 1;
        double p = 1;
        while(p > 0.5) {
            p *= (n - cnt) * 1.0 / n;
            cnt++;
        }

        printf("Case %d: %d\n", t, --cnt);
    }

    return 0;
}
View Code

LightOJ1151 Snakes and Ladders

原文地址:https://www.cnblogs.com/arch/p/15096622.html