HDU4336 Card Collector【容斥原理】

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4336

题目大意:

每包零食里有一张卡牌,总共有N种不同的卡牌,得到这N种卡牌的概率分别为P[i](1 <= i <= N)。

求收集到所有卡牌的期望是多少。

思路:

Pi表示得到第i张卡牌的概率,Ei表示得到第i张卡的期望。

假设现在有两张卡牌,由题意可知:

E1 = 1/P1,E2 = 1/P2,E12(表示肯定买到1或2其中一包的期望) = 1/(P1+P2)。

当我们计算E1和E2的时候,E12是重复计算了2次,应该减去一次。根据容斥定理可知:

E = E1 + E2 - E12。

同理,三张牌的时候:

E = E1 + E2 + E3 - E12 - E13 - E23 + E123。

以此类推,当计算期望中的各项的时候,如果该项为奇数项(奇数张卡的期望),则加上该项。

如果该项为偶数项2(偶数项卡的期望),则减去该项。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

double P[50];
int N;

double Solve()
{
    double xh = 0;
    for(int i = 1; i < (1 << N); ++i)   //遍历2^N种情况  从00…01 到 11…11
    {
        double sum = 0;
        int odd = 0;
        for(int j = 0; j < N; ++j)      //对于i(每种情况),计算i的二进制为1的位数
        {
            if((1<<j) & i)      //i从右往左数对应第j位上是否为1
            {
                odd++;          //二进制为1的位数
                sum += P[j+1];  //将为1项的概率加起来。
            }
        }
        if(odd & 1) //奇数项加,偶数项减
            xh += 1/sum;
        else
            xh -= 1/sum;
    }
    return xh;

}
int main()

{
    while(~scanf("%d",&N))
    {
        for(int i = 1; i <= N; ++i)
            scanf("%lf",&P[i]);
        printf("%.6lf\n",Solve());  //注意lf 和 f
    }

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 23:45:20

HDU4336 Card Collector【容斥原理】的相关文章

HDU-4336 Card Collector(状压概率DP||容斥原理)

Card Collector http://acm.hdu.edu.cn/showproblem.php?pid=4336 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful cards in

HDU4336 Card Collector 概率DP求期望+状压

题目大意:要集齐N张卡片,每包干脆面出现每种卡片的概率已知,问你集齐N张卡片所需要的方便面包数的数学期望(N<=20). solution: 由于N<=20,我们可以考虑状压,设dp[S]表示牌的状态为S时的需要的方便面包数的数学期望. 那么,对于每一个状态,考虑枚举每一张牌i(摸到了i),此时: ① 如果S中不含i,dp[S]+=(dp[S|(1<<i-1)]+1)*p[i]. ② 如果S中已经包含i,那么算到下面的情况中去. 但是注意到,上述情况是已经保证了摸到牌,但是其实可以

HDU 4336:Card Collector(容斥原理)

http://acm.split.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, for example, if you collect all the 108 people in

hdu 4336 Card Collector

Card Collector http://acm.hdu.edu.cn/showproblem.php?pid=4336 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful cards in

HDU 4336:Card Collector 期望+状压

Card Collector 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意: 去商店里买零食,每包零食里最多有1张卡片,也有可能没有,问要集齐所有n(n≤20)种卡片所需要购买零食个数的期望. 题解: 设dp[i](二进制,对应位为1表示已经有该卡片)为以当前状态为起点还需要购买零食个数的期望,则dp[0]即答案 对DP求解期望有问题的可以看下这里 代码 #include<stdio.h>#include<string.h

HDU 4336——Card Collector——————【概率dp】

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3407    Accepted Submission(s): 1665Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful

hdu 4336 Card Collector (概率dp+位运算 求期望)

题目链接 Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2711    Accepted Submission(s): 1277Special Judge Problem Description In your childhood, do you crazy for collecting the beaut

HDU 4336 Card Collector(概率dp+状压)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3014    Accepted Submission(s): 1445 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

Card Collector(HDU 4336)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3001    Accepted Submission(s): 1435Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful