UVa 557 Burger (概率+递推)
2016-09-07 09:13
330 查看
题意:有 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),然后就可以直接算了。
代码如下:
析:由于正面考虑还要要不要投硬币,太麻烦,所以我们先求最后两人吃到不同的概率即可,再用 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; }
相关文章推荐
- ROWNUM
- BootStrap中的表单大全
- MyBatis学习总结[5]-动态 SQL
- POJ 2777 Count Color (线段树)
- Codeforces Round #228 (Div. 2) C Fox and Box Accumulation(贪心)
- Java线程:线程的调度-守护线程
- Codeforces Round #228 (Div. 2) B Fox and Cross(DFS)
- 初学C++之自定义类型名简化
- Spring 使用注解方式进行事务管理
- Codeforces Round #228 (Div. 2) A - Fox and Number Game(水题)
- Java 数组声明、创建、初始化详解
- 解方程(按位或)
- 2、从键盘上输入两个整数,由用户回答它们的和,差,积,商和取余运算结果,并统计出正确答案的个数。
- Android Studio 将引用第三方jar包的android工程打包成jar包
- JQuery语法总结和注意事项
- swap 函数的四种写法
- TQ210 —— NandFlash
- Android Studio 学习笔记一(一哥)
- js、jq、ajax之间的关系
- centos vim配置高亮语法和格式化粘贴