Hiho #1075: 开锁魔法III
Problem Statement
描述
一日,崔克茜来到小马镇表演魔法。
其中有一个节目是开锁咒:舞台上有 n 个盒子,每个盒子中有一把钥匙,对于每个盒子而言有且仅有一把钥匙能打开它。初始时,崔克茜将会随机地选择 k 个盒子用魔法将它们打开。崔克茜想知道最后所有盒子都被打开的概率,你能帮助她回答这个问题吗?
输入
第一行一个整数$T$ ($T \leq 100$)表示数据组数。 对于每组数据,第一行有两个整数$n$和$k$ ($1 \leq n \leq 300, 0 \leq k \leq n$)。 第二行有$n$个整数$a_i$,表示第$i$个盒子中,装有可以打开第$a_i$个盒子的钥匙。
输出
对于每组询问,输出一行表示对应的答案。要求相对误差不超过四位小数。
样例输入
4
5 1
2 5 4 3 1
5 2
2 5 4 3 1
5 3
2 5 4 3 1
5 4
2 5 4 3 1
样例输出
0.000000000
0.600000000
0.900000000
1.000000000
The problem is to compute the probability that use $k$ keys to open the $n$ boxes. In fact we only need to comupte the number of methods that successfully opening $n$ boxes by $k$ choices. Then dividing $C_n^k$ is the final result. So, let's focus on the more refined problem.
First, let's use some notations to express the problem.
Assume the key in box $i$ can open box $a[i]$. Then, the boxes can be opend from box $1$ to $n$ is {$a[1], a[2], ..., a[n]$}.
If we determine open box $i$, then we'll use the key $a[i]$ to open box $a[i]$ which contains the $a[a[i]]$ box key.
So we can assume the keys in the $n$ boxes as a permutation of numbers {$1, 2, ..., n$}. The math model here is just the permutation group in Abstract Algebra.
In order to open all $n$ boxes, we first need to check how many cycles in the permutation. Because the number of keys we need to open all boxes must be greater than or equal to the number of cycles in permutation.
So, if define the number of keys is $k$, and the number of cycles in the $n$-permutation is $m$, the above states $k \geq m$.
Now, we need to design an algorithm to solve the problem. The basical idea is Dynamic Programming (DP).
In general, the hard part of DP is to form a sub-problem. Here, based on the analysis of the permutation above, we'll set the sub-problem by cycles. Because there're $m$ cycles in the $n$-permutation, we'll use $m$ steps to solve the problem.
Define: dp[i][j] = the number of methods that using $j$ keys to solve first $i$ cycles.
Thus the problem can be expressed as computing $dp[m][k]$.
Next, let's construct the recursion. Assume we need to compute $dp[i][j]$.
- In order to solve first $i$ cycles, we can first solve $i-1$ cycles and then the $i^{th}$ cycle.
- If we use $k_i$ keys to solve the $i^{th}$ cycle, we can use only $j-k_i$ keys to solve the first $i-1$ cycles.
- $k_i$ can vary from $1$ to $j$. (Because the initial status may not need key solving, thus $m$ can vary to $j$.) And $k_i$ can't be greater than the size of $i^{th}$ cycle, denoted as $l_i$. (Because every key is belong to one box, so the number of keys we choose can't be greater than the number of boxes in all.)
- For every fixed $k_i$, we just need to multiply the result of first $i-1$ cycles and the result of $i^{th}$ cycle, i.e. $dp[i-1][j-k_i] * C_{l_i}^{k_i}$
(Every $k_i$ keys can solve the $i^{th}$ cycle, so the result of solving $i^{th}$ cycle is $ C_{l_i}^{k_i}$.)
According to above statements, we can get the recursion equation. Here, we use array $comb[n][m]$ to denote the math combination $C_n^m$.
$$dp[i][j] = \sum_{m=1}^{j}(dp[i-1][j-m]*comb[cycle\_i\_length][m])$$
So, the problem is done. But there're two additional problems we need to solve priori.
- First, for efficiency, we can compute the combination numbers before we do the DP algorithm. The computing is also based on DP thinking:
- Compute the combination number by DP, i.e. the simple math equation $C_i^j = C_{i-1}^j + C_{i-1}^{j-1}$. Code is
for(int i = 0; i < 500; ++i)
for(int j = 0; j <= i; ++j)
comb[i][j] =
(0 == i || 0 == j) ? 1 : comb[i-1][j] + comb[i-1][j-1];
- Second, we need to compute the $m$ sizes of cycles in the $n$-permutation:
- Get the cycle information in a $n$-permutation, including number of cycles and the size of every cycle. Here we use array $perm$ to indicate the permutation of $n$ elements.
vector<int> cycles; //store the cycle information
bool used[500];
memset(used, 0, sizeof used); for (int i = 0; i < n; ++i){
if(used[i] == true)
continue; int num = 0;
int idx = i;
while(!used[idx])
{
++num;
used[idx] = true;
idx = perm[idx];
} cycles.push_back(num);
}
Hiho #1075: 开锁魔法III的更多相关文章
- hihocoder 1075 : 开锁魔法III
描述 一日,崔克茜来到小马镇表演魔法. 其中有一个节目是开锁咒:舞台上有 n 个盒子,每个盒子中有一把钥匙,对于每个盒子而言有且仅有一把钥匙能打开它.初始时,崔克茜将会随机地选择 k 个盒子用魔法将它 ...
- #1075 : 开锁魔法III
描述 一日,崔克茜来到小马镇表演魔法. 其中有一个节目是开锁咒:舞台上有 n 个盒子,每个盒子中有一把钥匙,对于每个盒子而言有且仅有一把钥匙能打开它.初始时,崔克茜将会随机地选择 k 个盒子用魔法将它 ...
- HihoCoder 1075 开锁魔法III(概率DP+组合)
描述 一日,崔克茜来到小马镇表演魔法. 其中有一个节目是开锁咒:舞台上有 n 个盒子,每个盒子中有一把钥匙,对于每个盒子而言有且仅有一把钥匙能打开它.初始时,崔克茜将会随机地选择 k 个盒子用魔法将它 ...
- hihoCode 1075 : 开锁魔法III
时间限制:6000ms 单点时限:1000ms 内存限制:256MB 描述 一日,崔克茜来到小马镇表演魔法. 其中有一个节目是开锁咒:舞台上有 n 个盒子,每个盒子中有一把钥匙,对于每个盒子而言有且仅 ...
- hrb——开锁魔法I——————【规律】
解题思路:从1到n的倒数之和. #include<stdio.h> #include<string.h> #include<algorithm> using nam ...
- hihocoder1075【开锁魔法】
hihocoder1075[开锁魔法] 题意是给你一个 \(1-n\) 的置换,求选 \(k\) 个可以遍历所有点的概率. 题目可以换个模型:有 \(n\) 个球,有 \(cnt\) 种不同的颜色,求 ...
- BZOJ 5004: 开锁魔法II 期望 + 组合
Description 题面:www.lydsy.com/JudgeOnline/upload/task.pdf Input Output 一般概率题有两种套路: 满足条件的方案/总方案. 直接求概率 ...
- bzoj5003: 与链 5004: 开锁魔法II 5005:乒乓游戏
www.lydsy.com/JudgeOnline/upload/task.pdf 第一题题意可以转为选一个长度k的序列,每一项二进制的1的位置被下一项包含,且总和为1,考虑每个二进制位的出现位置,可 ...
- 【bzoj5004】开锁魔法II 组合数学+概率dp
题目描述 有 $n$ 个箱子,每个箱子里有且仅有一把钥匙,每个箱子有且仅有一把钥匙可以将其打开.现在随机打开 $m$ 个箱子,求能够将所有箱子打开的概率. 题解 组合数学+概率dp 题目约定了每个点的 ...
随机推荐
- IOS初级:app的图标
1,首先准备6张png图,分辨率一定要正确,不然报错(The app icon set named "AppIcon" did not have any applicable co ...
- NOIP水题测试(2017082501)
日常水题测试又来了! 以后答案都以单题形式公布. 下面看今天的水题: 时间限制:5小时 题目一:无法形容的水 题目二:比上一题还水 题目三:一元三次方程求解 题目四:单词接龙 题目五:统计单词个数 题 ...
- 如何使用GCC生成动态库和静态库
根据链接时期的不同,库又有静态库和动态库之分.静态库是在链接阶段被链接的,所以生成的可执行文件就不受库的影响,即使库被删除,程序依然可以成功运行.而动态库是在程序执行的时候被链接的.程序执行完,库仍需 ...
- python之数据库内置方法以及pymysql的使用
一.mysql内置方法 1)视图的概念和用法 .什么是视图 视图就是通过查询得到一张虚拟表,然后保存下来,下次用的直接使用即可 .为什么要用视图 如果要频繁使用一张虚拟表,可以不用重复查询 .如何用视 ...
- clion配置c/c++环境
打开这个界面 点击添加Cygwin选择下载的Cygwin在进行下面的配置 去网站https://www.cygwin.com/选择路径即可(这里只写了配置过程中的关键步骤并且附上IDE的链接直接安装 ...
- fastq-to-fasta转换及fasta拆分、合并
格式转换: use awk :awk 'BEGIN{P=1}{if(P==1||P==2){gsub(/^[@]/,">");print}; if(P==4)P=0; P++ ...
- mysql学习之路_sql
查看数据库: Show databases; 查看指定部分数据库:模糊查询 Show databases like ‘patten’;--paatten是匹配模式 %:表示是匹配模式 _:表示匹配单个 ...
- hadoop 组件 hdfs架构及读写流程
一 . Namenode Namenode 是整个系统的管理节点 就像一本书的目录,储存文件信息,地址,接受用户请求,等 二 . Datanode 提供真实的文件数据,存储服务 文件块(block)是 ...
- ubuntu16.04 编译安卓4.2
1. root@ge-Lenovo:/usr/lib/jvm# cd /home/material/install/jdk/ jdk-6u29-linux-x64.bin jdk-6u45-l ...
- VESA时序与BT1120的区别
在实现内嵌传输的过程中,笔者参考VESA的时序,也就是下图,实现了一个内嵌同步的程序,同步码放在H Back Porch与H Front Porch的后端与前端,但是在传输过程中发现接收端画面不正常. ...