2016 ICPC Asia Qingdao Onsite D. Lucky Coins

Lucky Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1654    Accepted Submission(s): 601

Problem Description

Bob has collected a lot of coins in different kinds. He wants to know which kind of coins is lucky. He finds out a lucky kind of coins by the following way. He tosses all the coins simultaneously, and then removes the coins that come up tails. He then tosses all the remaining coins and removes the coins that come up tails. He repeats the previous step until there is one kind of coins remaining or there are no coins remaining. If there is one kind of coins remaining, then this kind of coins is lucky. Given the number of coins and the probability that the coins come up heads after tossing for each kind, your task is to calculate the probability for each kind of coins that will be lucky.

Input

The first line is the number of test cases. For each test case, the first line contains an integer k representing the number of kinds. Each of the following k lines describes a kind of coins, which contains an integer and a real number representing the number of coins and the probability that the coins come up heads after tossing. It is guaranteed that the number of kinds is no more than 10, the total number of coins is no more than 1000000, and the probabilities that the coins come up heads after tossing are between 0.4 and 0.6.

Output

For each test case, output a line containing k real numbers with the precision of 6 digits, which are the probabilities of each kind of coins that will be lucky.

Sample Input

3 1 1000000 0.5 2 1 0.4 1 0.6 3 2 0.4 2 0.5 2 0.6

Sample Output

1.000000 0.210526 0.473684 0.124867 0.234823 0.420066

Source

Recommend

jiangzijing2015

Solution

参考 https://blog.csdn.net/vici__/article/details/82935968

die[i][j]: 第i种硬币在第j步之前(包括第j步)全部被淘汰的概率

live[i][j]: 第i种硬币在第j步之前(包括第j步)幸存的概率

对于第i种硬币,枚举它胜出前经过的回合数。(当第j回合结束时除i以外的所有硬币都淘汰,当第j+1回合时i淘汰

ans[i]=\sum_{j=1}^{100}\sum_{k=1,k\neq i}^{n}(live[i][j]-live[i][j+1])\times die[k][j]

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
double die[10][100], live[10][100];

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(false);
    int t, n, m;
    double p, ans, prod;
    cin >> t;
    while (t--)
    {
        cin >> n;
        memset(die, 0, sizeof(die));
        memset(live, 0, sizeof(live));
        for (int i = 1; i <= n; i++)
        {
            cin >> m >> p;
            for (int j = 1; j < 100; j++)
            {
                die[i][j] = pow(1.0 - pow(p, j), m);
                live[i][j] = 1.0 - die[i][j];
            }
        }
        if (n == 1)
        {
            printf("%.6f\n", 1.0);
            continue;
        }
        for (int i = 1; i <= n; i++)
        {
            ans = 0.0;
            for (int j = 1; j < 100; j++)
            {
                prod = 1.0;
                for (int k = 1; k <= n; k++) if (k != i) prod *= die[k][j];
                ans += (live[i][j] - live[i][j + 1]) * prod;
            }
            printf("%.6f%s", ans, i == n ? "\n" : " ");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35850147/article/details/104194735