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. Codeforces Round #413 A. Carrot Cakes

    A. Carrot Cakes time limit per test   1 second memory limit per test   256 megabytes   In some game ...

  2. 01_java虚拟机基础入门

    [Java虚拟机的基本结构] [ 1.类加载子系统 ] 负责从文件系统或者网络中加载Class信息,加载的信息存放在一块称之为方法区的内存空间. [ 2.方法区 ] 存放类信息.常量信息.常量池信息, ...

  3. Java集合排序

    [ 1.对普通的包装类基本数据类型的list数组排序(Integer,Long,Double) ] Collections.sort(List list) [例] List<Long> m ...

  4. Windows系统中Oracle11g R2 版本数据库卸载

    1. 停止"服务"中所有的ORCLE服务. 进入服务的方法很多,如: (1)在运行中输入services.msc,然后找到所有跟oracle 有关的服务. (2)开始->设置 ...

  5. 【JAVA语法】03Java-继承性

    继承的实现 继承的限制 子类对象的实例化 方法的重写 Super关键字 重写与重载的区别 final关键字 抽象类 接口 一.继承的实现 1.1 格式 class 子类 extends 父类 {} c ...

  6. klee源码阅读笔记1--STPBuilder类

    初始化过程中四个数据成员中的两个数据成员被初始化: 一.vc被初始化为STP提供的C调用接口函数vc_createValidityChecker(): 二.optimizeDivides被初始化为fa ...

  7. Linux->解决用userdel删除不掉用户的问题

    情况: 一般我们移除,都是先把用户从组中删除,再依次把组删掉,但是这里出现了问题: root@ per# userdel -r mysql userdel: user mysql is current ...

  8. 7za 命令解析

    转载自:blog.chinaunix.net/uid-26330274-id-3055157.html 7za 命令讲的很详细,收藏下来. 命令行压缩解压一 7z   1) 简介 7z,全称7-Zip ...

  9. 删除datatable的行后,出现“不能通过已删除的行访问该行的信息”的错误,即DeletedRowInaccessibleException

    删除datatable的行后,出现“不能通过已删除的行访问该行的信息”的错误 =========================================================== 采 ...

  10. 中间人攻击-ARP毒化

    感谢Heee投递 中间人攻击虽然古老,但仍处于受到黑客攻击的危险中,可能会严重导致危害服务器和用户.仍然有很多变种的中间人攻击是有效的,它们能够很容易的欺骗外行并且入侵他们.正如字面意思一样,中间人攻 ...