Hiho #1075: 开锁魔法III

 

Problem Statement

描述

一日,崔克茜来到小马镇表演魔法。 

其中有一个节目是开锁咒:舞台上有 n 个盒子,每个盒子中有一把钥匙,对于每个盒子而言有且仅有一把钥匙能打开它。初始时,崔克茜将会随机地选择 k 个盒子用魔法将它们打开。崔克茜想知道最后所有盒子都被打开的概率,你能帮助她回答这个问题吗?

输入 

第一行一个整数T (T100)表示数据组数。 对于每组数据,第一行有两个整数nk (1n300,0kn)。 第二行有n个整数ai,表示第i个盒子中,装有可以打开第ai个盒子的钥匙。

输出 

对于每组询问,输出一行表示对应的答案。要求相对误差不超过四位小数。

样例输入

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 Cnk 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 km.

 

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 i1 cycles and then the ith cycle.

 

  • If we use ki keys to solve the ith cycle, we can use only jki keys to solve the first i1 cycles.

 

  • ki can vary from 1 to j. (Because the initial status may not need key solving, thus m can vary to j.) And ki can't be greater than the size of ith cycle, denoted as li. (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 ki, we just need to multiply the result of first i1 cycles and the result of ith cycle, i.e. dp[i1][jki]Cliki
    (Every ki keys can solve the ith cycle, so the result of solving ith cycle is Cliki.)

 

 

According to above statements, we can get the recursion equation. Here, we use array comb[n][m] to denote the math combination Cnm.

 dp[i][j]=m=1j(dp[i1][jm]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 Cij=Ci1j+Ci1j1. Code is
1
2
3
4
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);         
}

 

posted @   kid551  阅读(365)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示