Examining the Rooms(dp,斯特灵数)
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
3 1
3 2
4 2
0.6667
0.6250
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次炸门的机会,求能进入所有房间的可能性为多大。
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,斯特灵数)的更多相关文章
- Examining the Rooms - 第一类斯特灵数
---恢复内容开始--- 2017-08-10 20:32:37 writer:pprp 题意如下: Recently in Teddy's hometown there is a competiti ...
- cf932E. Team Work(第二类斯特灵数 组合数)
题意 题目链接 Sol 这篇题解写的非常详细 首先要知道第二类斯特灵数的一个性质 \[m^n = \sum_{i = 0}^m C_{n}^i S(n, i) i!\] 证明可以考虑组合意义:\(m^ ...
- HDU 3625 Examining the Rooms【第一类斯特灵数】
<题目链接> <转载于 >>> > 题目大意:有n个锁着的房间和对应n扇门的n把钥匙,每个房间内有一把钥匙.你可以破坏一扇门,取出其中的钥匙,然后用取出钥匙打 ...
- 斯特灵数 (Stirling数)
@维基百科 在组合数学,Stirling数可指两类数,都是由18世纪数学家James Stirling提出的. 第一类 s(4,2)=11 第一类Stirling数是有正负的,其绝对值是个元素的项目分 ...
- HDU 3625 Examining the Rooms:第一类stirling数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3625 题意: 有n个房间,每个房间里放着一把钥匙,对应能开1到n号房间的门. 除了1号门,你可以踹开任 ...
- counting the buildings - 第一类斯特灵数
2017-08-10 21:10:08 writer:pprp //TLE #include <iostream> #include <cstdio> #include < ...
- Rank - 第二类斯特灵数
2017-08-10 20:32:37 writer:pprp 题意如下: Recently in Teddy's hometown there is a competition named &quo ...
- 斯特灵(Stirling)数
http://zh.wikipedia.org/wiki/%E6%96%AF%E7%89%B9%E7%81%B5%E6%95%B0 第一类:n个元素分成k个非空循环排列(环)的方法总数 递推式:s(n ...
- HDU 3625 Examining the Rooms
题目大意:有n个房间,n!个钥匙,在房间中,最多可以破k扇门,然后得到其中的钥匙,去开其它的门,但是第一扇门不可以破开,求可以打开所有门的概率. 题解:首先,建立这样的一个模型,题目相当于给出一个图, ...
随机推荐
- ZigBee技术简介
Zigbee的由来 在蓝牙技术的使用过程中,人们发现蓝牙技术尽管有许多优点,但仍存在许多缺陷.对工业,家庭自动化控制和遥测遥控领域而言,蓝牙技术显得太复杂,功耗大,距离近,组网规模太小等,……而工业自 ...
- Windows服务安装完成后自动启动
public ServiceInstaller() { //... Installer code here this.AfterInstall += new InstallEventHandler(S ...
- Python中:self和__init__的含义 + 为何要有self和__init__
Python中:self和__init__的含义 + 为何要有self和__init__ 背景 回复: 我写的一些Python教程,需要的可以看看 中SongShouJiong的提问: Python中 ...
- C语言的本质(27)——C语言与汇编之计算机结构
现代计算机都是基于冯·诺依曼或哈佛体系结构的,不管是嵌入式系统.个人电脑还是服务器.这种两种体系结构的主要特点是:CPU和内存是计算机的两个主要组成部分,内存中保存着数据和指令,CPU从内存中取指令执 ...
- JS页面赋值
<html> <title>页面1</title> <div> <ul> <li> <div class=" ...
- H264源码分析(二)
原文出自http://blog.csdn.net/xfding/article/details/5476763(转载收集) (四)图像参数集语义 pic_parameter_set_rbsp( ) { ...
- POJ 1734 求最小环路径 拓展Floyd
九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/11888019 题意: n个点 m条无向边 下面m条有权无向边 问图中最小环的路径 ...
- L10 PUtty+SSH 访问vncviewer
在Linux下配置一个VNC服务器,并设置2个用户,要求其中一个用户登录时不需要输入密码.然后在客户端使用ssh+vncview的方式访问. 安装tigervnc: 输入的密码是123456 连接服务 ...
- Tera Term——访问linux的ssh工具
个人感觉他是一款免费的并且比较好用的ssh工具,下载地址: http://logmett.com/index.php?/products/teraterm.html 下载之后一路下一步就可以了.可以选 ...
- .Net 发邮件
对于.NET而言,从2.0开始,发邮件已经是一件非常easy 的事了.下面我给出一个用C#群发邮件的实例,做了比较详细的注解,希望对有需要的朋友有所help. // 引入命名空间using Syste ...