Chocolate
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Special Judge
Problem 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-j)/c+dp[i-1][j+1]*j/c;
由递推知,后一个状态只与前一个状态有关,所以可以将其变成滚动数组
dp[i%2][j]=dp[1-i%2][j-1]*(c-j)/c+dp[1-i%2][j+1]*j/c;
极大的减少了空间。由原来需要开出的dp[N][N]–>dp[2][N];
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
int c,n,m;
double dp[5][110];///表示取出i块,还剩j块的概率
while(scanf("%d",&c)!=EOF&&c)
{
scanf("%d %d",&n,&m);
memset(dp,0,sizeof(dp));
if(m>n||m>c||(m+n)%2)
{
printf("0.000\n");
continue;
}
if(n>1000)n=1000+n%2;
dp[0][0]=1.0;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=i&&j<=c;j++)
{
dp[i%2][j]=0.0;
if((i+j)%2) continue;
if(j>0)
dp[i%2][j]+=dp[1-i%2][j-1]*((c-j+1.0)*1.0/c);///滚动数组压缩数据,取出来的糖
if(j+1<=i-1)
dp[i%2][j]+=dp[1-i%2][j+1]*((j+1.0)*1.0/c);
}
}
printf("%.3f\n",dp[n%2][m]);
}
return 0;
}