HDU 3625 Examining the Rooms

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.

参考:http://blog.csdn.net/queuelovestack/article/details/47970827

钥匙和门的关系是类似环状的,打开一个门之后,该环内的所有房间都可以进入,怎么说呢,就拿Hint里的#6来举例,Room1 Room2 Room3是在一个环当中的,假设我破坏了Room3,那么我取出Room3内的钥匙Key2就可以打开Room2,而Room2里有钥匙Key1,那我们又可以打开Room1。

第一类斯特林数可以求解把包含n个元素的集合分成k个环排列的方法数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <bitset>
using namespace std;
typedef long long LL;
#define MOD 1000000007
LL fac[25],s[25][25],n,k,T;
int main(){
  // freopen("test.in","r",stdin);
  cin >> T;
  fac[1] = 1; s[0][0] = 0; s[1][1] = 1; s[1][0] = 0;
  for (int i=2;i<=21;i++){
    fac[i] = fac[i-1] * i;
    s[i][0] = 0;
    for (int j=1;j<=i;j++){
      s[i][j] = s[i-1][j-1] + (i-1) * s[i-1][j];
    }
  }
  for (int i=1;i<=T;i++){
    cin >> n >> k;
    LL ans = 0;
    for (int j=1;j<=k;j++){
      ans += s[n][j] - s[n-1][j-1];
    }
    double anss = ans * 1.0;
    printf("%.4lf\n",anss/fac[n]);
  }
  return 0;
}

时间: 2024-11-09 10:43:30

HDU 3625 Examining the Rooms的相关文章

HDU 3625 Examining the Rooms:第一类stirling数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3625 题意: 有n个房间,每个房间里放着一把钥匙,对应能开1到n号房间的门. 除了1号门,你可以踹开任意一扇门(不用钥匙),但你最多只能踹k次. 问你能将所有门打开的概率. 题解: · P(打开所有门) = 能打开所有门的钥匙放置情况数 / 钥匙放置的总情况数 · 钥匙放置的总情况数 = n! 那么考虑下能打开所有门的钥匙放置情况数... 由于每个房间里有且只有一把钥匙,所以如果将每个房间连向房间内

hdu 3625 Examining the Rooms —— 第一类斯特林数

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3625 学习斯特林数:https://blog.csdn.net/qq_33229466/article/details/75042895 https://www.cnblogs.com/gzy-cjoier/p/8426987.html http://www.cnblogs.com/zhouzhendong/p/Stirling-Number.html 关于这道题: 得到一把钥匙后,可以连续开的门与钥匙

HDU 3625 Examining the Rooms 第一类斯特林数

最多可以暴力打开k次 对于一个环暴力一次 n个数排成i个(i<=k)非空环排列的种数 另外第一扇门不能暴力打开 所以1不能独立成环 #include <cstdio> #include <cstring> using namespace std; typedef __int64 LL; LL dp[22][22]; LL f[22]; int main() { dp[0][0] = 1; f[0] = 1; for(int i = 1; i <= 20; i++) {

HDU 3625 Examining the Rooms(第一类斯特林数)

题意:给你n扇门,然后可以开k次门,每次们后都有一把钥匙,不能开1号门,问最后打开一号门的概率是多少 思路:看大家说是裸的第一类斯特林数,我觉得有篇博客写的很好(传送门),这里采取的是博客里的第二种思路,感觉这种如果想到的话更容易理解,但是并没有遇到博客里说的g++会wa的情况 代码: #include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=55; LL s1[maxn][maxn];

Examining the Rooms HDU - 3625(第一类斯特林数)

Examining the Rooms HDU - 3625 题意:n个房间,每个房间里有一把钥匙(等概率),每进到一个房间可以得到钥匙去该钥匙对应的房间,如果当前没有钥匙则可以破门而入(1号房间不能破门而入),不过最多破门而入k次,问成功进入n个房间的总概率. 明显是求n个元素的i个环排列,i = 1,2,--,k. 不过要减去第一个房间自环的情况. 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long lon

hdu 3625 第一类斯特林数

题目链接:click here Examining the Rooms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1146    Accepted Submission(s): 689 Problem Description A murder happened in the hotel. As the best detective

hdu 3625 第一类striling 数

1 /** 2 第一类Stirling数是有正负的,其绝对值是包含n个元素的集合分作k个环排列的方法数目. 3 递推公式为, 4 S(n,0) = 0, S(1,1) = 1. 5 S(n+1,k) = S(n,k-1) + nS(n,k). 6 7 大意: 有n个房间,n把钥匙,钥匙在房间中,问: 在最多破坏k个门的情况下,问有多少种方法,可以将所有的门打开,注意,不能破坏第一个门 8 9 思路: 即是将n个元素分成m个环,得排列方式..除掉第一个元素独立成环的方式 10 可以得出,这是第一类

HDU3265 Examining the Rooms【stirling数】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3625 题目大意: 有N个房间,每个房间的要是随机放在某个房间内,概率相同.有K次炸门的机会. 求能打开所有房间门,进入所有房间的概率有多大. 解题思路: 门和钥匙的对应关系出现环.打开一个门后,环内的门都可以打开.也就意味着: N个房间的钥匙与门形成1~K个环的概率有多大. 也就是求N个元素,构成K个以内的环,且1不成自环的概率. N个元素形成K个环的方法数是第一类stirling数 S(N,K)

Examining the Rooms - 第一类斯特灵数

---恢复内容开始--- 2017-08-10 20:32:37 writer:pprp 题意如下: Recently in Teddy's hometown there is a competition named "Cow Year Blow Cow".N competitors had took part in this competition.The competition was so intense that the rank was changing and changi