HDU 4336 Card Collector(容斥原理 or 状压求期望dp)
2015-02-09 14:18
363 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336
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 the famous novel Water Margin, you will win an amazing award.
As a smart boy, you notice that to win the award, you must buy much more snacks than it seems to be. To convince your friends not to waste money any more, you should find the expected number of snacks one should buy to collect a full suit of cards.
Input
The first line of each test case contains one integer N (1 <= N <= 20), indicating the number of different cards you need the collect. The second line contains N numbers p1, p2, ..., pN, (p1 + p2 + ... + pN <= 1), indicating the possibility of each card to
appear in a bag of snacks.
Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.
Output
Output one number for each test case, indicating the expected number of bags to buy to collect all the N different cards.
You will get accepted if the difference between your answer and the standard answer is no more that 10^-4.
Sample Input
Sample Output
题意:
买东西集齐全套卡片赢大奖。
每个包装袋里面最多一张卡片,最少可以没有。
且给出每种卡片出现的概率 p[i],以及所有的卡片种类的数量 n (1<=n<=20),
求集齐卡片需要买东西的数量的期望值。
注意:包装袋中或许没有卡片;
容斥原理
PS:奇数项和加,偶数项和减!
代码如下:
dp:
此题中,将n设置为 dp[0];
可以这样想,你要买sum包,才能集齐n种卡片,那么 你最后买的一包一定中奖,即一定是n种中的一种,
用状态压缩表示,dp[1111111]就表示,你现在可以要n包中的一包,也就是可以变成0111111,1011111,1101111.。。。1111110中的一种状态
dp[1111111]=上面列的所有的状态 乘以 中0那包的概率,即dp+=dp[i|(1<<j)]*p[j];
而dp[1111111]表示刚开始,你可以中任一种,它的期望值是0,因为你现在任一种都没有,
dp[0000000]即 dp[0] 则表示现在每一包都有,你已经不用买了,从直观上就可以理解为每位都是0,你没有选择了,
那么,给初值dp[(1<<n)-1]=0,
从这开始,对每一种状态,列举它的每一位,如果是0,则可以变成该位是1的状态,
讲解链接:http://www.bcwhy.com/thread-20576-1-1.html
代码如下:
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 the famous novel Water Margin, you will win an amazing award.
As a smart boy, you notice that to win the award, you must buy much more snacks than it seems to be. To convince your friends not to waste money any more, you should find the expected number of snacks one should buy to collect a full suit of cards.
Input
The first line of each test case contains one integer N (1 <= N <= 20), indicating the number of different cards you need the collect. The second line contains N numbers p1, p2, ..., pN, (p1 + p2 + ... + pN <= 1), indicating the possibility of each card to
appear in a bag of snacks.
Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.
Output
Output one number for each test case, indicating the expected number of bags to buy to collect all the N different cards.
You will get accepted if the difference between your answer and the standard answer is no more that 10^-4.
Sample Input
1 0.1 2 0.1 0.4
Sample Output
10.000 10.500
题意:
买东西集齐全套卡片赢大奖。
每个包装袋里面最多一张卡片,最少可以没有。
且给出每种卡片出现的概率 p[i],以及所有的卡片种类的数量 n (1<=n<=20),
求集齐卡片需要买东西的数量的期望值。
注意:包装袋中或许没有卡片;
容斥原理
PS:奇数项和加,偶数项和减!
代码如下:
#include <cstdio> #include <cstring> int main() { int n; double a[47]; while(~scanf("%d",&n)) { for(int i = 0; i < n; i++) { scanf("%lf",&a[i]); } double sum, ans = 0; for(int i = 1; i < (1<<n); i++) { sum = 0; int cnt = 0; for(int j = 0; j < n; j++) { if(i & (1<<j)) { sum+=a[j]; cnt++; } } if(cnt&1) { ans+=1.0/sum; } else { ans-=1.0/sum; } } printf("%lf\n",ans); } return 0; }
dp:
此题中,将n设置为 dp[0];
可以这样想,你要买sum包,才能集齐n种卡片,那么 你最后买的一包一定中奖,即一定是n种中的一种,
用状态压缩表示,dp[1111111]就表示,你现在可以要n包中的一包,也就是可以变成0111111,1011111,1101111.。。。1111110中的一种状态
dp[1111111]=上面列的所有的状态 乘以 中0那包的概率,即dp+=dp[i|(1<<j)]*p[j];
而dp[1111111]表示刚开始,你可以中任一种,它的期望值是0,因为你现在任一种都没有,
dp[0000000]即 dp[0] 则表示现在每一包都有,你已经不用买了,从直观上就可以理解为每位都是0,你没有选择了,
那么,给初值dp[(1<<n)-1]=0,
从这开始,对每一种状态,列举它的每一位,如果是0,则可以变成该位是1的状态,
讲解链接:http://www.bcwhy.com/thread-20576-1-1.html
代码如下:
#include <cstdio> #include <cstring> double p[47], dp[1<<20+1]; int main() { int n; while(~scanf("%d",&n)) { for(int i = 0; i < n; i++) { scanf("%lf",&p[i]); } dp[(1<<n)-1] = 0; for(int i = (1<<n)-2; i >= 0; i--)//枚举所有状态 { dp[i] = 0; double sum = 0; for(int j = 0; j < n; j++)//对每一位枚举 { if(!(i & (1<<j)))//该位是零 { dp[i] += dp[i | (1<<j)]*p[j]; sum+=p[j]; } } dp[i] += 1; dp[i]/=sum;//可以到达i这种状态的状态都找到了 在循环里累加的是期望值 要除概率和 } printf("%lf\n",dp[0]); } return 0; }
相关文章推荐
- HDU 4336 Card Collector(状压期望DP || 容斥原理)
- HDU-4336 Card Collector(状压概率DP||容斥原理)
- Card Collector - HDU 4336 状压期望dp
- hdu 4336 Card Collector (期望dp|容斥原理)
- HDU 4336 Card Collector 概率dp 状态压缩| 容斥原理
- 期望dp-hdu-4336-Card Collector
- hdu 4336 Card Collector(期望+状压DP)
- HDU 4336 Card Collector 状压+概率dp
- [HDU 4336] Card Collector (期望DP)
- ★HDU 4336 Card Collector 详细题解(状压求期望)
- hdu 4336 Card Collector (概率与期望+状压DP)
- HDU-4336 Card Collector 容斥定理 Or 概率DP
- hdu 4336 Card Collector (容斥 or dp)
- hdu 4336 Card Collector (概率dp+位运算 求期望)
- HDU 4336 Card Collector 数学期望(容斥原理)
- HDU 4336-Card Collector(状压,概率dp)
- hdu 4336 Card Collector(概率dp, 容斥原理)
- hdu 4336(状压+期望dp)
- HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
- [HDU] 4336 Card Collector 期望dp