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. C# 几种常见的排序方法

    1.冒泡排序 //冒泡排序 public void BubbleSort(int[] list) { int i, j, temp; bool done = false; j = ; while (( ...

  2. Inno Setup 创建站点,创建虚拟目录

    原文 http://hi.baidu.com/0531_sunmiles/item/ce22554ab7d33d0be9350477 下面的这段代码是用Inno Setup 做安装包的时候创建IIS新 ...

  3. 蜂鸟A20开发板刷 cubietruck 的 SD 卡固件

    美睿视讯 为蜂鸟A20准备的 MerriiLinux 功能非常简陋.所以能用上主流的 debian 或者 LUbuntu 就可以说是非常迫切的需求了.蜂鸟A20(Merrii Hummingbird ...

  4. poj 2632 Crashing Robots_模拟

    做的差点想吐,调来调去,编译器都犯毛病,wlgq,幸好1a. 题意:给你机器人怎走的路线,碰撞就输出 #include <cstdlib> #include <iostream> ...

  5. hdu 5620 KK's Steel(推理)

    Problem Description Our lovely KK has a difficult mathematical problem:he has a N(1≤N≤1018) meters s ...

  6. hdu 5606 tree(并查集)

    Problem Description There is a tree(the tree is a connected graph which contains n points and n−1 ed ...

  7. 安装android studio时,解决unable to access android sdk add-on list

    1.打开时提示如下错误. 2.在安装路径下找到idea.properties文件,用记事本打开,添加disable.android.first.run=true,保存即可. 3.再次打开Android ...

  8. JS属性

    1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型. 代码: var a1;var a2 = tr ...

  9. Hadoop权威指南学习笔记二

    MapReduce简单介绍 声明:本文是本人基于Hadoop权威指南学习的一些个人理解和笔记,仅供学习參考,有什么不到之处还望指出,一起学习一起进步. 转载请注明:http://blog.csdn.n ...

  10. Eclipse中更改默认java代码格式【转】

    在写代码时常常有2种代码样式习惯,如下图.由于一直习惯了第一种代码格式,而看第二种代码格式时感觉代码很乱,总找不到“{ }”对称的感觉.Eclipse自动格式化代码的快捷方式是Ctrl+Shift+F ...