UVa 557 Burger (概率+递推)
2024-08-30 08:39:38
题意:有 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;
}
最新文章
- LeetCode---Hash Table
- git之remote repository create(远程仓库创建)
- (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
- hdu 2571 命运(递推,请小心)
- Android开发编码规范(自用)
- MVC上传相关
- 支持多QQ登录的软件
- 如何写一个数据库How do you build a database?(转载)
- [译]SSIS 通过环境变量配置数据源连接参数
- css3-逐帧动画
- 201521123106 《Java程序设计》第9周学习总结
- Django学习-19-缓存
- EF6CodeFirst+MVC5+Autofac泛型注册 入门实例
- Android特效专辑(十一)——仿水波纹流量球进度条控制器,实现高端大气的主流特效
- 前端笔记之ES678&;Webpack&;Babel(上)初识ES678&;Babel&;let和const&;解构&;语法
- Tag Helpers 的使用介绍
- redis_key键
- R-CNN阅读笔记
- javascript实现对html便签等字符的转义
- "; 初窥 "; nginx