Recently one of my friend Tarik became a member of the food committee of an ACM regional competition. He has been given m distinguishable boxes, he has to put n types of chocolates in the boxes. The probability that one chocolate is placed in a certain box is 1/m. What is the probability that one or more boxes are empty? At first he thought it as an easy task. But soon he found that it was much harder. So, he falls into a great trouble and asked you to help him in this task. Input Each line of the input contains two integers n indicating total number of distinguishable types of chocolate and m indicating total number of distinguishable boxes (m ≤ n < 100). A single line containing ‘-1’ denotes the end. Output For each of the cases you should calculate the probability corrected to seven decimal places. The output format is shown below.

Sample Input

50 12

50 12

-1

Sample Output

Case 1: 0.1476651

Case 2: 0.1476651

思路:题目要求一个或者 多个盒子为空的概率,可以先求所有盒子都不为空的概率;

  设dp[i][j]为i个巧克力放入j个盒子的概率;

  分为两种情况:1)前i-1个巧克力放入了j个盒子:则第i个就放入前j个盒子;

         2)前i-1个巧克力放入了j-1个盒子:则第i个就放入剩下的m-(j-1)个盒子;

  状态转移公式为:dp[i][j]=dp[i-1][j]*f[j]+dp[i-1][j-1]*f[m-j+1];

    f[i]为i/m;

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define PI 3.141592653589792128462643383279502
double dp[][];
int i,j,k,n,m;
double f[];
int main(){
//#ifdef CDZSC_June
// freopen("in.txt","r",stdin);
//#endif
//std::ios::sync_with_stdio(false);
k=;
while(cin>>n){
if(n==-) break;
cin>>m;
memset(dp,,sizeof(dp));
for(i=;i<=m;i++) f[i]=(double)i/m;
dp[][]=;
for(i=;i<=n;i++)
for(j=;j<=m;j++){
dp[i][j]=dp[i-][j]*f[j]+dp[i-][j-]*f[m-j+];
}
printf("Case %d: %.7lf\n",++k,-dp[n][m]);
}
return ;
}

uva 10648(简单dp)的更多相关文章

  1. Uva 11078 简单dp

    题目链接:http://uva.onlinejudge.org/external/110/11078.pdf a[i] - a[j] 的最大值. 这个题目马毅问了我,O(n^2)超时,记忆化一下当前最 ...

  2. Partitioning by Palindromes UVA - 11584 简单dp

    题目:题目链接 思路:预处理出l到r为回文串的子串,然后如果j到i为回文串,dp[i] = min(dp[i], dp[j] + 1) AC代码: #include <iostream> ...

  3. UVA 111 简单DP 但是有坑

    题目传送门:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18201 其实是一道不算难的DP,但是搞了好久,才发现原来是题目没 ...

  4. UVA - 11584 划分字符串的回文串子串; 简单dp

    /** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...

  5. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  6. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  7. codeforces Gym 100500H A. Potion of Immortality 简单DP

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  8. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  9. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

随机推荐

  1. 51nod 1190 最小公倍数之和 V2

    给出2个数a, b,求LCM(a,b) + LCM(a+1,b) + .. + LCM(b,b). 例如:a = 1, b = 6,1,2,3,4,5,6 同6的最小公倍数分别为6,6,6,12,30 ...

  2. NGINX: 反向代理 Nexus

    Nginx 反向代理 nexus 的服务, 一直卡在 Initialize... 解决方式是添加一个 header X-Forwarded-Proto: proxy_set_header X-Forw ...

  3. POJ 2431 Expedition (优先队列+贪心)

    题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...

  4. for in、each; for 、forEach、map

    1.jQuery.each(object, [callback]) 用于例遍任何对象.回调函数拥有两个参数:第一个为对象的成员或数组的索引,第二个为对应变量或内容.如果需要退出 each 循环可使回调 ...

  5. hdu 1395 2^x mod n = 1(暴力题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1395 2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Oth ...

  6. CPU架构及并发编程基础(一)

    一.intel cpu发展计划tick-tock Tick-Tock是Intel发展微处理器芯片设计制造业务的一种战略模式.Intel指出,每一次处理器微架构的更新和每一次芯片制程的更新遵循“Tick ...

  7. Linux 入门记录:五、vi、vim 编辑器

    一.vi.vim编辑器 vi 是一个命令行界面下的文本编辑工具,最早在 1976 年由 Bill Joy 开发.当时名字叫做 ex.vi 支持绝大多数操作系统(最早在类 Unix 操作系统的 BSD上 ...

  8. 简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环)

    简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环) http://www.cnblogs.com/M18-BlankBox/p/5881700.html

  9. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance(xsi:schemaLocation详解)

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"中xsi的意思是 :本xml文件中要用到某些来自xsi代表的“http:/ ...

  10. 【bzoj4552】排序

    二分一个值,然后线段树上模拟. #include<bits/stdc++.h> #define lson (o<<1) #define rson (o<<1|1) ...