B - Candy

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description: 
System Crawler  (2014-10-17)

Description

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?
 

Input

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

Output

For 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
 
公式

快速排列组合函数:logC(m,n),zuhe[i]其实就是i的阶乘,然后给取log值
这样zuhe[m]-zuhe[n]-zuhe[m-n]就是C(m,n)的log值
然后把其余也取对数,然后再求exp就好了!
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define M(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f using namespace std; int n;
double p;
double zuhe[]; double logC(int m,int n)
{
return zuhe[m]-zuhe[n]-zuhe[m-n];
} int main()
{
int cas = ;
zuhe[] = ;
for(int i = ;i<;i++) zuhe[i] = zuhe[i-]+log(i*1.0);
while(scanf("%d%lf",&n,&p)==)
{
double res = ;
for(int i = ;i<=n;i++)
{
res+=(n-i)*exp(logC(n+i,i)+(n+)*log(p)+(i)*log(-p));
res+=(n-i)*exp(logC(n+i,i)+(n+)*log(-p)+(i)*log(p));
}
printf("Case %d: %.16f\n",cas++,res);
}
return ;
}

2012Chengdu B (快速组合数)的更多相关文章

  1. NOIP2011多项式系数[快速幂|组合数|逆元]

    题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别为 a ,b ,k , ...

  2. 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数

    1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...

  3. UVALive 7040 Color (容斥原理+逆元+组合数+费马小定理+快速幂)

    题目:传送门. 题意:t组数据,每组给定n,m,k.有n个格子,m种颜色,要求把每个格子涂上颜色且正好适用k种颜色且相邻的格子颜色不同,求一共有多少种方案,结果对1e9+7取余. 题解: 首先可以将m ...

  4. Paths on a Grid POJ - 1942 组合数学 (组合数的快速计算)

    题意:格路问题 没什么难度 难点在于如何快速计算相对较大的组合数 思路:运用手写计算组合数的方式进行计算  如c(8,3) 如果手算就是   8*7*6/(3*2*1)这样可以很快得解出 计算代码为: ...

  5. Gym - 101775A Chat Group 组合数+逆元+快速幂

    It is said that a dormitory with 6 persons has 7 chat groups ^_^. But the number can be even larger: ...

  6. HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description   Sample Input 2 Sample Outp ...

  7. 【BZOJ4870】组合数问题(动态规划,矩阵快速幂)

    [BZOJ4870]组合数问题(动态规划,矩阵快速幂) 题面 BZOJ 洛谷 题解 显然直接算是没法做的.但是要求的东西的和就是从\(nk\)个物品中选出模\(k\)意义下恰好\(r\)个物品的方案数 ...

  8. [BZOJ3209]花神的数论题 组合数+快速幂

    3209: 花神的数论题 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2498  Solved: 1129[Submit][Status][Disc ...

  9. 牛客网 牛客小白月赛1 I.あなたの蛙が帰っています-卡特兰数,组合数阶乘逆元快速幂

    I.あなたの蛙が帰っています   链接:https://www.nowcoder.com/acm/contest/85/I来源:牛客网     这个题有点意思,是卡特兰数,自行百度就可以.卡特兰数用处 ...

随机推荐

  1. nginx添加proxy_cache模块做缓存服务器

    业务需求nginx对后端tomcat(静态文件)做缓存 减轻后端服务器的压力 # nginx-1.6.2.tar.gz  ngx_cache_purge-2.3.tar.gz #编译安装 ./conf ...

  2. Codeforces 711E ZS and The Birthday Paradox

    传送门 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output st ...

  3. Autofac的高级使用——Autofac.2.6.3.862

    Autofac的高级使用——Autofac.2.6.3.862 目录(?)[-] 使用代码方式进行组件注册依赖服务类和组件类 使用配置文件进行组件注册不需要依赖 定义配置文件 读取config配置文件 ...

  4. PHPExcel使用体会

    PHPExcel使用体会 因为毕设导师智能分配系统的需要,系负责人在管理学生和导师时,希望可以使用Excel批量导入学生和导师的信息,学长的报课系统使用的是PHPExcel的类库,于是我也抽空花了2天 ...

  5. HTML5系列一(属性概述)

    HTML5概述 HTML5於2004年被WHATWG(网页超文本技术工作小组)提出,於2007年被W3C接纳 在2008年1月22日,第一份正式草案已公布.WHATWG表示该规范是目前正在进行的工作, ...

  6. ubuntu --- shortcut key

    Linux系统下图形界面与Linux命令行模式的切换的方法 由图形转换到控制台模式:ctrl+alt+f1~f6(同时按下3秒钟不要马上松开)....由控制台转向图形模式是:alt+f7 快捷键(ub ...

  7. 常用的工具类4-IP类

    public static class IpHelper { /// <summary> /// 获取Ip /// </summary> /// <returns> ...

  8. nodejs fs module

    fs.watchFile(filename[, options], listener)# Added in: v0.1.31 filename <String> | <Buffer& ...

  9. windows7-SQLyog 安装图解

    双击: 双击已下载的SQLyog Enterprise 安装文件,点击“next”,选择“I accept...”,勾选安装组件,选择安装目录,等待安装完成. 协议:选择我接受 选择操作   选择路径 ...

  10. JavaWeb学习笔记——DAO设计模式