POJ1322Chocolate-- DP Introduction

Title here

Remove each piece of chocolate from its packaging and place it on the table. If there are two identical color of chocolate on the table, will both lose.
If there are C colors chocolate package (color uniform distribution), N removed from the package after the chocolate does have M chocolate on the table what is the probability?
In each case, there are three non-negative integers: C (C <= 100), N and M (N, M <= 1000000). 

The subject of the request taken chocolate n, m chocolate remaining probability table. That means we will be pressing topic, dp [i] [j] is the meaning of this
First, determine the boundary conditions, if the chocolate out 0, then the remaining chocolate on the table 0 what is the probability? ? ? Very simple, dp [0] [0] = 1;
Further, for the determination of illegal input c, n, m, i.e., the probability of a direct output of 0.000 like
 
m is a number less than or equal to c, because if the chocolate is a color number of greater than or equal to 2, then both must be taken away, that is, each of the last remaining chocolate is either no or only one. So most all the colors are on the table, it is a .
After dp [i] [j] denotes the i-th former operation (i.e. removed chocolate i), j chocolate occurrence probability table. Just think, if i + j is odd what would happen?
dp [i] [j] is equal to 0 (in the case of unlikely). Why is it impossible, since each time the ball is now on the table will, and if there is no repetition of color, the number of +1 ball on the table, if there is repetition, repetition of the two balls are removed, that is, the number i is first applied to m, m is either the same moment, or -2, does not appear odd. Therefore dp [i] [j] i + j is an odd number in the probability 0
You can simulate the manual checking.
 
So, how to do the state transition equation? ? As to what color to get the ball and the ball on the table will not be repeated, i.e. dp [i-1] [j -1] * (c-j + 1.0) / c after is out in front of the chocolate i-1; , the j 1-chocolate probability tables remaining, multiplied by the table with the chocolate out of chocolate color does not repeat probability, c-J + 1.0, the total number of colors to represent different colors subtract the table, but also the rest of the different colors, divided by c is the probability of different colors corresponding to the extraction .
Either the same extracted color table and a ball of the ball, to take away with, so the equation is: DP [. 1-I] [J +. 1] * (J + 1.0) / C , + J. 1 / C, i.e. the same as the color of the ball and a color table out of the ball
dp [i] [j] will add to both
 
Further, when the great n is calculated, it can be regarded as a small n, because the probability of a large and a small n corresponding to the number m of the decimal point only after quite a few will be different, it is possible down conversion
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 using namespace std;
 5 #define MAX 105 
 6 int main()
 7 {
 8     int c = 0, n = 0, m = 0;
 9     double dp[MAX * 10][MAX];
10     while (scanf("%d", &c) != EOF)
11     {
12         if (c == 0)
13         {
14             break;
15         }
16         scanf("%d %d", &n, &m);
17         if (m > c || m > n || (m + n) % 2 != 0)//特判
18         {
19             printf("0.000\n");
20             continue;
21         }
22         memset(dp, 0, sizeof(dp));
23         if (n > 1000)//The larger n conversion smaller 
24          {
 25              n = 1000 + n% 2 ; // Parity selection 
26 is          }
 27          DP [ 0 ] [ 0 ] = . 1 ;
 28          for ( int I = . 1 ; I <= n; ++ I)
 29          {
 30              for ( int J = 0 ; J <= C; ++ J)
 31 is              {
 32                  IF ((I + J)% 2 ! = 0 )
 33 is                 {
34                     continue;
35                 }
36                 dp[i][j] = dp[i - 1][j - 1] * (c - j + 1.0) / c + dp[i - 1][j + 1] * (j + 1.0) / c;
37             }
38         }
39         printf("%.3lf\n", dp[n][m]);
40     }
41     return 0;
42 }

 

 

Guess you like

Origin www.cnblogs.com/ygsworld/p/11329954.html