[POJ 1322]Chocolate
题目
点这里看题目。
分析
显然,当
于是可以想到如下的
转移:
直接转移是
换一个方式,考虑取出糖果的序列本身。我们可以用合法序列数量除以总数量得到答案。总数量就是
序列本身需要考虑顺序,所以考虑使用指数型母函数。不难想到如下的式子:
左边一半是奇数部分,右边是偶数部分。接着化一下式子:
再把
我们只需要取
答案就是
再把答案的式子写出来:
用
代码
#include <cstdio>
const int MAXC = 105;
template<typename _T>
void read( _T &x )
{
x = 0;char s = getchar();int f = 1;
while( s > '9' || s < '0' ){if( s == '-' ) f = -1; s = getchar();}
while( s >= '0' && s <= '9' ){x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar();}
x *= f;
}
template<typename _T>
void write( _T x )
{
if( x < 0 ){ putchar( '-' ); x = ( ~ x ) + 1; }
if( 9 < x ){ write( x / 10 ); }
putchar( x % 10 + '0' );
}
double c[MAXC][MAXC];
int C, N, M;
double qkpow( double base, int indx )
{
double ret = 1;
while( indx )
{
if( indx & 1 ) ret *= base;
base *= base, indx >>= 1;
}
return ret;
}
int main()
{
c[0][0] = 1;
for( int i = 1 ; i <= 100 ; i ++ )
{
c[i][0] = c[i][i] = 1;
for( int j = 1 ; j < i ; j ++ ) c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
}
while( true )
{
read( C );
if( ! C ) break;
read( N ), read( M );
if( M > C || M > N || ( N + M ) & 1 ) { puts( "0.000" ); continue; }
double res = 0, p;
for( int i = 0 ; i <= M ; i ++ )
for( int j = 0 ; j <= C - M ; j ++ )
{
p = qkpow( 1.0 * ( C - 2 * i - 2 * j ) / C, N );
if( i & 1 ) res -= c[M][i] * c[C - M][j] * p;
else res += c[M][i] * c[C - M][j] * p;
}
printf( "%.3f\n", res * c[C][M] / qkpow( 2, C ) );
}
return 0;
}