Card Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4693 Accepted Submission(s): 2380
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 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
Source
2012 Multi-University Training Contest 4
大致题意,有一些买一些零食的时候,有时会配一些卡片,如果说集齐了所有的卡片,会得到大奖。现在告诉你,总共的卡片数目,以及每种卡片出现在零食中的概率,问期望买多少袋的时候可以集齐所以的卡片。然后,吐槽一下那个水浒人物的卡片,据说108好汉,实际上出现在零食中的只有107个,所以说无论如何也不可能有人得到大奖,但是促进了销量,然后还被当作了典范……
由于总共只有最多20种卡片,所以我们可以用状态压缩表示状态,每一位的二进制数字表示对应种的卡片是否已经拿到。对于一个状态i,我再买一个零食,可能得到一个某一个新的卡片,或者得到一个已经有的卡片,于是我们可以利用这个来却定转移的方向。还是和往常的套路一样,我们一开始先预处理出所有的可能状态,当前若已经有了i张卡片,那么就可以从所有的合法的含有i+1张卡片处转移而来。有转移方程dp[i]=(1-Σp[k])*dp[i]+Σp[k]*dp[i|(1<<k)],移项可以得到dp[i]=(Σp[k]*dp[i|(1<<k)]+1-Σp[k])/Σp[k]。按照方程转移即可。具体见代码: