24h購物| | PChome| 登入
2013-06-09 21:22:18| 人氣1,102| 回應0 | 上一篇 | 下一篇

[UVA][math] 10648 - Chocolate Box

推薦 0 收藏 0 轉貼0 訂閱站台

Chocolate Box

Input: standard input
Output: standard output
Time Limit: 1 second


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

 


Problem Setter: A. K. M. Saifun Nabi (Shabuj) ( BUET PESSIMISTIC )
Thanks to Anupam Bhattacharjee for his alternate solution and data.

dp[i][j] 表示成目前放入第 i 個巧克力,且已經有 j 個箱子被使用。
則有 j/m 的機率維持一樣(仍在原本的 j 個箱子中挑),
又或者會有 (m-j)/m 的機率會增加一個箱子(挑剩餘的箱子)。


#include <stdio.h>

int main() {
int n, m, cases = 0;
while(scanf("%d %d", &n, &m) == 2) {
double dp[105][105] = {};
int i, j;
dp[0][0] = 1;
for(i = 0; i <= n; i++) {
for(j = 0; j <= m; j++) {
dp[i+1][j] += (dp[i][j] * (j))/m;
dp[i+1][j+1] += (dp[i][j] * (m-j))/m;
}
}
printf("Case %d: %.7lf\n", ++cases, 1 - dp[n][m]);
}
return 0;
}

台長: Morris
人氣(1,102) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 159 - Word Crosses
此分類上一篇:[UVA][math][JAVA] 10643 - Facing Problem With Trees

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文