POJ 1322 Chocolate

Chocolate
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7652   Accepted: 2003   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? 

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. 

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 
题目大意:c种不同颜色的巧克力,每种巧克力同样多,把巧克力一个一个拿到桌子上,当发现有相同颜色就全吃掉,求取出了n个后,还剩m个在桌子的概率。
解题方法:动态规划,dp[i][j]表示当拿出i块巧克力时,桌子上剩余j块巧克力的概率,dp[i][j]由两种情况转移的到,第一种dp[i - 1][j - 1] / C * (double)(C - j + 1)表示第j块巧克力的颜色和桌子上的巧克力颜色不一样,第二种dp[i - 1][j + 1] * (double)(j + 1) / C表示桌子上本来有j + 1块,第j块和桌子上的某一块颜色相同,吃掉之后剩下j块,所以状态转移方程为:
dp[i][j] = dp[i - 1][j - 1] / C * (double)(C - j + 1) + dp[i - 1][j + 1] * (double)(j + 1) / C;当然还要考虑j=0和j=C的情况。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;

double dp[1005][105];

int main()
{
    int C, M, N;
    while(scanf("%d", &C) != EOF)
    {
        if (C == 0)
        {
            break;
        }
        scanf("%d%d", &N, &M);
        if(M > C || M > N || (M + N) & 1)  
        {  
            printf("0.000
");  
            continue;  
        }  
        if (N > 1001)
        {
            N = N % 2 + 1000;
        }
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1.0;
        for (int i = 1; i <= N; i++)
        {
            for (int j = 0; j <= C; j++)
            {
                if (j == 0)
                {
                    dp[i][j] = dp[i - 1][j + 1] / C;
                }
                else
                {
                    if (j == C)
                    {
                        dp[i][j] = dp[i - 1][j - 1] / C;
                    }
                    else
                    {
                        dp[i][j] = dp[i - 1][j - 1] / C * (double)(C - j + 1) + dp[i - 1][j + 1] * (double)(j + 1) / C;
                    }
                }
            }
        }
        printf("%.3f
", dp[N][M]);
    }
    return 0;
}



原文地址:https://www.cnblogs.com/lzmfywz/p/3208601.html