Chocolate_DP
Description
"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
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
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的更多相关文章
随机推荐
- hdu----(4686)Arc of Dream(矩阵快速幂)
Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- 在Linux下安装aws命令行操作
使用安装包安装 环境: Linux, OS X, or Unix Python 2 version 2.6.5+ or Python 3 version 3.3+ 检查Python版本 $ pytho ...
- MVC中view页面用jquery方法绑定select控件值
var sortid = '@Model.myWorkMatter.WorkMatterSortID'; $("#selectSort").val(sortid); $(" ...
- C#多态问题
为什么对这个感觉趣呢.因为以前写过两篇关于这个多态和重载混合起来很乱的调用情况分析,自从哪以后,我自认为随便乱写一些继承多态的代码都应该难不到我.但是今天看到一段代码有一个地方计算错误了,所以有必要写 ...
- HDU4815
Little Tiger vs. Deep Monkey Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K ( ...
- SVN clean up问题
问题如截图所示: 解决方法:在根目录clean up,注意把所有的选项都勾上
- 网站开发中必备的8个 jQuery 效果【附源码】
jQuery 作为最优秀 JavaScript 库之一,改变了很多人编写 JavaScript 的方式.它简化了 HTML 文档遍历,事件处理,动画和 Ajax 交互,而且有成千上万的成熟 jQuer ...
- 如果解决ubuntu tab键不能提示命令
/bin/sh is symlinked to /bin/dashTo change it, do:sudo rm /bin/shsudo ln -s /bin/bash /bin/sh 原文:htt ...
- 知名杀毒软件Mcafee(麦咖啡)个人版 资源汇总兼科普(来自卡饭)
虽然早已不是用咖啡了,但我也实时关注的咖啡的一举一动,潜水看帖日久,发现小白众多,好多有价值的帖子淹没于帖海当中,甚是惋惜. 我有如下建议 1.咖啡区管理层,能否吧一些优秀的资源教程 ...
- C#同一位置切换显示两个Panel内容
如果两个panel重合在一起,点击不同按钮切换显示不同的panel,需要xxx.BringToFront(); 1.首先让两个panel的visible都为false, 在加载页面load方法里可以让 ...