hdu4336--expectation + State compression Dp--card Collector

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=4336

Transfer from http://www.cnblogs.com/zhj5chengfeng/archive/2013/03/02/2939601.html

Approach Analysis


Since there are only 20 cards, using state compression, 0 means the card is not collected, and 1 means the card is collected

Order: F[s] Represents the status of the card type that has been set, the expectation that all cards need to buy things

Buy a thing, the bag may:

1. No card

2. The card is already collected

3. The card is not collected

So there are:

F[s] = 1 + ((1-segma{p[i}) F[s]) + (segma{P[j]*f[s]}) + (segma{p[k]*f[s| ( 1<<k)]})

Including: i=0,2,..., n-1

J= J cards have been collected, i.e. s from right to left number J is 1:s& (1<<J)!=0

K= k cards are not collected, i.e. s from right to left number K bit is 0:s& (1<<k) ==0

The move item can be:

segma{P[i]}f[s] = 1 + segma{p[i]*f[s| ( 1<<i)},i= I card not collected

Target status is: F[0]

/************************************************* Author:P owatr* Created time:2015-8-25 18:21:57* File Name : Hdu4336.cpp ************************************************/#include <cstdio> #include <algorithm># Include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int MAXN = 1e5 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;double dp[1<<21];    Double A[21];int Main () {int n;        while (~SCANF ("%d", &n)) {for (int i = 0; i < n; i++) scanf ("%lf", &a[i]);        memset (DP, 0, sizeof (DP)); int MAXN = (1 << N)-1;       DP[MAXN] = 0;            for (int i = maxn-1; I >= 0; i--) {double sum = 0;            Dp[i] = 1;                for (int j = 0; J < N; j + +) {if (I & (1 << j)) continue; Dp[i] + = dp[i| (                1&LT;&LT;J)]*a[j];            Sum + = A[j];        } Dp[i]/= sum;    } printf ("%f\n", dp[0]);             } return 0;}

  

hdu4336--expectation + State compression Dp--card Collector

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.