uva 10648(简单dp)
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)的更多相关文章
- Uva 11078 简单dp
题目链接:http://uva.onlinejudge.org/external/110/11078.pdf a[i] - a[j] 的最大值. 这个题目马毅问了我,O(n^2)超时,记忆化一下当前最 ...
- Partitioning by Palindromes UVA - 11584 简单dp
题目:题目链接 思路:预处理出l到r为回文串的子串,然后如果j到i为回文串,dp[i] = min(dp[i], dp[j] + 1) AC代码: #include <iostream> ...
- UVA 111 简单DP 但是有坑
题目传送门:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18201 其实是一道不算难的DP,但是搞了好久,才发现原来是题目没 ...
- UVA - 11584 划分字符串的回文串子串; 简单dp
/** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- 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 ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
随机推荐
- 【hdu1828/poj1177】线段树求矩形周长并
题意如图 题解:这题非常类似与矩形面积并,也是维护一个被覆盖了一次以上的线段总长. 但是周长要算新出现的,所以每次都要和上一次做差求绝对值. x轴做一遍,y轴做一遍. 但是有个问题:矩形边界重合的时候 ...
- COGS1882 [国家集训队2011]单选错位
★ 输入文件:nt2011_exp.in 输出文件:nt2011_exp.out 简单对比时间限制:1 s 内存限制:512 MB [试题来源] 2011中国国家集训队命题答辩 [问题 ...
- Can you answer these queries?(HDU4027+势能线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 题目: 题意:n个数,每次区间更新将其数值变成它的根号倍(向下取整),区间查询数值和. 思路:易 ...
- C#利用WebClient 两种方式下载文件
WebClient client = new WebClient(); 第一种 string URLAddress = @"http://files.cnblogs.com/x4646/tr ...
- 【Python学习笔记】多版本python使用pip安装第三方库
不知道是不是有人跟我一样,一直Python2与Python3混着用,然而在cmd中默认的Python版本只有一种,使用 pip install xxx(第三方库名) 只会安装到默认版本上. 而如果需 ...
- dump_stack 实现分析【转】
转自:http://kernel.meizu.com/2017/03/18-40-19-dump_stack.html 1 简介 说起 dump_stack() ,相信从事 Linux 内核或者驱动相 ...
- linux非阻塞的socket EAGAIN的错误处理【转】
转自:http://blog.csdn.net/tianmohust/article/details/8691644 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux中使用非阻塞的s ...
- Development tools[重点]
Development tools yum groupinfo "Development tools" Loaded plugins: product-id, security, ...
- Centos7 安装
一.先把Centos7的镜像下载到本地 镜像下载网址:http://archive.kernel.org/centos-vault/ (里面有任何需要的版本) 二.启动VMware 1. 创建新的虚拟 ...
- Android UI 设计:pixel dip dpi sp density
1. px (pixels)像素 – 是像素,就是屏幕上实际的像素点单位. dip或dp (device independent pixels)设备独立像素,与设备屏幕有关. sp (scaled p ...