HDU - 3625 Examining the Rooms (第一类Stirling数)

Problem Description
A murder happened in the hotel. As the best detective in the town, you should examine all the N rooms of the hotel immediately. However, all the doors of the rooms are locked, and the keys are just locked in the rooms, what a trap! You know that there is exactly one key in each room, and all the possible distributions are of equal possibility. For example, if N = 3, there are 6 possible distributions, the possibility of each is 1/6. For convenience, we number the rooms from 1 to N, and the key for Room 1 is numbered Key 1, the key for Room 2 is Key 2, etc.
To examine all the rooms, you have to destroy some doors by force. But you don’t want to destroy too many, so you take the following strategy: At first, you have no keys in hand, so you randomly destroy a locked door, get into the room, examine it and fetch the key in it. Then maybe you can open another room with the new key, examine it and get the second key. Repeat this until you can’t open any new rooms. If there are still rooms un-examined, you have to randomly pick another unopened door to destroy by force, then repeat the procedure above, until all the rooms are examined.
Now you are only allowed to destroy at most K doors by force. What’s more, there lives a Very Important Person in Room 1. You are not allowed to destroy the doors of Room 1, that is, the only way to examine Room 1 is opening it with the corresponding key. You want to know what is the possibility of that you can examine all the rooms finally.

Input
The first line of the input contains an integer T (T ≤ 200), indicating the number of test cases. Then T cases follow. Each case contains a line with two numbers N and K. (1 < N ≤ 20, 1 ≤ K < N)

Output
Output one line for each case, indicating the corresponding possibility. Four digits after decimal point are preserved by rounding.

Sample Input
3
3 1
3 2
4 2

Sample Output
0.3333
0.6667
0.6250
Hint
Sample Explanation

When N = 3, there are 6 possible distributions of keys:

Room 1 Room 2 Room 3 Destroy Times
1 Key 1 Key 2 Key 3 Impossible
2 Key 1 Key 3 Key 2 Impossible
3 Key 2 Key 1 Key 3 Two
4 Key 3 Key 2 Key 1 Two
5 Key 2 Key 3 Key 1 One
6 Key 3 Key 1 Key 2 One

In the first two distributions, because Key 1 is locked in Room 1 itself and you can’t destroy Room 1, it is impossible to open Room 1.
In the third and forth distributions, you have to destroy Room 2 and 3 both. In the last two distributions, you only need to destroy one of Room 2 or Room

大致题意:有N个房间,每个房间里有一把钥匙,钥匙随机分配。如果手中有对应的钥匙,就可以开门,如果没有钥匙就只能选择破门而入拿钥匙,第一个房间不允许破门,给定最多破门次数K,求能进入所有房间的概率

思路:易知总共有N!种门中钥匙放置的情况。假如所有的门都可以破,那么能进入所有房间所需满足的条件为N个房间所构成的环排列的个数不能超过k,但题目中要求第一个房间的门不能破,所以如果第一个房间的钥匙就放在第一个房间里(即第一个房间自己构成一个环),此时无解,所以我们还需将无解的情况数减去。假如N个房间构成i个环,那么其中无解的情况数是N-1个房间构成了i-1个环(此时第一个房间自己构成一个环)。
所以答案即为:
这里写图片描述
S1(n,i)表示第一类Stirling数(即n个数构成i个环排列的情况数)。

代码如下

#include <cstdio>  
#include <cstring>  
#include <algorithm> 
#include <iostream> 
using namespace std;  
#define ll long long   

using namespace std;
ll stir[25][25];
ll f[25];
void init(){
    memset(stir,0,sizeof(stir));
    stir[0][0]=0;
    stir[1][1]=1;
    for(int i=1;i<=20;i++)
    for(int j=1;j<=i;j++)
    {
        if(i==j)
        stir[i][j]=1;
        else 
        stir[i][j]=stir[i-1][j-1]+stir[i-1][j]*(i-1);
    }
    f[1]=1;
    for(int i=2;i<=20;i++)
    f[i]=f[i-1]*i;

}
int main ()  
{    
    init();

    int T;
    cin>>T;
    while(T--)
    {
        int n,k;
        cin>>n>>k;
        ll sum=0;
        for(int i=1;i<=k;i++)
        sum+=stir[n][i]-stir[n-1][i-1];
        printf("%.4lf\n",sum/(1.0*f[n]));
    }
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值