Examining the Rooms

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1305    Accepted Submission(s): 796

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

 
Source
 

题解:

给出N个房间,每个房间的钥匙随机放在某个房间内,概率相同。有K次炸门的机会,求能进入所有房间的可能性为多大。

dp[i][j]代表i个房间形成j个环的总数;

则dp[i][j]=(i-1)*dp[i-1][j]+dp[i-1][j-1];

由于1号房间不能被砸,所以dp[i][j]-dp[i-1][j-1](减去1号房间被砸的总数)代表1号房间不被砸的总数,结果从1加到k除以总方案数(n的阶乘)即可;

代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
//S(P,K)=(P-1)*S(P-1,K)+S(P-1,K-1)
LL dp[][];
void db(){
for(int i=;i<=;i++){
dp[i][]=;
dp[i][i]=;
for(int j=;j<i;j++)
dp[i][j]=(i-)*dp[i-][j]+dp[i-][j-];
}
}
LL fac(int n){
LL temp=;
while(n>){
temp*=n;
n--;
}
return temp;
}
int main(){
int T,N,K;
mem(dp,);
db();
SI(T);
while(T--){
scanf("%d%d",&N,&K);
LL ans=;
for(int i=;i<=K;i++)ans+=dp[N][i]-dp[N-][i-];
printf("%.4lf\n",1.0*ans/fac(N));
}
return ;
}

Examining the Rooms(dp,斯特灵数)的更多相关文章

  1. Examining the Rooms - 第一类斯特灵数

    ---恢复内容开始--- 2017-08-10 20:32:37 writer:pprp 题意如下: Recently in Teddy's hometown there is a competiti ...

  2. cf932E. Team Work(第二类斯特灵数 组合数)

    题意 题目链接 Sol 这篇题解写的非常详细 首先要知道第二类斯特灵数的一个性质 \[m^n = \sum_{i = 0}^m C_{n}^i S(n, i) i!\] 证明可以考虑组合意义:\(m^ ...

  3. HDU 3625 Examining the Rooms【第一类斯特灵数】

    <题目链接> <转载于 >>> > 题目大意:有n个锁着的房间和对应n扇门的n把钥匙,每个房间内有一把钥匙.你可以破坏一扇门,取出其中的钥匙,然后用取出钥匙打 ...

  4. 斯特灵数 (Stirling数)

    @维基百科 在组合数学,Stirling数可指两类数,都是由18世纪数学家James Stirling提出的. 第一类 s(4,2)=11 第一类Stirling数是有正负的,其绝对值是个元素的项目分 ...

  5. HDU 3625 Examining the Rooms:第一类stirling数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3625 题意: 有n个房间,每个房间里放着一把钥匙,对应能开1到n号房间的门. 除了1号门,你可以踹开任 ...

  6. counting the buildings - 第一类斯特灵数

    2017-08-10 21:10:08 writer:pprp //TLE #include <iostream> #include <cstdio> #include < ...

  7. Rank - 第二类斯特灵数

    2017-08-10 20:32:37 writer:pprp 题意如下: Recently in Teddy's hometown there is a competition named &quo ...

  8. 斯特灵(Stirling)数

    http://zh.wikipedia.org/wiki/%E6%96%AF%E7%89%B9%E7%81%B5%E6%95%B0 第一类:n个元素分成k个非空循环排列(环)的方法总数 递推式:s(n ...

  9. HDU 3625 Examining the Rooms

    题目大意:有n个房间,n!个钥匙,在房间中,最多可以破k扇门,然后得到其中的钥匙,去开其它的门,但是第一扇门不可以破开,求可以打开所有门的概率. 题解:首先,建立这样的一个模型,题目相当于给出一个图, ...

随机推荐

  1. Dynamics CRM 2013 初体验(3):新增加的功能

    新系统除了修补系统历史漏洞外当然还会添加些比较有意思的新功能,至于这些新功能是否好用那就得看它是否能经过咱们这些使用者的考验了.Dynamics CRM 2013系统将不再支持Dynamics CRM ...

  2. C语言的本质(25)——C标准库之内存管理

    程序中需要动态分配一块内存时怎么办呢?我们可以定义一个缓冲区数组,但是这种方法不够灵活,C89要求定义的数组是固定长度的,而程序往往在运行时才知道要动态分配多大的内存,例如: void foo(cha ...

  3. 2014.8.15模拟赛【公主的工作】&&bzoj1046[HAOI2007]上升序列

    bzoj题目是这样的 Description 对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm ...

  4. DBS小结

    <数据库系统原理>主要介绍的是数据库技术的基本原理.方法和应用技术. 它可以使我们能有效地使用现有的数据库管理系统和软件开发工具,掌握数据库结构的设计和数据库应用系统的开发原理. 在这里, ...

  5. error LNK1104: 无法打开文件“libboost_thread-vc140-mt-gd-1_61.lib”

    error LNK1104: 无法打开文件“libboost_thread-vc140-mt-gd-1_61.lib” 调试->你的项目属性 配置属性->VC++目录 包含目录 D:\bo ...

  6. hdu4135Co-prime 容斥原理水题

    //问一个区间[a,b]与n互素的数的个数 //利用容斥原理可知 //在[a,b] 区间内对n的素数因子 //ans = 被一个数整除的数的个数 - 被两个数的最小公倍数整除的数的个数 + 被三个数的 ...

  7. springMVC3学习(四)--訪问静态文件如js,jpg,css

    假设你的DispatcherServlet拦截的是*.do这种URL.就不存在訪问不到静态资源的问题 假设你的DispatcherServlet拦截了"/"全部的请求,那同一时候对 ...

  8. 《Effective C++》:条款46-条款47

    条款46请输入转换的时候,需要定义非模板成员函数 条款47请使用traits class表现类型信息 条款46:须要类型转换时请为模板定义非成员函数 条款 24提到过为什么non-member函数才有 ...

  9. java 判断字符串编码

    String iso8859 = new String(sb.toString().getBytes("iso8859-1"));String gbk = new String(s ...

  10. Backup Exec Inventory 与Catalog的含义(转载)

    编录:即catalog,就是让磁带机读取磁带之前所备份过的内容的目录列表,可以让你知道之前做过什么备份,以及备份时间等详细信息. 列清单:inventory,跟编录是不同,inventory是查询磁带 ...