uva1639 Candy
组合数,对数。
这道题要用到20w的组合数,如果直接相乘的话,会丢失很多精度,所以用去对数的方式实现。
注意指数,因为取完一次后,还要再取一次才能发现取完,所以是(n+1)次方。
double 会爆掉,需要用long double
然后就是scanf和printf读入输出long doube会发生不可逆转的错误(dev-cpp),所以可以读入输出时候强制转换类型,或者用cin,cout(后者我感觉比较麻烦)。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<iomanip>
using namespace std;
const int maxn = 200000; long double v1,v2,c;
long double p,p1,p2,res;
double pd,resd;
int n,kase;
long double f[maxn*2+10]; double logC(int n,int m) {
return f[n]-f[m]-f[n-m];
} inline void init() {
for(int i=1;i<=maxn*2;i++) f[i]=f[i-1]+log(i);
} int main() {
init();
/*
while(scanf("%d",&n)==1) {
scanf("%lf",&pd);
p=pd;
p1=p2=1;
res=0;
for(int i=1;i<=n;i++) {
c=logC(2*n-i,n);
v1=c+(n+1)*log(p)+(n-i)*log(1-p);
v2=c+(n+1)*log(1-p)+(n-i)*log(p);
res+=(double) i*(exp(v1)+exp(v2));
}
resd=res;
printf("Case %d: %.6lf\n",++kase,resd);
}*/
// 上面的代码是对的,嗯。我就想用cin,cout.
while(cin>>n) {
cin>>p;
p1=p2=1;
res=0;
for(int i=1;i<=n;i++) {
c=logC(2*n-i,n);
v1=c+(n+1)*log(p)+(n-i)*log(1-p);
v2=c+(n+1)*log(1-p)+(n-i)*log(p);
res+=(double) i*(exp(v1)+exp(v2));
}
cout << "Case "<<++kase<<": ";
cout << setprecision(6) <<fixed<< res<<'\n';
}
// 明显感觉还是 cstdio大法好。
return 0;
}
uva1639 Candy的更多相关文章
- [水题日常]UVA1639 糖果(Candy,ACM/ICPC Chengdu 2012)
今天来尝试了几道数学期望相关的题,这是我认为比较有趣的一道题 这次不废话啦直接开始~ 一句话题意:两个分别装有n个糖果的盒子,每次随机选一个盒子然后拿走一颗糖(选的概率分别是\(p\)和\((1-p) ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【leetcode】Candy(hard) 自己做出来了 但别人的更好
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 【leetcode】Candy
题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
- [LintCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
随机推荐
- 面试问到struts1与struts2的解析对比
一.struts1要继承一个抽象类.struts1是类编程而不是接口编程. struts2的action可以实现一个action接口,也可以实现其他的接口,使其成为可选的定制的服务. 二.struts ...
- python logging TimedRotatingFileHandler 作用
max backup count这样的参数,即打印的日志文件数量超过这个阈值,发生rotate,从最老的文件开始清理 未亲测.
- DispatcherServlet--Spring的前置控制器作用简介
参考网址:http://jinnianshilongnian.iteye.com/blog/1602617 Struts有一个ActionServlet,用来完成前置控制器(分发器)的功能.其实,所有 ...
- 剑指offer--面试题7
//两个栈实现一个队列 #include<stack> //STL #include<iostream> using namespace std; template<cl ...
- hdu 2196
树形dp 本文出自 http://blog.csdn.net/shuangde800 题目传送门 题意: 给出一棵树,求离每个节点最远的点的距离 思路: 把无根树转化成有根树分析, 对于上面那棵树 ...
- Searching a 2D Sorted Matrix Part I
Write an efficient algorithm that searches for a value in an n x m table (two-dimensional array). Th ...
- CentOS中基于不同版本安装重复包的解决方案
http://blog.chinaunix.net/uid-21710705-id-3039675.html
- [优先队列]HDOJ5360 Hiking
题意:有n个人,每个人有两个参数$l$和$r$ 邀请他们去hiking, 当 当前已经邀请到的人数大于等于$l$,并且小于等于$r$,那么这个人就会去 问最多能邀请到几个人 并输出 依次要邀请的人的 ...
- lintcode :链表插入排序
题目: 链表插入排序 用插入排序对链表排序 样例 Given 1->3->2->0->null, return 0->1->2->3->null 解题: ...
- [GCJ]Password Attacker
https://code.google.com/codejam/contest/4214486/dashboard#s=p0 排列组合.DP递推式,如下代码.dp[m][n]表示长度为n的字符串里有m ...