组合数,对数。

这道题要用到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的更多相关文章

  1. [水题日常]UVA1639 糖果(Candy,ACM/ICPC Chengdu 2012)

    今天来尝试了几道数学期望相关的题,这是我认为比较有趣的一道题 这次不废话啦直接开始~ 一句话题意:两个分别装有n个糖果的盒子,每次随机选一个盒子然后拿走一颗糖(选的概率分别是\(p\)和\((1-p) ...

  2. [LeetCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. Leetcode Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  4. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  5. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. 【leetcode】Candy(hard) 自己做出来了 但别人的更好

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  7. 【leetcode】Candy

    题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  8. 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 ...

  9. [LintCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

随机推荐

  1. C语言基础:指针类型与指针和数组、字符串的关系

    //指针变量就是用来存储地址的,只能存储地址 格式:  int  *p;  这个p为指针变量:指针变量占8个字节 类型是用来说明这个指针指向的类型: 比如上边的int代表这个指针变量会指向int类型的 ...

  2. bnuoj 4208 Bubble sort

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=4208 [题意]:如题,求冒泡排序遍历趟数 [题解]:这题开始2B了,先模拟TLE,然后想了一下,能不 ...

  3. 2433: [Noi2011]智能车比赛 - BZOJ

    Description 新一届智能车大赛在JL大学开始啦!比赛赛道可以看作是由n个矩形区域拼接而成(如下图所示),每个矩形的边都平行于坐标轴,第i个矩形区域的左下角和右上角坐标分别为(xi,1,yi, ...

  4. DX SetFVF

    自由顶点格式(flexible vertex format,FVF) http://www.cnblogs.com/xmzyl/articles/1604096.html if( SUCCEEDED( ...

  5. 【Unity--Apwork框架】AOP编程--拦截,用于缓存和异常处理(Unity框架的拦截注入-Interception)

    第一步:定义拦截行为:CachingBehavior 和 ExceptionLoggingBehavior 他们都继承接口:IInterceptionBehavior (程序集 Microsoft.P ...

  6. java后台生成zip打包文件

    /** * * @param zipFile 压缩包文件对象 * @param listKey 压缩的图片物理地址 * @return */ public static boolean package ...

  7. winform中的checkedListbox数据源绑定

    首先看清楚一点 winform下该控件的名称叫做:checkedListbox webform下叫做CheckBoxList 不知道这样起名的用意何在,这个别管了,看看用法吧. web下很简单,直接设 ...

  8. docker设置代理

    在天朝使用docker需要FQ. 下面给出docker的代理方式: HTTP_PROXY=http://10.167.251.83:8080 docker -d

  9. codeforces div.1 A

    A. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...

  10. 6.5 THUSC 考试题解

    QAQ 由于并没有数据,而且没有A掉的是提交答案题目,所以并没有修改 QAQ 只能放题解了,代码还没有拿到,不过在清华听了一波习题讲评的安利 第一题 成绩单 先说暴力分 对于单调序列来说最优决策一定是 ...