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 ...
随机推荐
- C++(MFC)
C++(MFC) 1.关联变量(与控件关联): (1)一组单选按钮:需要将第一个单选按钮的Group选项设为true,则单选按钮就为一组(组框为标示作用).选中第一个则为0,第二个为1,依次类推(P2 ...
- Careercup - Facebook面试题 - 4713484755402752
2014-05-02 00:30 题目链接 原题: Given two arrays of sorted integers, merge them keeping in mind that there ...
- spring mvc 学习笔记【1】---前言
扎扎实实地掌握好技术 ----------------------------------------------------------------------------------------- ...
- Ext学习-HelloWorld以及基础环境搭建
1.目的 在eclipse中搭建EXTJS的基础环境,学习如何按照从官方下载的代码中搭建新的Extjs工程 2.主要包含内容: 1.ExtJS文件下载以及目录说明 2.ExtJS开发环境 ...
- PHP Zend Studio9.0怎么把代码搞成和服务器端的同步(就是直接在服务器端修改)
Zend Studio 可以直接通过Remote System的方式直接连接服务器端的代码,就是可以直接修改服务器端的代码,不过修改的时间小心点,修改就会立即生效的. 选择Remote Systems ...
- 一个Java程序员应该掌握的10项技能
1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...
- Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- POJ 2080 Calendar(很水的模拟)
刚开始一直WA,才发现原来代码中两处减去年份.月份的天数的判断条件用的是>=,虽然最后考虑n=0要退回一天的情况,但还是WA.后来改成>的条件判断,省去了考虑n=0的麻烦,AC. 此题无非 ...
- D&F学数据结构系列——红黑树
红黑树 定义:一棵二叉查找树如果满足下面的红黑性质,则为一棵红黑树: 1)每个结点不是红的就是黑的 2)根结点是黑的 3)每个叶结点是黑的 4)如果一个结点是红的,它的两个儿子都是黑的(即不可能有两个 ...
- 0环境设置 - AUTOTRACE设置
Autotrace是sqlplus的一个工具,用来显示所执行查询的查询计划 设置步骤 • cd [ORACLE_HOME]/rdbms/admin• log into SQL*Plus as SYST ...