poj 1322 chocolate (指数型母函数 )

http://poj.org/problem?id=1322 题意:

思路:别看n,m很大。。。但是想一下。。m显然不可能大于c(如果大于c,那么根据抽屉原理,至少存在一种巧克力大于一个,然而大于一个就会被取走…矛盾)   这样概率为0.m也不可能大于n,因为最好的情况就是取出的巧克力都放在了桌子上,如果总共取的还不到n个,又怎么可能剩下m(m>n)个呢。此外,还需要n,m奇偶性相同,否则设n-m=2K+1 ,说明如果要剩余m个,那么就要减少2k+1个,但是巧克力是两个两个减少的,减少的个数一定是偶数,因此矛盾。所以n,m奇偶性相同。

接下来可以用概率dp做,由于n比较大,滚动一下应该可以… 然后看到别人的题解里写到当n>1000的时候已经趋向平衡(达到了要求的精度)… 这道题dp写起来的确容易,也不是很难想。

不过作为dp废宁愿选择数学方法,指数型母函数。

分析可知,取过偶数次的巧克力消失,只有取过奇数次的巧克力会留在桌子上。

那么要剩余m个巧克力,也就是有m种巧克力取了奇数次,剩下的c-m种巧克力取了偶数次。

对应的的生成函数(母函数)分别是(e^x-e(-x))/2和(e^x+e(-x))/2  (推倒类似)hdu2065红色病毒解题报告

总事件个数为c^n

根据古典概型,所求概率为 (Gn*n!C[c][m])/(c^n)  其中Gnn!为生成函数,C[c][m]是因为不确定c种巧克力中的哪m种取了奇数个。

现在的问题就成了求Gn中x^n的系数。。我就是因为这个卡了两天这道题。。。

其实模拟就好,复杂度O(c^2)而已。。主要是好久没写二项式定理。。。有点忘了(手动智力-2)

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年03月02日 星期三 18时57分58秒
  4File Name :code/poj/1322.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=105;
 34double c[N][N];
 35int C,n,m;
 36
 37
 38
 39void pre()
 40{
 41    c[0][0] = 1;
 42    for ( int i = 1 ; i <  N ; i++)
 43    {
 44	c[i][0]=c[i][i] = 1;
 45	for ( int j = 1 ; j < i ; j++) c[i][j]=c[i-1][j-1]+c[i-1][j];
 46    }
 47}
 48double ksm( double a,int b)
 49{
 50    double res = 1.0;
 51    while (b)
 52    {
 53	if (b&1) res = (res*a);
 54	b = b>>1;
 55	a = a*a;
 56    }
 57    return res;
 58}
 59void solve()
 60{
 61    double posi[N],nega[N],a[N],b[N];
 62
 63    ms(posi,0);
 64    ms(nega,0);
 65    ms(a,0);
 66    ms(b,0);
 67
 68    double tmp1=ksm(0.5,m);
 69    double tmp2;
 70    for ( int i = 0 ; i <= m ; i++)
 71    {
 72	int j = i-(m-i);
 73	if ((m-i)&1) tmp2 = -tmp1;
 74	    else tmp2 = tmp1 ;   //正负项
 75	if (j>=0) a[j]+=tmp2*c[m][i];
 76	    else b[-j] +=tmp2*c[m][i];
 77    }
 78
 79    tmp1 = ksm(0.5,C-m);
 80    for ( int i = 0 ; i <= C-m ; i++)
 81    {
 82	tmp2 = tmp1 *c[C-m][i];
 83	for (int j = 0 ; j <= m ; j++)
 84	{
 85	    int k = j+i-(C-m-i);
 86	    if (k>=0) posi[k]+=tmp2*a[j];
 87	    else nega[-k]+=tmp2*a[j];
 88
 89	}
 90
 91	for ( int j = 0 ; j <= m ; j ++)
 92	{
 93	    int k = -j+i-(C-m-i);
 94	    if (k>=0) posi[k]+=tmp2*b[j];
 95	    else nega[-k]+=tmp2*b[j];
 96
 97	}
 98    }
 99
100    double ans =  0.0;
101    for ( int i = 1 ; i <= C ; i++)
102    {
103	if (n&1) nega[i]=-nega[i];
104	ans +=c[C][m]*ksm(1.0*i/(1.0*C),n)*(posi[i]+nega[i]);
105//	cout<<c[C][m]<<" "<<ksm(1.0*i/C,n)<<" "<<posi[i]+nega[i]<<endl;
106    }
107
108    printf("%.3f\n",ans);
109
110
111}
112int main()
113{
114	#ifndef  ONLINE_JUDGE 
115	freopen("code/in.txt","r",stdin);
116  #endif
117
118	pre();
119	while (~scanf("%d",&C))
120	{
121	    if (C==0) break;
122	    scanf("%d %d",&n,&m);
123	    if ((n-m)%2||m>C||m>n)
124	    {
125		puts("0.000");
126		continue;
127	    }
128	    if (n==0&&m==0)
129	    {
130		puts("1.000");
131		continue;
132	    }
133	    solve();
134
135	}
136
137  #ifndef ONLINE_JUDGE  
138  fclose(stdin);
139  #endif
140    return 0;
141}