FZoj--2103--Bin & Jing in wonderland(概率)


                                     Problem 2103 Bin & Jing in wonderland

Accept: 203    Submit: 1050
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
题意:共由n种礼物,一个人每呼叫一次,天上都会掉下来一个礼物,礼物的id不确定,他总共叫了k次,最后取ID最大的前r个礼物,问得到这样的r个礼物的可能性有多大
#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\n", ans * ans1);
	}
	return 0;
}

你可能感兴趣的:(FZoj--2103--Bin & Jing in wonderland(概率))