UVA - 557 Burger(汉堡)(dp+概率)

时间:2022-09-19 06:17:17

题意:有n个牛肉堡和n个鸡肉堡给2n个孩子吃。每个孩子在吃之前都要抛硬币,正面吃牛肉堡,反面吃鸡肉堡。如果剩下的所有汉堡都一样,则不用抛硬币。求最后两个孩子吃到相同汉堡的概率。

分析:

1、先求最后两个孩子吃到不同汉堡的概率。

2、dp[i]表示2i个人的情况。

3、dp[i + 1] = (2 * i - 1) * dp[i]  / (2 * i) 。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b) {
    if(fabs(a - b) < eps)  return 0;
    return a < b ? -1 : 1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 50000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
double dp[MAXN];
void init(){
    dp[1] = 1;
    for(int i = 1; i < MAXN; ++i){
        dp[i + 1] = dp[i] * (2 * i - 1) / (2 * i);
    }
}
int main(){
    init();
    int T;
    scanf("%d", &T);
    while(T--){
        int n;
        scanf("%d", &n);
        printf("%.4lf\n", 1 - dp[n / 2]);
    }
    return 0;
}