HDOJ 4336——確率DP
Card Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1948 Accepted Submission(s): 909 Special Judge
Problem Description
In 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.
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
Sample Output
Source
2012 Multi-University Training Contest 4
Recommend
zhoujiaqi2010 | We have carefully selected several similar problems for you: 4337 4331 4332 4333 4334
問題はあなたにn枚のカードが現れる確率をあげて、あなたにそれらをすべて取る回数の期待を得ることを聞きます.
考え方:nは20しかないので、私たちは状態圧縮で状態を表して、毎回3種類の結果を抽出します:抽出したことがないカードを抽出します
,
ドロー中のカード
あ、カードを引いていません.後の2つの状況は状態に変化がなく,彼らの確率はsumであることを覚えた.
dp[S]は、S状態で全てのカードをパスする回数の所望を表す.
dp[S] = dp[S] * sum + dp[S ^ (1 << x1)] * p(x1) + dp[S ^ (1 << x2)] * p(x2) ...
逆に押すと、dp[0]が結果です.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1948 Accepted Submission(s): 909 Special Judge
Problem Description
In 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.
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
Recommend
zhoujiaqi2010 | We have carefully selected several similar problems for you: 4337 4331 4332 4333 4334
問題はあなたにn枚のカードが現れる確率をあげて、あなたにそれらをすべて取る回数の期待を得ることを聞きます.
考え方:nは20しかないので、私たちは状態圧縮で状態を表して、毎回3種類の結果を抽出します:抽出したことがないカードを抽出します
,
ドロー中のカード
あ、カードを引いていません.後の2つの状況は状態に変化がなく,彼らの確率はsumであることを覚えた.
dp[S]は、S状態で全てのカードをパスする回数の所望を表す.
dp[S] = dp[S] * sum + dp[S ^ (1 << x1)] * p(x1) + dp[S ^ (1 << x2)] * p(x2) ...
逆に押すと、dp[0]が結果です.
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 40000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-10
const long long MOD = 1000000000 + 7;
const int mod = 10007;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
typedef vector<int> vec;
typedef vector<vec> mat;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
double a[25] , dp[1 << 25] , sum , none , same;
int n;
int main()
{
//ios::sync_with_stdio(false);
#ifdef Online_Judge
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // Online_Judge
while(~scanf("%d" , &n))
{
none = 0;
for(int i = 0 ; i < n ; i ++)
{
scanf("%lf" , &a[i]);
none += a[i];
}
none = 1 - none;///
dp[(1 << n) - 1] = 0;///
for(int i = (1 << n) - 2 ; i >= 0 ; i--)
{
sum = 1;
same = 0;
for(int j = 0 ; j < n ; j++)
{
if(i & (1 << j))same += a[j];
else sum += dp[i | (1 << j)] * a[j];
}
dp[i] = sum / (1 - same - none);
// cout << dp[i] << endl;
// cout << '*' << sum << ' ' << same << endl;
}
printf("%.4lf
" , dp[0]);
}
return 0;
}