Card Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5254 Accepted Submission(s): 2676
Special JudgeProblem DescriptionIn 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.
InputThe 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.
OutputOutput 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 Input1 0.1 2 0.1 0.4
Sample Output10.000 10.500
Source
Recommendzhoujiaqi2010 | We have carefully selected several similar problems for you: 4337 4331 4332 4333 4334
First of all, the state pressure DP is obvious, that is, $f_S=\frac{1+\sum_{i\subseteq S}p_i f_{S\cup i}}{1-\sum_{i\subseteq S}p_i}$
Time $O(2^n*n)$, space $O(2^n)$.
Introduce the most value inversion: $\max\{S\}=\sum_{T\subseteq S}(-1)^{|T|+1}min\{T\}$.
$\max\{S\}$ in this formula can represent the largest number in the set, or it can represent the last number in the set (because the number in the order of appearance becomes the maximum number).
Min-max tolerance is actually the most value inversion. Considering such a type of problem, each element has a probability of occurrence, and find the expectation of the number of times the last element of a certain set appears.
$E[\max\{S\}]=\sum_{T\subseteq S}(-1)^{|T|+1}E[min\{T\}]$
Considering how to find $E[min\{T\}]$, it is actually to find the inverse of the probability that any element appears in the set, that is, $\frac{1}{\sum_{i\in T}p_i}$
This way only a subset of the full set needs to be enumerated. The complexity is optimized a lot.
Time $O(2^n)$, space $O(n)$.
1 #include<cstdio> 2 #include<algorithm> 3 #define rep(i,l,r) for (int i=l; i<=r; i++) 4 typedef double db; 5 using namespace std; 6 7 const int N=21; 8 const db eps=1e-8; 9 db a[N],ans; 10 int n; 11 12 void dfs(int x,db sum,int k){ 13 if (x>n) { if (sum>=eps) ans+=(db)k/sum; return; } 14 dfs(x+1,sum,k); dfs(x+1,sum+a[x],-k); 15 } 16 17 int main(){ 18 while (~scanf("%d",&n)){ 19 ans=0; rep(i,1,n) scanf("%lf",&a[i]); 20 dfs(1,0,-1); printf("%.10lf\n",ans); 21 } 22 return 0; 23 }