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 ...
随机推荐
- HDU 4162 Shape Number
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4162 题意: 求给定字符的一阶差分链的最小表示. 题解: 先求一阶差分链,再求一阶差分链的最小表示法 ...
- redis window环境下的安装地址
https://github-cloud.s3.amazonaws.com/releases/3402186/25358446-c083-11e5-89cb-61582694855e.zip?X-Am ...
- Properties --- C++读配置信息的类
http://blog.csdn.net/billow_zhang/article/details/4304980 在开发实践中,积累了一些通用的C++ 类库,在此写出来给大家分享.也希望能给出更好的 ...
- C# 使用TimeSpan计算两个时间差
转载:http://www.cnblogs.com/wifi/articles/2439916.html 可以加两个日期之间任何一个时间单位. private string DateDiff(Date ...
- /bin/bash^M: bad interpreter: No such file or dire
在执行shell脚本时提示这样的错误主要是由于shell脚本文件是dos格式,即每一行结尾以\r\n来标识,而unix格式的文件行尾则以\n来标识. 查看脚本文件是dos格式还是unix格式的几种办 ...
- poj 3604 Professor Ben
质因数分解:牛人推导公式(1^3+2^3+……+(1+a1)^3)*……*(1^3+2^3+……+(1+ai)^3)…… 链接http://poj.org/problem?id=3604 #inclu ...
- Jmeter 快速入门教程(二)--创建简单web测试
[版权所有: whoistester.com & jmeter.cf] http://wenku.baidu.com/linkurl=9zc4VHe6vUUeMdDZPpNsRehkazZFw ...
- Oracle 6 - 锁和闩 - transaction的可串行化
本文主要内容 1.transaction的可串行化 2.数据库并发带来的问题, dirty read, Nonrepeatable reads, Phantoms幻读 3.隔离级别和2中的问题 4. ...
- Floodlight 防火墙是如何起作用的
前言 用mininet + floodlight搭建好环境之后,运行flooglight,然后在mininet中建立网络拓扑,建好之后,pingall,所有host之间可以ping通. 然后向控制器f ...
- mac 设置 ll 等alias 并永久生效
往上看了在.bash_profile中配置 然后 source 的方法, 试过了, 只是当前的终端有效,当电脑重启或者关闭终端就失效了,只好看看 mac 的 profile 代码 # System- ...