- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在微软面试中被问到了。非常想知道为什么这些人会问这么奇怪的概率问题?
给定一个 rand(N),一个随机生成器,它生成从 0 到 N-1 的随机数。
int A[N]; // An array of size N
for(i = 0; i < N; i++)
{
int m = rand(N);
int n = rand(N);
swap(A[m],A[n]);
}
最佳答案
好吧,我对这个有点乐趣。当我第一次阅读这个问题时,我想到的第一件事是群论(特别是对称群 Sn)。 for 循环通过在每次迭代中组合转置(即交换)来简单地在 Sn 中构建置换 σ。我的数学不是那么出色,而且我有点生疏,所以如果我的符号不合适,请忍受我。
概述
让 A
是我们的数组在排列后不变的事件。我们最终被要求找到事件的概率 A
, Pr(A)
.
我的解决方案尝试遵循以下过程:
An identity transposition occurs when a number is swapped with itself. That is, when n == m in the above for loop.
N
换位。可以有
0, 1, 2, ... , N
出现在这个“链”中的身份转换。
N = 3
案例:
Given our input [0, 1, 2].
Swap (0 1) and get [1, 0, 2].
Swap (1 1) and get [1, 0, 2]. ** Here is an identity **
Swap (2 2) and get [1, 0, 2]. ** And another **
K_i
是事件
i
身份换位出现在给定的排列中。请注意,这形成了所有可能结果的详尽划分:
0
之间和 N
身份转换。 Now we can finally take advantage of the the partition. Note that when the number of non-identity transpositions is odd, there is no way the array can go unchanged*. Thus:
*From group theory, a permutation is even or odd but never both. Therefore an odd permutation cannot be the identity permutation (since the identity permutation is even).
So we now must determine two probabilities for N-i
even:
The first term, , represents the probability of obtaining a permutation with i
identity transpositions. This turns out to be binomial since for each iteration of the for loop:
1/N
.Thus for N
trials, the probability of obtaining i
identity transpositions is:
So if you've made it this far, we have reduced the problem to finding for N - i
even. This represents the probability of obtaining an identity permutation given i
of the transpositions are identities. I use a naive counting approach to determine the number of ways of achieving the identity permutation over the number of possible permutations.
First consider the permutations (n, m)
and (m, n)
equivalent. Then, let M
be the number of non-identity permutations possible. We will use this quantity frequently.
The goal here is to determine the number of ways a collections of transpositions can be combined to form the identity permutation. I will try to construct the general solution along side an example of N = 4
.
Let's consider the N = 4
case with all identity transpositions (i.e. i = N = 4
). Let X
represent an identity transposition. For each X
, there are N
possibilities (they are: n = m = 0, 1, 2, ... , N - 1
). Thus there are N^i = 4^4
possibilities for achieving the identity permutation. For completeness, we add the binomial coefficient, C(N, i)
, to consider ordering of the identity transpositions (here it just equals 1). I've tried to depict this below with the physical layout of elements above and the number of possibilities below:
I = _X_ _X_ _X_ _X_
N * N * N * N * C(4, 4) => N^N * C(N, N) possibilities
N = 4
和
i = 4
,我们可以看看一般情况。结合上面的分母,我们发现:
This is intuitive. In fact, any other value other than 1
should probably alarm you. Think about it: we are given the situation in which all N
transpositions are said to be identities. What's the probably that the array is unchanged in this situation? Clearly, 1
.
Now, again for N = 4
, let's consider 2 identity transpositions (i.e. i = N - 2 = 2
). As a convention, we will place the two identities at the end (and account for ordering later). We know now that we need to pick two transpositions which, when composed, will become the identity permutation. Let's place any element in the first location, call it t1
. As stated above, there are M
possibilities supposing t1
is not an identity (it can't be as we have already placed two).
I = _t1_ ___ _X_ _X_
M * ? * N * N
t1
的倒数。 ,实际上是
t1
(这是唯一的逆唯一性)。我们再次包含二项式系数:在这种情况下,我们有 4 个开放位置,我们希望放置 2 个身份排列。我们有多少种方法可以做到这一点? 4 选择 2。
I = _t1_ _t1_ _X_ _X_
M * 1 * N * N * C(4, 2) => C(N, N-2) * M * N^(N-2) possibilities
Finally we do the N = 4
case with no identity transpositions (i.e. i = N - 4 = 0
). Since there are a lot of possibilities, it starts to get tricky and we must be careful not to double count. We start similarly by placing a single element in the first spot and working out possible combinations. Take the easiest first: the same transposition 4 times.
I = _t1_ _t1_ _t1_ _t1_
M * 1 * 1 * 1 => M possibilities
t1
和
t2
.有
M
t1
的可能性并且只有
M-1
t2
的可能性(因为
t2
不能等于
t1
)。如果我们穷尽所有的安排,我们会留下以下模式:
I = _t1_ _t1_ _t2_ _t2_
M * 1 * M-1 * 1 => M * (M - 1) possibilities (1)st
= _t1_ _t2_ _t1_ _t2_
M * M-1 * 1 * 1 => M * (M - 1) possibilities (2)nd
= _t1_ _t2_ _t2_ _t1_
M * M-1 * 1 * 1 => M * (M - 1) possibilities (3)rd
t1
,
t2
,
t3
.让我们放置
t1
先然后
t2
.像往常一样,我们有:
I = _t1_ _t2_ ___ ___
M * ? * ? * ?
t2
s 可能还有,我们将在一分钟内看到原因。
t1
在第三个位置。通知,
t1
必须去那里,因为如果要去最后一个地方,我们只会重新创建
(3)rd
上面的安排。重复计算是不好的!这留下了第三个唯一元素
t3
到最终位置。
I = _t1_ _t2_ _t1_ _t3_
M * ? * 1 * ?
t2
的数量?更接近?换位
t1
和
t2
不能 是不相交的排列(即它们必须共享其
n
或
m
中的一个(并且只有一个,因为它们也不能相等)。这样做的原因是因为如果它们不相交,我们可以交换排列顺序。这意味着我们将重复计算
(1)st
安排。
t1 = (n, m)
.
t2
必须是形式
(n, x)
或
(y, m)
一些
x
和
y
为了不相交。请注意
x
可能不是
n
或
m
和
y
许多不是
n
或
m
.因此,可能的排列数
t2
可能实际上是
2 * (N - 2)
.
I = _t1_ _t2_ _t1_ _t3_
M * 2(N-2) * 1 * ?
t3
必须是
t1 t2 t1
的组合的倒数.让我们手动完成:
(n, m)(n, x)(n, m) = (m, x)
t3
必须是
(m, x)
.注意这是
不是 与
t1
不相交并且不等于
t1
或
t2
所以在这种情况下没有重复计算。
I = _t1_ _t2_ _t1_ _t3_
M * 2(N-2) * 1 * 1 => M * 2(N - 2) possibilities
N = 4
的答案。下面的情况。它与另一个答案中的经验数字非常匹配!
N = 4
停止工作.对于
N > 5
,给出的只是一个近似值(有多好,我不确定)。如果你仔细想想,很清楚为什么会这样:例如,给定
N = 8
换位,显然有四种方法可以用上面没有说明的四个独特元素来创建身份。随着排列变长(据我所知......),方式的数量似乎变得更加难以计算。
关于algorithm - 数组保持不变的概率是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11872190/
我正在尝试创建一个包含 int[][] 项的数组 即 int version0Indexes[][4] = { {1,2,3,4}, {5,6,7,8} }; int version1Indexes[
我有一个整数数组: private int array[]; 如果我还有一个名为 add 的方法,那么以下有什么区别: public void add(int value) { array[va
当您尝试在 JavaScript 中将一个数组添加到另一个数组时,它会将其转换为一个字符串。通常,当以另一种语言执行此操作时,列表会合并。 JavaScript [1, 2] + [3, 4] = "
根据我正在阅读的教程,如果您想创建一个包含 5 列和 3 行的表格来表示这样的数据... 45 4 34 99 56 3 23 99 43 2 1 1 0 43 67 ...它说你可以使用下
我通常使用 python 编写脚本/程序,但最近开始使用 JavaScript 进行编程,并且在使用数组时遇到了一些问题。 在 python 中,当我创建一个数组并使用 for x in y 时,我得
我有一个这样的数组: temp = [ 'data1', ['data1_a','data1_b'], ['data2_a','data2_b','data2_c'] ]; // 我想使用 toStr
rent_property (table name) id fullName propertyName 1 A House Name1 2 B
这个问题在这里已经有了答案: 关闭13年前。 Possible Duplicate: In C arrays why is this true? a[5] == 5[a] array[index] 和
使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。 我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。 使用两个
当仅在一个边距上操作时,apply 似乎不会重新组装 3D 数组。考虑: arr 1),但对我来说仍然很奇怪,如果一个函数返回一个具有尺寸的对象,那么它们基本上会被忽略。 最佳答案 这是一个不太理
我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码; $sql = "SELECT lat, lon FROM gps_data"; $stmt=$db->query
我需要找到一种方法来执行这个操作,我有一个形状数组 [批量大小, 150, 1] 代表 batch_size 整数序列,每个序列有 150 个元素长,但在每个序列中都有很多添加的零,以使所有序列具有相
我必须通过 url 中的 json 获取文本。 层次结构如下: 对象>数组>对象>数组>对象。 我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No valu
enter code here- (void)viewDidLoad { NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
知道如何对二维字符串数组执行修剪操作,例如使用 Java 流 API 进行 3x3 并将其收集回相同维度的 3x3 数组? 重点是避免使用显式的 for 循环。 当前的解决方案只是简单地执行一个 fo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有来自 ASP.NET Web 服务的以下 XML 输出: 1710 1711 1712 1713
如果我有一个对象todo作为您状态的一部分,并且该对象包含数组列表,则列表内部有对象,在这些对象内部还有另一个数组listItems。如何更新数组 listItems 中 id 为“poi098”的对
我想将最大长度为 8 的 bool 数组打包成一个字节,通过网络发送它,然后将其解压回 bool 数组。已经在这里尝试了一些解决方案,但没有用。我正在使用单声道。 我制作了 BitArray,然后尝试
我们的数据库中有这个字段指示一周中的每一天的真/假标志,如下所示:'1111110' 我需要将此值转换为 boolean 数组。 为此,我编写了以下代码: char[] freqs = weekday
我是一名优秀的程序员,十分优秀!