跳到正文

个人记录

[HDU 3625]Examining the Rooms

crashed约 3 分钟
原文

这是 2025年10月14日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年8月10日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

题目

        点这里看题目。

分析

        先忽略第一个房间的限制。考虑炸开一个房间然后拿到里面的钥匙,然后再去打开新的房间,然后再拿到新的钥匙…这样操作完,我们一定会经过最开始炸开的那个房间。
        所以我们可以发现,这本质上也是一个将点分环的问题。所以就想到了斯特林数。此时的总的合理方案是∑i=0ks(n,i)\sum_{i=0}^ks(n,i)。
        再考虑第一个房间的限制,我们发现,这个限制只会影响第一个房间单独成环的方案,所以需要减去对应的方案,也就是s(n−1,i−1)s(n-1,i-1)。所以此时的方案总数就是∑i=0ks(n,i)−s(n−1,i−1)\sum_{i=0}^ks(n,i)-s(n-1,i-1)。
        概率就是∑i=0ks(n,i)−s(n−1,i−1)n!\frac{\sum_{i=0}^ks(n,i)-s(n-1,i-1)}{n!}。

代码

C++
#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;
}