[BC Round#26] Card 【各种水】
题目链接:HDOJ - 5159
这道题的做法太多了..BC的第二题也是可以非常水的..
算法一
我在比赛的时候写的算法是这样的..
预处理出所有的答案,然后对于每个询问直接输出。
询问 (a, b) 记作 (a, b) 。
(a, b) 的答案是由 (a, b-1) 的答案推出的。
(a, 1) 的答案是 1 到 a 的平均数,着十分显然。
如果 b > 1 ,那么我们就考虑在第 b 次,我们抽到每种牌的概率都是 1/a ,然后这张牌之前 b-1 次没被抽到的概率为 ((a-1)/a)^(b-1) ,那么第 b 次新获得的期望得分就是
Sum(1~a) * ((a-1)/a)^(b-1) * (1/a) 。然后这个值加上 (a, b-1) 的答案就是 (a, b) 的答案了。
【代码】
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; typedef double DB; const int MaxN = 100000 + 5, MaxM = 5 + 2; DB Ans[MaxN][MaxM]; int main()
{
int T, a, b;
scanf("%d", &T);
DB t;
for (int i = 1; i <= 100000; ++i) {
for (int j = 1; j <= 5; ++j) {
if (j == 1) {
Ans[i][j] = (DB)(1 + i) / 2.0;
t = (DB)(i - 1) / (DB)i;
continue;
}
Ans[i][j] = Ans[i][j - 1] + (DB)(1 + i) / 2.0 * t;
if (j == 5) continue;
t *= (DB)(i - 1) / (DB)i;
}
}
for (int Case = 1; Case <= T; ++Case) {
scanf("%d%d", &a, &b);
printf("Case #%d: %.3lf\n", Case, Ans[a][b]);
}
return 0;
}
算法二
这种算法是对于 (a, b) 直接求,写起来简单多了。
我们考虑每一张牌,它在 b 次之内如果被抽到了,得分就会加上它的值,那么它在 b 次之内有多大概率被抽到呢?
这个不好直接算,我们就考虑,b 次之内都没有被抽到的概率有多大呢?这个显然就是 ((a-1)/a)^(b) 。那么 b 次之内抽到它的概率就是 1 - ((a-1)/a)^b ,这个概率乘它的值就是这张牌对期望得分的贡献。
那么答案就是 Sum(1~a) * (1 - ((a-1)/a)^b) 。
【代码】
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; typedef double DB; const int MaxN = 100000 + 5, MaxM = 5 + 2; DB Ans[MaxN][MaxM]; DB Solve(int a, int b) {
DB t = 1;
for (int i = 1; i <= b; ++i) t *= (DB)(a - 1) / a;
t = 1 - t;
return (DB)(1 + a) * (DB)a / 2.0 * t;
} int main()
{
int T, a, b;
scanf("%d", &T);
for (int Case = 1; Case <= T; ++Case) {
scanf("%d%d", &a, &b);
printf("Case #%d: %.3lf\n", Case, Solve(a, b));
}
return 0;
}
[BC Round#26] Card 【各种水】的更多相关文章
- Educational Codeforces Round 26
Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...
- CodeForces 837F - Prefix Sums | Educational Codeforces Round 26
按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...
- CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26
/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...
- CodeForces 837D - Round Subset | Educational Codeforces Round 26
/* CodeForces 837D - Round Subset [ DP ] | Educational Codeforces Round 26 题意: 选k个数相乘让末尾0最多 分析: 第i个数 ...
- Codeforces Beta Round #2 A. Winner 水题
A. Winner 题目连接: http://www.codeforces.com/contest/2/problem/A Description The winner of the card gam ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- Codeforces Testing Round #12 A. Divisibility 水题
A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- HDU 5618 Jam's problem again CDQ分治 BC ROUND 70
题意:给你1e5个点(x,y,z),对于每一个点询问有多少个点(x1,y1,z1)满足x1<=x&&y1<=y&&z1<=z 分析:(官方题解奉上)很 ...
- Educational Codeforces Round 26 B,C
B. Flag of Berland 链接:http://codeforces.com/contest/837/problem/B 思路:题目要求判断三个字母是否是条纹型的,而且宽和高相同,那么先求出 ...
随机推荐
- 《Linux设备驱动开发详解(第2版)》配套视频登录51cto教育频道
http://edu.51cto.com/course/course_id-379-page-1.html http://edu.51cto.com/course/course_id-379-page ...
- jQuery Animation实现CSS3动画
jQuery Animation的工作原理是通过将元素的CSS样式从一个状态改变为另一个状态.CSS属性值是逐渐改变的,这样就可以创建动画效果.只有数字值可创建动画(比如 "margin:3 ...
- Eclipse启动 报错[Failed to load the JNI shared library jvm.dll
准备要做java服务器,在安装开发环境时,启动Eclipse报错[Failed to load the JNI shared library jvm.dll] 研究了下,造成错误的原因是由于eclip ...
- 利用systemtap学习Linux路由代码
http://bbs.chinaunix.net/thread-4090162-1-1.html 一.为什么要这样做读kernel route子系统代码,当我弄懂了数据结构之间的关系以及控制流程后,心 ...
- Spring AOP + AspectJ Annotation Example---reference
In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...
- 如何设计一个简单的C++ ORM
2016/11/15 "没有好的接口,用C++读写数据库和写图形界面一样痛苦" 阅读这篇文章前,你最好知道什么是 Object Relation Mapping (ORM) 阅读这 ...
- 使用fastjson前台报406的问题解决方法
返回的json数据前台页面报406,而后台没有报错,下面为解决方法 <?xml version="1.0" encoding="UTF-8"?> & ...
- Android(java)学习笔记202:Handler消息机制的原理和实现
联合学习 Android 异步消息处理机制 让你深入理解 Looper.Handler.Message三者关系 1. 首先我们通过一个实例案例来引出一个异常: (1)布局文件activity_m ...
- windows10UWP:Segoe MDL2 图标指南
windows10 UWP 开发中,图标的使用非常广泛.为此,微软建议大家使用 Segoe MDL2 Assets 字体来获取图标.Segoe MDL2 Assets 包括了哪里图标,微软在 http ...
- Python之路【第十七篇】:Django【进阶篇】
Python之路[第十七篇]:Django[进阶篇 ] Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...