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. 【游记】GDOI 2017

    Day 0 学校好大>_<! 酒店好高级>_<! 晚上复习代码的时候很困QAQ,中间和hwh面基.复习到SA的时候因为太久没看忘记板子的意思了又背不下来,于是不看了,相信考了也 ...

  2. python数据处理课程笔记(一)

    一.numpy 1.numpy中所有元素必须是相同的类型 a=[1,2,3,4,'t'] #列表中有str类型,转换为ndarray时所有元素都转换为str类型 arr1=np.array(a) pr ...

  3. HDU 4757 可持久化trie树

    首先如果给定一些数,询问这些数中哪个数^给定的数的值最大的话,我们可以建立一颗trie树,根连接的两条边分别为0,1,表示二进制下第15位,那么我们可以建立一颗trie树,每一条从根到叶子节点的链表示 ...

  4. js 的function为什么可以添加属性

    (1) function person(){ this.name = 'Tom'; } (2) function person(){} person.name = 'Tom'; (3) functio ...

  5. js函数定义方法

    1.函数声明 其语法为 function functionName(){ //函数体 } 首先是function关键字,然后是函数名,其重要特征是函数声明提升,即在执行代码之前会先读取函数声明,使其在 ...

  6. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

  7. Linux NAPI/非NAPI

    本文主要介绍二层收包流程,包括NAPI与非NAPI方式: NAPI:数据包到来,第一个数据包产生硬件中断,中断处理程序将设备的napi_struct结构挂在当前cpu的待收包设备链表softnet_d ...

  8. VO、DTO、DO、PO的概念、区别和用处

    转至:http://qixuejia.cnblogs.com/ 本篇文章主要讨论一下我们经常会用到的一些对象:VO.DTO.DO和PO. 由于不同的项目和开发人员有不同的命名习惯,这里我首先对上述的概 ...

  9. 微信小程序宽高适配

    小程序的宽任何机型都是750rpx,但是画布canvas的默认单位是px,可能会出现需要怪异的样式,我们可以用到 wx.getSystemInfoSync().windowWidth和 wx.getS ...

  10. java - 线程1打印1-10,当线程打印到5后,线程2打印“hello”,然后线程1继续打印

    public class T { private static int a =1;//1代表线程1 2线程2 public static void main(String[] args) { fina ...