Description

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 

【题意】给出c,n,m代表c种颜色的糖,从袋子里拿出n颗,放在桌子上,如果已经相同颜色,就把两颗都吃掉,问最后桌子剩m颗的概率

【思路】dp,dp[i][j]=dp[i-1][j-1]*(c-j+1)/c+dp[i-1][j+1]*(j+1)/c;

剪枝,m不可能大于n和c,并且n和m同奇同偶;

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
double dp[][];
int c,n,m;
while(cin>>c,c)
{
scanf("%d%d",&n,&m);
if(m>c||m>n||(n+m)&)
{
printf("0.000\n");
continue;
}
if(n>)//n很大时,误差可忽略不计
n=+(n&);
memset(dp,,sizeof(dp));
dp[][]=;
for(int i=;i<=n;i++)
{
dp[i][]=dp[i-][]/c;
dp[i][c]=dp[i-][c-]/c;
for(int j=;j<c;j++)
{
dp[i][j]=dp[i-][j-]*(double)(c-j+)/c+dp[i-][j+]*(double)(j+)/c;
}
}
printf("%.3f\n",dp[n][m]);
}
return ;
}

Chocolate_DP的更多相关文章

随机推荐

  1. jquery 行交换 上移 下移

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. C#多态问题

    为什么对这个感觉趣呢.因为以前写过两篇关于这个多态和重载混合起来很乱的调用情况分析,自从哪以后,我自认为随便乱写一些继承多态的代码都应该难不到我.但是今天看到一段代码有一个地方计算错误了,所以有必要写 ...

  3. 超棒的响应式设计测试书签和工具(bookmarks)(转)

    一.测试书签(bookmarks) Viewport Resizer 这个书签号称拥有158个国家3万多活跃的用户,主要特性: 完全自定制 方便的添加自定义尺寸 手动的横竖屏切换 自动的横竖屏切换 ( ...

  4. mysql str_to_date字符串转换为日期

    mysql内置函数,在mysql里面利用str_to_date()把字符串转换为日期. 示例:分隔符一致,年月日要一致 select str_to_date('2008-4-2 15:3:28','% ...

  5. Caffe 深度学习框架介绍

    转自:http://suanfazu.com/t/caffe/281 Caffe是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的贾扬清,目前在Google工作. Caffe是 ...

  6. linux 设置windows共享

    在linux下需要设置windows共享. 我们使用SMBA来实现. 1.查看是否安装smba. rpm -qa | grep samba 如果没有安装,则去 www.rpmfind.net 下载sm ...

  7. 尽量少用Include

    当我们使用EF写查询,尤其是关联到多个表的时候,我们一般习惯使用include关联,但是过多地使用include会带来性能问题.作为替代方法,可以使用多个toList来代替. 改造前:

  8. memached 服务器lru算法

    1.LRU是Least Recently Used的缩写,即最近最少使用页面置换算法,是为虚拟页式存储管理服务的.LRU算法的提出,是基于这样一个事实:在前面几条指令中使用频繁的页面很可能在后面的几条 ...

  9. Operating System Concepts with java 项目: Shell Unix 和历史特点

    线程间通信,fork(),waitpid(),signal,捕捉信号,用c执行shell命令,共享内存,mmap 实验要求: 1.简单shell: 通过c实现基本的命令行shell操作,实现两个函数, ...

  10. https://github.com/oneuijs/You-Dont-Need-jQuery

    https://github.com/oneuijs/You-Dont-Need-jQuery