HDU-5985-Lucky Coins

ACM模版

描述

题解

image.png

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int MAX_STAPES = 75;
const int MAXN = 15;

int n;
int num[MAXN];
double p[MAXN];
double die[MAXN][MAX_STAPES + 5];
double alive[MAXN][MAX_STAPES + 5];
double luck[MAXN];

void solve()
{
    for (int i = 0; i < MAXN; i++)
    {
        luck[i] = 0.0;
    }

    for (int i = 0; i < n; i++)
    {
        double tmp = p[i];
        for (int j = 1; j <= MAX_STAPES; j++)
        {
            die[i][j] = pow(1 - tmp, num[i]);
            alive[i][j] = 1 - die[i][j];
            tmp *= p[i];
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 1; j < MAX_STAPES; j++)
        {
            double tmp = 1.0;
            for (int k = 0; k < n; k++)
            {
                if (k != i)
                {
                    tmp *= die[k][j];
                }
            }
            luck[i] += (alive[i][j] - alive[i][j + 1]) * tmp;
        }
    }
}

int main()
{
    int T;
    scanf("%d", &T);

    while (T--)
    {
        scanf("%d", &n);

        for (int i = 0; i < n; i++)
        {
            scanf("%d%lf", &num[i], &p[i]);
        }

        if (n == 1)
        {
            puts("1.000000");
            continue;
        }

        solve();

        for (int i = 0; i < n; i++)
        {
            printf("%.6f%c", luck[i], (i == n - 1) ? '\n' : ' ');
        }
    }

    return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券