Chocolate


Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

In 2100, ACM chocolate will be one of the favorite foods in the world.
"Green, orange, brown, red��", colorful sugar-coated shell maybe is the most attractive feature of ACM chocolate. How many colors have you ever seen? Nowadays, it's said that the ACM chooses from a palette of twenty-four colors to paint their delicious candy bits.

One day, Sandy played a game on a big package of ACM chocolates which contains five colors (green, orange, brown, red and yellow). Each time he took one chocolate from the package and placed it on the table. If there were two chocolates of the same color on the table, he ate both of them. He found a quite interesting thing that in most of the time there were always 2 or 3 chocolates on the table.

Now, here comes the problem, if there are C colors of ACM chocolates in the package (colors are distributed evenly), after N chocolates are taken from the package, what's the probability that there is exactly M chocolates on the table? Would you please write a program to figure it out?

Input

The input file for this problem contains several test cases, one per line.

For each case, there are three non-negative integers: C (C <= 100), N and M (N, M <= 1000000).

The input is terminated by a line containing a single zero.

Output

The output should be one real number per line, shows the probability for each case, round to three decimal places.

Sample Input

5 100 2
0

Sample Output

0.625

2100年,ACM牌巧克力将风靡全球。

题意

“绿的,橘红的,棕色的,红的…”,彩色的糖衣可能是ACM巧克力最吸引人的地方。你一共见过多少种颜色?现在,据说ACM公司从一个24种颜色的调色板中选择颜色来装饰他们的美味巧克力。

有一天,Sandy用一大包有五种颜色的巧克力玩了一个游戏。每次他从包里拿出一粒巧克力并把它放在桌上。如果有桌上有两粒相同颜色的巧克力,他就把他们吃掉。他惊奇的发现大多数时候桌上都有2到3粒巧克力。

如果ACM巧克力有C(1≤C≤100)种颜色,在拿出了N(1≤N≤1000000)粒巧克力之后在桌上恰有M(1≤M≤1000000)粒的概率是多少?

分析

如果N不是那么大的话,我们是很容易用动态规划来解决此题,状态转移方程就是

Pi+1,k=Pi,k-1*(C-k-1)/C+Pi,k+1*(k+1)/C

其中Pi,k表示拿出了i粒巧克力后桌上剩余M粒的概率(当然还要考虑一些边界情况)。但是现在N可以达到1000000,如果直接动态规划肯定是要超时的。

这个题的标准算法是使用生成函数。也就是说把“桌上有m块巧克力”转化成“有m种巧克力取了奇数块,其余的都取偶数块的取法”。所以就可以列出生成函数是,所以总的取法数就是xn的系数乘以n!和C(c,m),而概率就是总的取法数除以cn,然后通过进一步的代数分析来化简解决。这种方法当然是很优秀的,复杂度是O(c2)。但是由于这道题的精度要求很低,迭代的方法也是可以达到目的的,而且复杂度也接近O(c2)。

这道题里不会出现极大或极小的概率,一般来说这种情况下的收敛是比较快的。我们可以不断的计算P的值,当它的变化不足以影响结果时就停止计算。当然这道题里的收敛是分奇偶的(显然桌上剩余的巧克力数和拿出的巧克力数是同奇偶的),所以不能比较Pi和Pi-1,而要比较Pi和Pi-2,只要看到Pi和Pi-2差距小于一个定值(比如1e-5),而且i和N同奇偶,就可以停止动态规划,因为此时继续规划下去所产生的不同已经不可能影响到要输出的结果。经过实验发现,最大的数据也只在几百次迭代中就稳定了,这样就将效率大大提高,满足了题目的要求。

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=;
double f[N][N];int n,m,c;
int main(){
while(~scanf("%d",&c)&&c){
scanf("%d%d",&n,&m);
if(!n&&!m){puts("1.000");continue;}
if(m>n||m>c||(n+m)&){puts("0.000");continue;}
if(n>) n=+(n&);
memset(f,,sizeof f);f[][]=;
for(int i=;i<=n;i++){
for(int j=;j<=min(i,c);j++){
if(i+j&){
f[i][j]=;continue;
}
if(j>) f[i][j]+=f[i-][j-]*(-(double)(j-)/(double)c);
if(j<i) f[i][j]+=f[i-][j+]*(double)(j+)/(double)c;
}
}
printf("%.3lf\n",f[n][m]);
}
return ;
}

ZOJ1363 Chocolate的更多相关文章

  1. ZOJ1363 Chocolate 【生成函数】 【泰勒展开】

    题目大意: 有c种不同的巧克力,每种无限个,意味着取出每种的几率每次为1/c.现在你需要取n次.然后将统计每种取出来的巧克力的数量.若为偶数则舍去,否则留下一个.问最后留下m个的概率是多少. 题目分析 ...

  2. Big Chocolate

    Big Chocolate 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19127 Big Chocolat ...

  3. Dividing a Chocolate(zoj 2705)

    Dividing a Chocolate zoj 2705 递推,找规律的题目: 具体思路见:http://blog.csdn.net/u010770930/article/details/97693 ...

  4. hdu----(4301)Divide Chocolate(状态打表)

    多校综合排名前25名的学校请发送邮件到HDUACM@QQ.COM,告知转账信息(支付宝或者卡号) Divide Chocolate Time Limit: 2000/1000 MS (Java/Oth ...

  5. Codeforces Round #340 (Div. 2) B. Chocolate 水题

    B. Chocolate 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co Bob loves everyt ...

  6. Codeforces Round #310 (Div. 1) C. Case of Chocolate set

    C. Case of Chocolate Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/555/ ...

  7. codeforces 678C C. Joty and Chocolate(水题)

    题目链接: C. Joty and Chocolate time limit per test 1 second memory limit per test 256 megabytes input s ...

  8. CodeForces 689C Mike and Chocolate Thieves (二分+数论)

    Mike and Chocolate Thieves 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/G Description ...

  9. Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索

    E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...

随机推荐

  1. Oracle11g select查询时候输出未选定行

    解决方法是: 查询的表名是否是大写的: 是否没有提交执行结果:可以commit一下:

  2. 怎样解决Java Web项目更改项目名后报错

    作为企业级开发最流行的工具,用Myeclipse开发java web程序无疑是最合适的,有时候,我们需要web工程的项目名,单方面的改动工程的项目名是会报错的,那么该如何改web工程项目名呢? 简 单 ...

  3. LR进行接口测试

    其实无论用那种测试方法,接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端,客户端接收应答报文这一个过程. 方法一.用Lo ...

  4. debian、ubuntu:使用apt包管理器可能存在的问题! 让新手望而却步!

    apt包管理器说好真好,说不好真不好. 最近在debian9.ubuntu18.04上安装oracle 10g 玩. 怎么都准备不好安装环境.原因就是i386构架体系的deb包总安装不正确! baid ...

  5. lua封装的位运算

    1.移位运算基础 --与 同为1,则为1 --或 有一个为1,则为1 --非 true为 false,其余为true --异或 相同为0,不同为1 --ZZMathBit = {} function ...

  6. sed在替换的时候,使用变量中的值?如何在sed实现变量的替换?获取到变量中的值?

    需求描述: 今天在做nrpe配置的时候,想要通过批量的方式来将定义文件中的IP给替换掉 开始做的时候没有成功,报错了.在此记录下,如何实现,获取到变量的值,然后 进行替换. 操作过程: 1.原文件的内 ...

  7. mysql中floor函数的作用是什么?

    需求描述: 最近写mysql程序的时候,使用了floor函数,在此记录下该函数的作用 操作过程: 1.使用floor函数的测试 mysql> select floor(1.23),floor(- ...

  8. C# 调用dephi dll 实例

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runti ...

  9. C++ 关键字——friend【转载】

    转载自: http://www.cnblogs.com/CBDoctor/archive/2012/02/04/2337733.html 友元是指: 采用类的机制后实现了数据的隐藏与封装,类的数据成员 ...

  10. SpringMVC------maven编译报错:Dynamic Web Module 3.0 requires Java 1.6 or newer

    如图所示: 但是 Eclipse 明明已经将编译级别设置为 1.7: 这是由于你的 Maven 编译级别是 jdk1.5 或以下,而你导入了 jdk1.6 以上的依赖包:查看 Eclipse 的 Na ...