概率题 fzu Problem 2103

http://acm.fzu.edu.cn/problem.php?pid=2103

Problem 2103 Bin & Jing in wonderland

Accept: 96    Submit: 546
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

42 3 30.3 0.71 1 12 3 30.3 0.71 1 22 3 30.3 0.71 2 22 3 30.3 0.72 2 2

 Sample Output

0.0270000.1890000.4410000.343000

 Source

“高教社杯”第三届福建省大学生程序设计竞赛
陷阱是----------k>r的时候 k-r  也是要算概率的  这个落下WA的让你流泪
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>

using namespace std;
#define MAXN 200
#define ll long long

/*ll C(ll n, ll k)
{
    ll m=1,t=1;
    for(ll i=2;i<=n-k;i++)
        m*=i;
    for(ll i=k+1;i<=n;i++)
        t*=i;
    return t/m;
}*/
long long C(ll n,ll k)
{
	ll i;
	long long ans=1;
	for ( i = 1; i <=k ; i++)
	{
		ans*=  (n-i+1);
		ans/=i;
	}
	return ans;
}

double Pow(double a,ll n)
{
    double ans = 1;
    for(ll i=0;i<n;i++)
    {
        ans*=a;
    }
    return ans;
}

int main()
{
    //freopen("B.txt","r",stdin);
    ll ncase,n,k,r,tmp,tt,mmin,ttt,rr;
    double p[MAXN],ans,tmpans,restp,restnum;
    ll li[MAXN],num[MAXN],cnt[MAXN];

    cin>>ncase;

    while(ncase--)
    {
        memset(num,0,sizeof(num));
        ans=1;
        tmp=1;
        tmpans=restp=restnum=rr=0;
        mmin=MAXN;

        cin>>n>>k>>r;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lf",&p[i]);
        }
        for(ll i=1;i<=r;i++)
        {
            cin>>li[i];
            num[li[i]]++;
            mmin=min(mmin,li[i]);
        }
        tt=k;
        for(ll i=1;i<=n;i++)
        {
            if(num[i]&&i!=mmin)
            {
                tmp=C(tt,num[i]);
                ans*=tmp*Pow(p[i],num[i]);
                tt-=num[i];
            }
        }
        restnum=tt;
        //tt=num[mmin];
        for(ll i=1;i<mmin;i++)
        {
            restp+=p[i];
        }
        for(ll i=num[mmin];i<=restnum;i++)
        {

            tmpans+=C(restnum-i,restnum-i)*Pow(restp,restnum-i)*C(restnum,i)*Pow(p[mmin],i);
        }
        ans*=tmpans;
        printf("%.6lf\n",ans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值