Chocolate
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 8743 | Accepted: 2291 | Special Judge |
Description
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?
"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.
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
Source
Beijing 2002
概率dp, 方程很好想到
dp[i][j] 表示拿了i次,桌子上有j块巧克力的概率
dp[i][j] =(1 - (j - 1) / c) * dp[i - 1][j - 1] + (j + 1) / c * dp[i - 1][j + 1];
数据范围太大,所以滚动数组用起来,然后注意循环顺序,所以要先循环i
这样去写是要TLE的,这题真的是让我心服口服啊,还能根据输出3位小数是近似解来压缩n,涨姿势了
概率dp, 方程很好想到
dp[i][j] 表示拿了i次,桌子上有j块巧克力的概率
dp[i][j] =(1 - (j - 1) / c) * dp[i - 1][j - 1] + (j + 1) / c * dp[i - 1][j + 1];
数据范围太大,所以滚动数组用起来,然后注意循环顺序,所以要先循环i
这样去写是要TLE的,这题真的是让我心服口服啊,还能根据输出3位小数是近似解来压缩n,涨姿势了
/*************************************************************************
> File Name: POJ1322.cpp
> Author: ALex
> Mail: 405045132@qq.com
> Created Time: 2014年12月30日 星期二 20时23分07秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
double dp[2][105];
int main()
{
int c, n, m;
while (~scanf("%d", &c))
{
if (c == 0)
{
break;
}
scanf("%d%d", &n, &m);
if (m > c || m > n)
{
printf("0.000\n");
continue;
}
if (n > 1001)
{
n = (n % 2) ? 1001 : 1000;
}
memset (dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j <= min(c, i); ++j)
{
dp[i % 2][j] = 0;
if (j > 0)
{
dp[i % 2][j] = ((c - j + 1) * 1.0 / c) * dp[1 - i % 2][j - 1];
}
if (j + 1 > i - 1)
{
continue;
}
dp[i % 2][j] += ((j + 1) * 1.0 / c) * dp[1 - i % 2][j + 1];
}
}
printf("%.3f\n", dp[n % 2][m]);
}
return 0;
}