• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
UVa 557 Burger (概率+递推)

题意:有 n 个牛肉堡和 n 个鸡肉堡给 2n 个客人吃,在吃之前抛硬币来决定吃什么,如果剩下的汉堡一样,就不用投了,求最后两个人吃到相同的概率。

析:由于正面考虑还要要不要投硬币,太麻烦,所以我们先求最后两人吃到不同的概率即可,再用 1 减去就OK。

假设最后两个人吃的不一样,那么前 n-2 个人吃的肯定是 n/2 -1个牛肉堡和n/2-1 个鸡肉堡,根据排列组合可知,概率应该是C(n-2, n/2-1) * (0.5)^(n-2)。

这就是公式,然而这个并不好算,很可能超时,所以我们再把第 n-2 写出来,对比一下,然后就得到一个递推公式:

dp[i] = dp[i-1] * (2*i-3.0)*(2*i-2.0)/(i-1.0)/(i-1.0) * 0.25;(注意这里的 i 等于原来的2*i),然后就可以直接算了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 50000 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
double dp[maxn];

void init(){
    dp[1] = 1.0;
    for(int i = 2; i <= 50000; ++i)
        dp[i] = dp[i-1] * (2*i-3.0)*(2*i-2.0)/(i-1.0)/(i-1.0) * 0.25;
}

int main(){
    init();
    int T;  cin >> T;
    while(T--){
        cin >> n;
        printf("%.4f\n", 1.0-dp[n/2]);
    }
    return 0;
}

 

posted on 2016-09-07 09:13  dwtfukgv  阅读(438)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3