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. php5.5以上的版本 开启curl

    对于php5.5以上的版本开启方法,需要libeay32.dll.ssleay32.dll.libssh2.dll三个文件拷备到C:\Windows目录下,php.ini中 扩展开启,重启apache ...

  2. Android Studido下的应用性能优化总结--布局优化

    前言:一个应用的成功=产品设计*性能 ,再此我们不讨论一个应用的设计,那交给我们可爱又可恨的产品经理和UI设计师来决定!所以这里分步骤讨论如何提升一个应用的性能,这里先探讨布局优化问题. 布局优化 避 ...

  3. JVM 看不到某些异常的stacktrace问题(转)

    在java 1.5的release notes里面可以看到这样一句话: The compiler in the server VM now provides correct stack backtra ...

  4. 转:Memcached常用命令及使用说明

    一.存储命令 存储命令的格式: 1 2 <command name> <key> <flags> <exptime> <bytes> < ...

  5. accel-pptp 部署

    accel-pptp 是 pptp-client 和 pptpd 的改进版,使用内核 pptp 模块,相比 raw socket 实现方式能提供更好的性能.   Ubuntu 12.04 上启用内核 ...

  6. 【DSA MOOC】起泡排序的原理及常数优化

    根据学堂在线TsinghuaX: 30240184X 数据结构(2015秋)这门课的内容,对bubblesort做了一些总结. 1. bubblesort(起泡排序),原理来自这样一个观察规律:若序列 ...

  7. 杭电oj 2719

    Tips:本程序没有什么难度,只要按照逻辑进行替换即可,需要注意的是,由于输入串中含有空格符号,所以不能使用scanf("%s",ch);来读取一串,可以使用gets()函数读取一 ...

  8. js实现车轮的来回滚动

    最近喜欢用js做车轮的来回滚动,简单的js动画分享给大家.有什么建议记得说出来大家一起讨论哦!效果图如下: 源代码: <style> #pic1{ width:20px; height:2 ...

  9. Vbox 未指定XXX网络名称 找不到网卡问题

    链接方式不止Host-Only 我一般选桥连 选择 VBoxNetFltM.inf VBoxNetFltM.inf VirtualBox的桥接网络驱动程序的INF文件(Miniport:端口) VBo ...

  10. 具体解释HTML中的window对象和document对象

    Window -- 代表浏览器中一个打开的窗体: 对象属性 window //窗体自身 window.self //引用本窗户window=window.self window.name //为窗体命 ...