[HDU 3625]Examining the Rooms
题目
点这里看题目。
分析
先忽略第一个房间的限制。考虑炸开一个房间然后拿到里面的钥匙,然后再去打开新的房间,然后再拿到新的钥匙.....这样操作完,我们一定会经过最开始炸开的那个房间。
所以我们可以发现,这本质上也是一个将点分环的问题。所以就想到了斯特林数。此时的总的合理方案是
再考虑第一个房间的限制,我们发现,这个限制只会影响第一个房间单独成环的方案,所以需要减去对应的方案,也就是
概率就是
代码
#include <cstdio>
typedef long long LL;
const int MAXN = 25;
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' );
}
LL s[MAXN][MAXN];
int N, K;
void init()
{
s[0][0] = 1;
for( int i = 1 ; i <= 20 ; i ++ )
{
for( int j = 1 ; j <= i ; j ++ )
{
s[i][j] = s[i - 1][j - 1] + s[i - 1][j] * ( i - 1 );
}
}
}
int main()
{
init();
int T;
read( T );
while( T -- )
{
read( N ), read( K );
double res = 0;
for( int i = 1 ; i <= K ; i ++ )
{
res += s[N][i] - s[N - 1][i - 1];
}
for( int i = 1 ; i <= N ; i ++ ) res /= i;
printf( "%.4lf\n", res );
}
return 0;
}