ZOJ1363 Chocolate
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的更多相关文章
- ZOJ1363 Chocolate 【生成函数】 【泰勒展开】
题目大意: 有c种不同的巧克力,每种无限个,意味着取出每种的几率每次为1/c.现在你需要取n次.然后将统计每种取出来的巧克力的数量.若为偶数则舍去,否则留下一个.问最后留下m个的概率是多少. 题目分析 ...
- Big Chocolate
Big Chocolate 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19127 Big Chocolat ...
- Dividing a Chocolate(zoj 2705)
Dividing a Chocolate zoj 2705 递推,找规律的题目: 具体思路见:http://blog.csdn.net/u010770930/article/details/97693 ...
- hdu----(4301)Divide Chocolate(状态打表)
多校综合排名前25名的学校请发送邮件到HDUACM@QQ.COM,告知转账信息(支付宝或者卡号) Divide Chocolate Time Limit: 2000/1000 MS (Java/Oth ...
- Codeforces Round #340 (Div. 2) B. Chocolate 水题
B. Chocolate 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co Bob loves everyt ...
- 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/ ...
- codeforces 678C C. Joty and Chocolate(水题)
题目链接: C. Joty and Chocolate time limit per test 1 second memory limit per test 256 megabytes input s ...
- CodeForces 689C Mike and Chocolate Thieves (二分+数论)
Mike and Chocolate Thieves 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/G Description ...
- Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...
随机推荐
- tf.variable_scope
转载:https://blog.csdn.net/gaoyueace/article/details/79079068 例如: #在名字为ae的命名空间内创建变量 with tf.variable_s ...
- Java 利用POI操作PPT
解析PPT文件中的图片 import java.io.File; import java.io.FileOutputStream; import org.apache.poi.hslf.HSLFSli ...
- Java 从静态代理到动态代理
先举个静态代理的例子,可能多少有些不恰当,不过本次学习记录,重点不在于通信协议. 比如你在一个机房里,你不能联网,只能连通过一台能连公网的代理机器上网.你发送了一个http请求,将由代理帮你上网. 首 ...
- bash: /usr/bin/npm: No such file or directory
一个整得很烂了的Ubuntu服务器, 各种问题乱出. npm老是升不到最新版(一直显示1.4),于是我干脆删了, 结果再去装却装不上了, 如果用apt-get install npm安装, 就得到如下 ...
- jmeter正则表达式提取器--关联
http://desert3.iteye.com/blog/1394934 1.http://www.cnblogs.com/quange/archive/2010/06/11/1756260.htm ...
- Windows 下 绿化 Oracle
既然Oracle在非windows平台上可以很“绿色”的执行,那么在windows平台上有无可能呢? 答案是“Yes-No” 基本上,除了监听器(lisentner)这个异类外,Oracle实例完全可 ...
- 用plsql 导入导出oracle表结构数据
一.导出 (1)导出数据 进入pl/sql,"工具"---->"Export Tables...",然后在弹出的对话框中选择要导出的表,最后点击" ...
- node.js富文本编辑器
摘要: 最近在搭建自己的博客,这一段时间可能没有时间来写博客了,但是有了好东西还是要分享给大家.博客网站必然要有编辑文章的编辑器,所以在网上查了些资料.大部分编辑器的后台是基于java.php.asp ...
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression
if (!string.IsNullOrEmpty(FarmWorkId)) { data = data.Where(p => p.TypeId == Convert.ToInt32(FarmW ...
- iOS 开发,工程中混合使用 ARC 和非ARC(转)
[前提知识] ARC:Automatic Reference Counting,自动引用计数 在开发 iOS 3 以及之前的版本的项目时我们要自己负责使用引用计数来管理内存,比如要手动 retain. ...