hdu_4465_Candy
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的更多相关文章
随机推荐
- 原生ajax与封装的ajax使用方法
当我们不会写后端接口来测试ajax时,我们可以使用node环境创建一个本地服务器. 1.创建一个本地服务器可参考http://www.cnblogs.com/heyujun-/p/6793900.ht ...
- ArrayList的使用方法详解(转)
1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和I ...
- Redis源码学习1-sds.c
https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/sds.c#L120 /* SDSLib, A C dynami ...
- ASPNET MVC Error 403.14
今天创建了一个新的ASPNET MVC 项目部署到本地, 生成成功后在浏览器中输入URL却发现报这个错 解决办法: 因为我的站点是4.5的,但是我没有设置Application Pool所以当前还是默 ...
- July 09th 2017 Week 28th Sunday
He that boasts of his own knowledge proclaims ignorance. 夸耀知识实乃无知. Honestly speaking, I don't agree ...
- 【Asp.Net MVC】asp.net mvc Model验证总结及常用正则表达式
转自:http://www.cnblogs.com/easy5weikai/p/3843131.html 关于Model验证官方资料: http://msdn.microsoft.com/zh-cn/ ...
- 在giuhub上演示自己的项目
首先在github上建立项目,然后git clone; 然后切换分支到 git checkout gh-pages 最后提交代码到这个分支上,访问地址:[github用户名].github.io/[项 ...
- IntelliJ IDEA中 查看某个类中的所有方法
方法一:alt + 7 方法二: ctrl + F12 方法三: 自定义 File Structure
- Type Systems
This section deals with more theoretical aspects of types. A type system is a set of rules used by a ...
- luogu P1462 通往奥格瑞玛的道路
嘟嘟嘟 这道题的题面相当的迷,我看了半天都没懂.最后看了题解的解释才懂. 他是这么个意思:对于所有能活着走到终点的路径,输出每一条路径中过路费最多的城市的最小值. 那么自然想到二分过路费,然后用dij ...