且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何找到一个K-排列从n个元素的索引?

更新时间:2023-01-11 21:01:49

排列在给定位置的给定数字的数量由一个公式给出 (正数位)! /(N-K)!其中,数位开始在左边1。

The number of permutations for a given digit in a given position is given by a formula (n-digit position)! / (n-k)! where digit position starts on the left with 1.

要得到的preceding排列对于给定的置换(即,其索引),乘式为每一个数字由尚未使用preceding的位数,并把它们加起来。数

To get the number of preceding permutations for a given permutation (that is, its index), multiply the formula for each digit by the number of preceding digits not already used, and add them up.

实施例1中,k = 2,n = 4时,p值= [3,4]

Example 1, k = 2, n = 4, p = [3,4]

第一个数字,3: (4-1)! /(4-2)! *(未使用preceding位数,2)= 6 有六个排列preceding第一,拥有3个在位置1。

First digit, 3: (4-1)! / (4-2)! * (number of unused preceding digits, 2) = 6 There are six permutations preceding the first that has 3 in position 1.

二位,4: (4-2)! /(4-2)! *(未使用preceding位数,2)= 2 有两种排列preceding第一,在位置2有4个。

Second digit, 4: (4-2)! / (4-2)! * (number of unused preceding digits, 2) = 2 There are two permutations preceding the first that has 4 in position 2.

零基指数:6 + 2 = 8

Zero based index: 6 + 2 = 8.

实施例2中,k = 3,N = 4,p值= [3,2,4]

Example 2, k = 3, n = 4, p = [3,2,4]

第一个数字,3: (4-1)! /(4-3)! *(未使用preceding位数,2)= 12 有12个排列preceding第一,拥有3个在位置1。

First digit, 3: (4-1)! / (4-3)! * (number of unused preceding digits, 2) = 12 There are 12 permutations preceding the first that has 3 in position 1.

二位,2: (4-2)! /(4-3)! *(未使用preceding位数,1)= 2 有两种排列preceding第一个有2个位置2。

Second digit, 2: (4-2)! / (4-3)! * (number of unused preceding digits, 1) = 2 There are two permutations preceding the first that has 2 in position 2.

第三位,4: (4-3)! /(4-3)! *(未使用preceding位数,1)= 1 有一个置换preceding第一,在第3位有4个。

Third digit, 4: (4-3)! / (4-3)! * (number of unused preceding digits, 1) = 1 There is one permutation preceding the first that has 4 in position 3.

从零开始的索引:12 + 2 + 1 = 15

Zero based index: 12 + 2 + 1 = 15.