[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 思路:题目要求判断三个字母是否是条纹型的,而且宽和高相同,那么先求出 ...
随机推荐
- C++一些注意点之操作符重载
重载操作符需要注意 (1)重载操作符必须具有一个类类型操作数.不能重载内建类型的操作符. operator +(int,int);//这个是错误的,都为内建类型 operator +(int,clas ...
- Spring DI模式 小样例
今儿跟同事讨论起来spring早期的,通过大篇幅xml的配置演变到今天annotation的过程,然后随手写了个小样例,感觉还不错,贴到这里留个纪念. 样例就是用JAVA API的方式, ...
- gprof + kprof + gprof2dot (性能 与 函数调用图)-
http://www.cnblogs.com/rocketfan/archive/2009/11/15/1603465.html http://blog.csdn.net/stanjiang2010/ ...
- json-lib-2.4-jdk15.jar maven
最近自己将一个web项目装换到使用mevan自动管理. 遇到了一个json包导入的问题.最终解决如下: <!-- https://mvnrepository.com/artifact/net.s ...
- python 学习笔记(二)两种方式实现第一个python程序
在交互模式下: 如果要让Python打印出指定的文字,可以用print语句,然后把希望打印的文字用单引号或者双引号括起来,但不能混用单引号和双引号: >>> print 'hello ...
- Winform 程序中dll程序集嵌入exe可执行文件
关于这方面,Google一下有很多方法,参考: http://blog.csdn.net/astonqa/article/details/7300856 但按照以上的方法我并没有成功,于是继续找到了一 ...
- HTML5 <Audio/>标签Api整理(二)
1.实例2: 相对较完整 Html代码: <style> #volumeSlider .slider-selection { background:#bababa; } </styl ...
- 我的第一个boke
哈哈哈 接口
- swift把汉字转换为拼音,并且截取首字母做索引用
var transformContents = CFStringCreateMutableCopy(nil, 0, "咋啊的看到回复阿斯顿发货发哦iasdifas")CFStrin ...
- OC - 22.隐式动画
简介 每个UI控件,默认自动创建一个图层(根图层),即每个UI控件对应于至少一个图层 每一个UIView内部都默认关联着一个CALayer,我们可用称这个Layer为Root Layer(根层) ...