Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4336
Main topic: There are n kinds of cards, need to eat snacks collection, open snacks, the probability of the first card is p[i], also may not appear card. ask you to collect n kinds of cards, what is the desired number of snacks to eat?
State compression: F[mask], represents the collection of masks, but also the number of expected snacks to eat.
Open Packaging, there are 3 cases, the first: no card, probability (1-sigma (p[i))
Second, in known species: Probability sigma (P[j])
The third, in the unknown species: p[k]
So f[mask] = f[mask]* (1-sigma (P[i])) + f[mask] * Sigma (P[j]) + sigma (F[mask|k]*p[k]) + 1
1 ///#pragma COMMENT (linker, "/stack:102400000,102400000")2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <vector>6#include <map>7#include <Set>8#include <bitset>9#include <cmath>Ten#include <numeric> One#include <iterator> A#include <iostream> -#include <cstdlib> -#include <functional> the#include <queue> -#include <stack> -#include <string> -#include <cctype> + using namespacestd; - #definePB push_back + #defineMP Make_pair A #defineSZ size () at #defineST begin () - #defineED End () - #defineCLR Clear () - #defineZERO (x) memset ((x), 0,sizeof (x)) -typedefLong LongLL; -typedef unsignedLong LongULL; intypedef pair<int,int>PII; - Const DoubleEPS = 1e-8; to + Const intMax_n = -; - intN; the Doublep[max_n+1],f[1<<Max_n]; * $ Panax Notoginseng intMain () { - the while(~SCANF ("%d",&N)) { + ZERO (f); A Doublesum =0.0; the for(intI=1; i<=n;i++){ +scanf"%LF",&p[i]); -Sum + =P[i]; $ } $f[(1<<n)-1] =0; - for(intMask= (1<<n)-2; mask>=0; mask-- ){ - DoubleP1 =0; the for(intI=0; i<n;i++){ - if((mask>>i) &1 ) {WuyiP1 + = p[i+1]; the } - } Wu //printf ("P1 =%f\n", p1); - DoubleP2 =0; About for(intI=0; i<n;i++){ $ if( ! ((mask>>i) &1) ) { -P2 + = f[mask| (1<<i)]*p[i+1]; - //printf ("f[mask| ( 1<<i)]=%f\n ", f[mask| ( 1<<i)]); - } A } + //printf ("P2 =%f\n", p2); theF[mask] = (p2+1.0)/(sum-p1); - } $printf"%.10f\n", f[0]); the } the the return 0; the}
[HDU 4336] Card Collector (state compression probability DP)