hdu3625
hdu3625
题意:
酒店发生一起谋杀案。作为镇上最好的侦探,您应该立即检查酒店的所有N个房间。但是,房间的所有门都是锁着的,钥匙刚锁在房间里,真是个陷阱!您知道每个房间里只有一把钥匙,并且所有可能的分配可能性均等。例如,如果N = 3,则有6种可能的分布,每种分布的概率为1/6。为方便起见,我们将房间编号从1到N,房间1的键编号为键1,房间2的键编号为2,依此类推。
要检查所有房间,您必须用力摧毁一些门。但是您不想破坏太多,因此您采取以下策略:首先,您手中没有钥匙,因此您会随机破坏一扇锁着的门,进入房间,检查并取出其中的钥匙。然后,也许您可以使用新钥匙打开另一个房间,检查一下并获得第二把钥匙。重复此操作,直到您无法打开任何新房间。如果仍然有未检查的房间,则必须随机挑选另一扇未打开的门用力摧毁,然后重复上述步骤,直到检查完所有房间为止。
现在只允许您强行摧毁最多K门。更重要的是,房间1中有一个非常重要的人物。不允许您破坏房间1的门,也就是说,检查房间1的唯一方法是使用相应的钥匙打开房间。您想知道最终检查所有房间的概率是什么。(来自一键翻译)
分析
等价于将 $n$ 个元素分成 $k$ 个子集,其中1号元素不能单独成一个子集,$k$ 可以取1至K.
所以,设 $s(n,i)$ 为第一类Stirling数,可行方案为 $\sum_{i=1}^k s(n, i)-s(n-1, i-1)$,总方案数为 $n!$
#include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = + ;
ll fact[maxn], stir[maxn][maxn]; void init()
{
fact[] = ;
for(int i = ;i < maxn;i++) fact[i] = fact[i-] * i; stir[][] = ;
stir[][] = ;
for(int i = ;i < maxn;i++)
for(int j = ;j <= i;j++)
stir[i][j] = stir[i-][j-] + (i-)*stir[i-][j];
} int main()
{
init();
int T;
scanf("%d", &T);
while(T--)
{
int n, k;
scanf("%d%d", &n, &k);
ll ans = ;
for(int i = ;i <= k; i++)
ans += stir[n][i] - stir[n-][i-];
printf("%.4f\n", 1.0*ans/fact[n]);
}
return ;
}
Codejam4214486 A Password Attacker
题意:一串长度为 $N$ 的密码恰有 $M$ 种字符组成,求可能的字符串的种数。
分析:把 $n$ 个位置看作 $n$ 个有区别的小球,问题等价于将 $n$ 个有区别的球放到 $m$ 个不同的盒子里,且无空盒的方案数,易知,方案数为 $m!s(n, m)$,其中 $s(n, m)$ 为第二类Stirling数。
#include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = +;
const ll mod =;
ll fact[maxn], stir[maxn][maxn]; void init()
{
fact[] = ;
for(int i = ;i < maxn;i++) fact[i] = fact[i-] * i % mod; stir[][] = ;
stir[][] = ;
for(int i = ;i < maxn;i++)
for(int j = ;j <= i;j++)
stir[i][j] = (stir[i-][j-] + j*stir[i-][j]) % mod;
} int main()
{
freopen("A-large-practice.in", "r", stdin);
freopen("a.out", "w", stdout); init();
int T, kase = ;
scanf("%d", &T);
while(T--)
{
int n, m;
scanf("%d%d", &m, &n);
ll ans = fact[m] * stir[n][m] % mod;
printf("Case #%d: %lld\n", ++kase, ans);
}
return ;
}
参考链接:
1. https://blog.csdn.net/doyouseeman/article/details/50876786
2. https://blog.csdn.net/ACdreamers/article/details/8521134
hdu3625的更多相关文章
- 【组合数学:第一类斯特林数】【HDU3625】Examining the Rooms
Examining the Rooms Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU3625(SummerTrainingDay05-N 第一类斯特林数)
Examining the Rooms Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu3625(第一类斯特林数)
与第二类有些区别! #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...
- HDU3625 Examining the Rooms
@(HDU)[Stirling數] Problem Description A murder happened in the hotel. As the best detective in the t ...
随机推荐
- 设计模式 AOP,OOP
AOP.OOP在字面上虽然非常类似,但却是面向不同领域的两种设计思想. 简单说,AOP面向动词领域,OOP面向名词领域 AOP: (Aspect Oriented Programming) 面向切面编 ...
- XML中的XPATH和DTD
大家好,乐字节小乐又来了,上次给大家说道的是XML解析,这次接着讲述XML文档中的语言:XPATH.DTD 一.先来说说XPATH 1.XPATH 概念 XPath 是一门在 XML 文档中查找信息的 ...
- 解决 niceScroll 自适应DOM 高度变化
利用dataTable展示数据列表时,当选择每页显示数量时,滚动条还是按照页面初始化时显示的,导致无法滚动查看下面的数据, 在stackoverflower 找到一个可用的方法,但不知道为什么仅写 ...
- swift版 二分查找 (折半查找)
二分查找作为一种常见的查找方法,将原本是线性时间提升到了对数时间范围之内,大大缩短了搜索时间,但它有一个前提,就是必须在有序数据中进行查找.废话少说,直接上代码,可复制粘贴直接出结果! import ...
- 解决使用RabbitTemplate操作RabbitMQ,发生The channelMax limit is reached. Try later.问题
使用RabbitTemplate操纵RabbitMQ,每个RabbitTemplate等于一个connection,每个connection最多支持2048个channel,当hannel达到2048 ...
- c#mysql数据库备份还原
1:引用dll MySql.Data.dll, MySqlbackup.dll 2:建一个数据连接静态类 public static class mysql { public static str ...
- left join 左边有数据,右边无数据
参考了链接: https://blog.csdn.net/chenjianandiyi/article/details/52402011 主要是and和where的区别: 原Sql: Con ...
- centos7#yum install ffmpeg
yum install ffmpeg rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro rpm -Uvh http://li. ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- celery 定时任务,使用crontab表达式不执行(版本4.3.x)
celery 定时任务,使用crontab表达式不执行(版本4.3.x) 在使用celery 执行定时任务时,发现任务不会执行,schedule设置如下: 经测试,如果去掉hour,则任务每分钟都会执 ...