Bin & Jing in wonderland(概率,组合数学)

Problem 2103 Bin & Jing in wonderland

Accept: 201    Submit: 1048 Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

Bin has a dream that he and Jing are both in a wonderland full of beautiful gifts. Bin wants to choose some gifts for Jing to get in her good graces.

There are N different gifts in the wonderland, with ID from 1 to N, and all kinds of these gifts have infinite duplicates. Each time, Bin shouts loudly, “I love Jing”, and then the wonderland random drop a gift in front of Bin. The dropping probability for gift i (1≤i≤N) is P(i). Of cause, P(1)+P(2)+…+P(N)=1. Bin finds that the gifts with the higher ID are better. Bin shouts k times and selects r best gifts finally.

That is, firstly Bin gets k gifts, then sorts all these gifts according to their ID, and picks up the largest r gifts at last. Now, if given the final list of the r largest gifts, can you help Bin find out the probability of the list?

 Input

The first line of the input contains an integer T (T≤2,000), indicating number of test cases.

For each test cast, the first line contains 3 integers N, k and r (1≤N≤20, 1≤k≤52, 1≤r≤min(k,25)) as the description above. In the second line, there are N positive float numbers indicates the probability of each gift. There are at most 3 digits after the decimal point. The third line has r integers ranging from 1 to N indicates the finally list of the r best gifts’ ID.

 Output

For each case, output a float number with 6 digits after the decimal points, which indicates the probability of the final list.

 Sample Input

4 2 3 3 0.3 0.7 1 1 1 2 3 3 0.3 0.7 1 1 2 2 3 3 0.3 0.7 1 2 2 2 3 3 0.3 0.7 2 2 2

 Sample Output

0.027000 0.189000 0.441000 0.343000

 Source

“高教社杯”第三届福建省大学生程序设计竞赛
 
题解:有N种苹果,一个人在下面喊k次,每次掉下来一个苹果,每种苹果掉下来的概率已知,现在给出前r大的苹果种类;
问此时的概率;先找出最小的出现的种类mi,求出比mi大的概率,再求出比mi小的概率,枚举mi出现的次数,概率想乘就好了;
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
double p[25], dp[25];
typedef long long LL;
LL C[53][53];
int num[110];
int a[110];
void db(){
    C[1][0] = C[1][1] = 1;
    for(int i = 2; i < 53; i++){
        C[i][0] = C[i][i] = 1;
        for(int j = 1; j < i; j++){
            C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
        }
    }
}
int main(){
    int T, N, k, r;
    scanf("%d", &T);
    db();
    while(T--){
        scanf("%d%d%d", &N, &k, &r);
        dp[0] = 0;
        for(int i = 1; i <= N; i++){
            scanf("%lf", p + i);
            dp[i] = dp[i - 1] + p[i];
        }
        double ans = 1;
        memset(num, 0, sizeof(num));
        int mi = 0x3f3f3f3f;
        for(int i = 1; i <= r; i++){
            scanf("%d", a + i);
            num[a[i]]++;
            mi = min(mi, a[i]);
        }
        int now = k;
        for(int i = mi + 1; i <= N; i++){
            if(!num[i])continue;
            ans *= C[now][num[i]] * pow(p[i], num[i]);
            now -= num[i];
        }
        double ans1 = 0;
        for(int i = num[mi]; i <= k - r + num[mi]; i++){
            ans1 += C[k - r + num[mi]][i] * pow(p[mi], i) * pow(dp[mi - 1], k - r + num[mi] - i);
        }
        printf("%.6lf
", ans * ans1);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/handsomecui/p/5468150.html