【HDOJ】4336 Card Collector

概率DP的题目,一直就不会做这类题目。
dp[s]表示状态为s的时候再买多少张牌可以买全,表示的是一个期望值。
dp[s] = 1 + P(empty) * dp[s] + P(had) * dp[s] + P(new) * dp[nst]。
从而可以解dp[s]。

 /* 4336 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
double p[maxn];
double dp[<<maxn];
int n; void solve() {
int mst = << n;
int mask = mst - ;
dp[mask] = ; per(i, , mask) {
double tmp = 0.0; dp[i] = 1.0;
rep(j, , n) {
if (i & (<<j))
continue;
dp[i] += p[j] * dp[i|(<<j)];
tmp += p[j];
}
dp[i] /= tmp;
} double ans = dp[];
printf("%.06lf\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d", &n)!=EOF) {
rep(i, , n)
scanf("%lf", &p[i]);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
上一篇:正则基础之——环视(Lookaround)


下一篇:kafka性能测试