[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 思路:题目要求判断三个字母是否是条纹型的,而且宽和高相同,那么先求出 ...
随机推荐
- python小程序——购物
流程图 代码程序 saving = int(input('请输入你的工资:'))shopping = [['iphone',5800],['mx6',2000],['pythonbook',80], ...
- android中listview的一些样式设置
在Android中,ListView是最常用的一个控件,在做UI设计的时候,很多人希望能够改变一下它的背景,使他能够符合整体的UI设计,改变背景背很简单只需要准备一张图片然后指定属性 android: ...
- 初步掌握MapReduce的架构及原理
目录 1.MapReduce定义 2.MapReduce来源 3.MapReduce特点 4.MapReduce实例 5.MapReduce编程模型 6.MapReduce 内部逻辑 7.MapRed ...
- [转] 让ctags支持Javascript
mac下安装exuberant ctags mac 下自带ctags但是功能有限,要使用一些常用的功能需要安装exuberant ctags 下载exuberant ctags 安装exuberant ...
- Android(java)学习笔记203:网页源码查看器(Handler消息机制)
1.项目框架图: 2.首先是布局文件activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com ...
- ZOJ 3905 Cake(贪心+dp)
动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j ...
- vs2010 web 发布
1.在服务器上安装web deploy,这时iis中右侧功能中就多了“导入应用程序” 2.在代码的项目中,点击项目属性,将debug改为release,选择对应的平台.目标平台,主要用来区分32位还是 ...
- Nginx和Apache共存环境下apache获得真实IP
自从Nginx出现以后,我们都喜欢让 Nginx 跑在前方处理静态文件,然后通过 proxy 把动态请求过滤给 apache.这么有个问题,跑在后方 apache 上的应用获取到的IP都是Nginx所 ...
- OC - 19.GCD
简介 GCD(Grand Center Dispatch)是Apple为多核的并行运算提出的解决方案,纯C语言 更加适配多核处理器,且自动管理线程的生命周期,使用起来较为方便 GCD通过任务和队列实现 ...
- 数字证书文件cer和pfx的区别
作为文件形式存在的证书一般有这几种格式: 1.带有私钥的证书 由Public Key Cryptography Standards #12,PKCS#12标准定义,包含了公钥和私钥的二进制格式的证书形 ...