LazyChild is a lazy child who likes candy very much. Despite being very young, he has two large candy boxes, each contains n candies initially. Everyday he chooses one box and open it. He chooses the first box with probability p and the second box with probability (1 - p). For the chosen box, if there are still candies in it, he eats one of them; otherwise, he will be sad and then open the other box.
He has been eating one candy a day for several days. But one day, when opening a box, he finds no candy left. Before opening the other box, he wants to know the expected number of candies left in the other box. Can you help him?

InputThere are several test cases.

For each test case, there is a single line containing an integer n (1 ≤ n ≤ 2 × 10
5) and a real number p (0 ≤ p ≤ 1, with 6 digits after the decimal).

Input is terminated by EOF.OutputFor each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is a real number indicating the desired answer.

Any answer with an absolute error less than or equal to 10
-4 would be accepted.Sample Input

10 0.400000
100 0.500000
124 0.432650
325 0.325100
532 0.487520
2276 0.720000

Sample Output

Case 1: 3.528175
Case 2: 10.326044
Case 3: 28.861945
Case 4: 167.965476
Case 5: 32.601816
Case 6: 1390.500000 求期望,用log来提高精度并且实现组合数。
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<math.h>
#define ll __int64
using namespace std;
#define N 1000005
double a[N<<1];
int main()
{
a[0]=0;
for(int i=1;i<N*2;i++)
a[i]=a[i-1]+log(1.0*i);
int n;
double p;
int t=0;
while(~scanf("%d%lf",&n,&p))
{
double ans=0,res1,res2;
for(int i=0;i<=n;i++)
{
res1=(a[2*n-i]-a[n]-a[n-i])+(n+1)*log(p)+(n-i)*log(1-p);
res2=(a[2*n-i]-a[n]-a[n-i])+log(1-p)*(n+1)+log(p)*(n-i);
ans+=exp(res1)*i+exp(res2)*i;
}
printf("Case %d: %.6f\n",++t,ans);
}
}

  

hdu_4465_Candy的更多相关文章

随机推荐

  1. 基础架构之GitLab

    Git几乎是软件开发人员的必备工具了,关于代码管理,公司都一般都会搭建自己的仓库,关于GitLab的详细介绍参见官方网站详见 https://about.gitlab.com,这篇文章主要介绍安装及使 ...

  2. 在Linux中安装redmine

    Redmine是用Ruby开发的基于web的项目管理软件,是用ROR框架开发的一套跨平台项目管理系统. 如下即为安装步骤: (1)配置ruby环境,可用rvm进行安装匹配,参考http://ruby- ...

  3. androidtab

    https://github.com/H07000223/FlycoTabLayout tensorflow https://github.com/topics/tensorflow-examples ...

  4. 如何有效防止API的重放攻击(转自阿里云)

    API重放攻击(Replay Attacks)又称重播攻击.回放攻击,这种攻击会不断恶意或欺诈性地重复一个有效的API请求.攻击者利用网络监听或者其他方式盗取API请求,进行一定的处理后,再把它重新发 ...

  5. devexpress 中 XtraTabcontrol 改变BackColor 的方法

    装载自CSDN,原文地址  https://blog.csdn.net/zjzytnn/article/details/53699340 今天在实现CAD文件的读和显示操作的时候,需要将CAD文件显示 ...

  6. Laravel Query Builder 复杂查询案例:子查询实现分区查询 partition by

    案例 案例:Laravel 在文章列表中附带上前10条评论?,在获取文章列表时同时把每个文章的前10条评论一同查询出来. 这是典型分区查询案例,需要根据 comments 表中的 post_id 字段 ...

  7. hydra 常用的命令

    1.本地调试模式运行项目  /gaea hydra /zk节点名 -r "zk://zk地址" -t "节点标识" -d -w 项目相对于$GOPATH/src ...

  8. dll的制作

    https://blog.csdn.net/guanchanghui/article/details/1621031

  9. gulp 前端构建工具入门

    gulp 前端构建工具入门 标签(空格分隔): gulp 1. 安装gulp npm i -g gulp 2. 创建gulp项目 2.1 Hello world 使用npm init初始化项目文件夹. ...

  10. 【转】Java中的String,StringBuilder,StringBuffer三者的区别

    https://www.cnblogs.com/su-feng/p/6659064.html 最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及String ...