题目

有\(n\)个房间,\(n\)个钥匙,每个钥匙随机出现在一个房间里,一个房间里有且仅有一个钥匙。我们现在手上没有钥匙,但我们要搜索所有的房间,所以我们有\(k\)次机会把一个房间炸开。一号房间里住着一个重要的人,所以一号房间不能炸。给出\(n\),\(k\),求我们能够成功搜索所有房间的概率。

分析

英文题看了一会儿……

我们发现,对于一个由房间和钥匙组成的环,我们只需要随便炸开其中一个房间即可搜索这个环上所有的房间。所以成功的情况就是环的个数小于等于\(k\)并且一号钥匙不在一号房里。

我自己推出的公式比较复杂,基本思路就是,首先求出不管1号房间的可能情况,再减去\(\frac{1}{n}\),即减去一号钥匙在一号房间里的情况,再加上一号钥匙在一号房间且不可行的情况,一起除以\(n!\)即可:

\[\begin{aligned}
ans=\frac{\sum _{i=1}^k s(n,i)}{n!}-\frac{1}{n}+\frac{\sum _{i=k+1}^n s(n-1,i-1)}{n!}
\end{aligned}
\]

然而hjh告诉我有更简洁的方法:

\[\begin{aligned}
ans=\frac{\sum _{i=1}^k s(n,i)-s(n-1,i-1)}{n!}
\end{aligned}
\]

成功的情况数就是\(n\)个房间中的环小于等于\(k\)个的情况数减去后\(n-1\)个房间刚好组成\(k-1\)个环(即1号房间自己组成一个环)的情况数。

其实这两个式子在数学上应该是等价的,但这种在枚举的时候计算的思想很棒。

代码

hdu上g++ WA了,c++可以AC。

#include<cstdio>
#include<algorithm>
#include<cctype>
#include<cstring>
using namespace std;
int read() {
int x=0,f=1;
char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=1<<5;
long long s[maxn][maxn];
long double jc[maxn],ans;
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif
s[0][0]=jc[0]=1;
for (int i=1;i<maxn;++i) {
jc[i]=jc[i-1]*i;
for (int j=1;j<=i;++j) s[i][j]=s[i-1][j-1]+s[i-1][j]*(i-1);
}
int T=read();
while (T--) {
int n=read(),k=read();
long double tmp=0;
for (int i=1;i<=k;++i) tmp+=s[n][i]-s[n-1][i-1];
ans=(tmp/jc[n]);
/*
ans-=1.0/n;
tmp=0;
for (int i=k;i<n;++i) tmp+=s[n-1][i];
ans+=(tmp/jc[n]);
*/
printf("%.4Lf\n",ans);
}
}

hdu3625-Rooms的更多相关文章

  1. 【组合数学:第一类斯特林数】【HDU3625】Examining the Rooms

    Examining the Rooms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. HDU3625 Examining the Rooms

    @(HDU)[Stirling數] Problem Description A murder happened in the hotel. As the best detective in the t ...

  3. [LeetCode] Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  4. [LeetCode] Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  5. codeforces 519E A and B and Lecture Rooms LCA倍增

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  6. The 2015 China Collegiate Programming Contest K Game Rooms hdu 5550

    Game Rooms Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  7. [SmartFoxServer概述]Zones和Rooms结构

    Zones和Rooms结构: 相对于SFS 1.X而言,在Zones和Rooms的配置上,SFS2X有了显著的改善.尤其是我们建立了房组这样一个简单的概念,它允许在一个逻辑组中管理Rooms,从而独立 ...

  8. LeetCode Meeting Rooms II

    原题链接在这里:https://leetcode.com/problems/meeting-rooms-ii/ Given an array of meeting time intervals con ...

  9. LeetCode Meeting Rooms

    原题链接在这里:https://leetcode.com/problems/meeting-rooms/ Given an array of meeting time intervals consis ...

  10. socket.io问题,io.sockets.manager.rooms和io.sockets.clients('particular room')这两个函数怎么用?

    为什么我用nodejs用这个两个函数获取都会出错呢?是不是socket的api改了?请问现在获取房间数和当前房间的客户怎么获取?什么函数?谢谢!!急求!     网友采纳 版本问题.io.socket ...

随机推荐

  1. SQL Server 中对 FOR XML和FROM的转换处理

    在SQL Server中对XML的再操作转换: 方法1: --生成XML SELECT * FROM [T_BAS_预算科目] FOR XML PATH --把XML转成SQL表 declare @X ...

  2. 浅谈Hash在多个字符串匹配类型问题中的应用

    在生活中们有时会遇到一些有关字符串匹配的问题. 这时打暴力往往显得很愚蠢,效率低下. 所以就需要一些算法和数据结构来提高效率. Hash Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把 ...

  3. 成都Uber优步司机奖励政策(1月23日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. .net core 基于multipart/form-data的文件上传,这里以图片上传为例

    首先传递的数据格式大概如下: 然后就可以在后端获取数据了:直接上代码了哈: [HttpPost]        ///分别获取 data数据和调用图片上传方法 public async Task< ...

  5. hdu1596find the safest road(floyd)

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. centos下php环境安装redis

    一.安装redis(仅可在服务器使用,尚不能通过浏览器访问) (1)首先下载redis:wget http://download.redis.io/releases/redis-4.0.9.tar.g ...

  7. python基础之全局局部变量及函数参数

    1.局部变量和全局变量 1.1局部变量 局部变量是在函数内部定义的变量,只能在定义函数的内部使用 函数执行结束后,函数内部的局部变量会被系统收回 不同函数可以定义相同名字的局部变量,但是各用个的互不影 ...

  8. 前端开发工程师 - 02.JavaScript程序设计 - 期末考试

    期末考试客观题 期末考试主观题 https://www.15yan.com/story/aY0HWAQ7oNU/     1(8分) 函数myType用于根据输入参数返回相应的类型信息. 语法如下: ...

  9. POJ - 3259

    要判断是否有负的权值 #include<iostream> #include<stdio.h> #include<algorithm> #include<st ...

  10. 开源自动驾驶仿真平台 AirSim (3) - 运行 AirSim

    AirSim 的官方 Github: https://github.com/Microsoft/AirSim 之前配置了很多,终于要让 AirSim 自己跑起来了. 我们需要把 AirSim 这个插件 ...