HDU-4336-期望dp-bit
2018-05-11 17:25
330 查看
Card Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5272 Accepted Submission(s): 2688
Special Judge
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 容斥暂时搞不懂,f[S]表示当前收集状态为S距离目标的期望购买次数,S能推出的状态有n+1种,把推完之后状态还是S的移到方程左边,其他的在右边最后算一下答案就好了。(显然推完之后还是S的情况只可能是下一次买的卡是空的或者S中已经有了)。
#include<iostream> #include<cstring> #include<queue> #include<cstdio> #include<stack> #include<set> #include<map> #include<cmath> #include<ctime> #include<time.h> #include<algorithm> using namespace std; #define mp make_pair #define pb push_back #define debug puts("debug") #define LL long long #define pii pair<int,int> #define eps 1e-10 #define inf 0x3f3f3f3f double f[(1<<21)+30]; double p[25]; int main() { int t,i,j,k,n,m,u,v; while(scanf("%d",&n)==1){double none=0; for(i=0;i<n;++i) scanf("%lf",p+i),none+=p[i]; int all=(1<<n)-1; memset(f,0,sizeof(f)); for(i=all-1;i>=0;--i){ double Pj=(double)1.00-none,s=0; for(j=0;j<n;++j){ if(i&(1<<j)){ Pj+=p[j]; } else{ s+=p[j]*f[i|(1<<j)]; } } s+=1.00; Pj=1.00-Pj; f[i]=s/Pj; } printf("%.5f\n",f[0]); } return 0; }
容斥做法的话不是很懂说一下简单思路。
E(至少得到i号卡)=1/pi,把他记作E(i),这个的含义就是一直买卡直至第一次出现i卡时停止的期望购买次数,这个式子是可以推出来的,数学不好真tm烦= =E(至少得到A卡或者B卡的)=E(A|B).
我们要求的就是E(至少得到1&2&...&N号卡),不妨记作E(1&2&...&N)=E(1)+E(2)+...+E(n)-{E(1|2)+E(2|3)+......}+{E(1|2|3)+...}.....
就这样一直利用容斥定理奇加偶减算出最终的答案。
以后会了可能会补。
相关文章推荐
- HDU 4336 概率DP求期望(or容斥原理)
- hdu 4336 Card Collector(期望+状压DP)
- HDU 4336 Card Collector(容斥原理 or 状压求期望dp)
- hdu 4336 dp求期望(状态压缩)
- hdu 4336 Card Collector (期望dp|容斥原理)
- hdu 4336 Card Collector(期望 dp 状态压缩)
- HDU 4336 Card Collector(状压期望DP || 容斥原理)
- hdu 4336 Card Collector (概率与期望+状压DP)
- hdu 4336 全期望公式+状态压缩+概率dp
- HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
- hdu 4336(状压+期望dp)
- Card Collector - HDU 4336 状压期望dp
- [HDU] 4336 Card Collector 期望dp
- HDU 4336 集齐方便面卡片的期望-期望dp 或 容斥原理 -(二进制压缩辅助)
- 期望dp-hdu-4336-Card Collector
- 期望dp-hdu-4336-Card Collector
- hdu-4336 Card Collector状态压缩|期望dp】
- hdu 4336 Card Collector (概率dp+位运算 求期望)
- [HDU 4336] Card Collector (期望DP)
- HDU 4035 Maze 期望dp