考虑以下示例:
#define SIZE 10
#define BASE 7
void set_combination(char destination[SIZE],
unsigned int index,
const char source[SIZE][BASE])
{
int i = SIZE;
/* Extract the 'SIZE' digits from 'index', starting
with the least significant (rightmost) one. */
while (i-->0) {
destination[i] = source[i][ index % BASE ];
index /= BASE;
}
/* Make sure destination[] is a string, by adding
the string-terminating '\0' char. */
destination[SIZE] = '\0';
}
该函数将 index 解释为 base BASE 中的 SIZE 位数字。
索引的有效范围是 0 到 BASESIZE-1,包括端点。范围环绕,因此索引 0 和 BASESIZE 在目标中生成完全相同的字符串。 (假设unsigned int 有足够的范围来表达该数字。在这种特殊情况下,如果 BASE=7 和 SIZE=10,则 710-1 = 282,475,248,恰好适合 unsigned int好的。)
最左边的数字是 source[0][i],其中 i 是 0 到 BASE-1(含)。每个数字都有自己的字符数组。最右边的数字是 source[SIZE-1][k],其中 k 是 0 到 BASE-1,包括 0。
在评论中,OP 询问是否可以将这种方法应用于源集为 59 个数字(1 到 59,包括在内)的情况,其中选择了三个。 (在这种情况下,唯一组合的数量为 59*58*57 = 195054。)
我已经展示了一种方法 construct_word() in this answer。与上面示例的不同之处在于,这一次,source 集合现在是可变的,并且每当我们选择一个字母/数字时,我们都会将其从源集合中删除。 (每个组合都以相同的source 集开始;变化只发生在单个组合的生成期间。)适应 OP 的特殊情况,它类似于以下内容:
void choose_3of59(char destination[3],
unsigned int index)
{
char source[59] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59 };
unsigned int base = 59;
int i = 3;
while (i-->0) {
/* Remove next 'base'-base digit out of index */
const unsigned int k = index % base;
index /= base;
/* Add that digit to destination */
destination[i] = source[k];
/* Remove this digit from the base set. */
base--;
source[k] = source[base];
}
}
index 从 0 到 195053 (= 59*58*57 - 1) 生成所有三位数组合,其中每个数字都是唯一的,从 1 到 59,包括 1 到 59。
请注意,虽然上面确实产生了所有组合,但这些组合不是按升序排列的(也就是说,destination[0]*59*59 + destination[1]*58 + destination[2] 不是按升序排列的)。
有些组合比其他组合更难生成。
例如,要生成具有 n1、n2 和 n3 整数的三元组 1 ≤ n1 0 和 (57*58*59)/(1*2*3) - 1 之间,包括) 对于每个三元组 (n1, n2, n3):
index(n1, n2, n3) = ((n1 - 174)*n1 + 10091)*n1 / 6 + (117 - n2)*n2 / 2 + n3 - 1771
接下来,我们可以使用二分查找首先找到最高有效数 n1(1 到 57 之间最多 6 步)。然后,当找到它时,我们可以使用另一个二进制搜索首先找到中间数字 n2。最后,我们可以用上面的方程来求解最低有效数n3:
/* Given 0 <= index <= 32508, generate a triplet of integers,
1 <= num[0] < num[1] < num[2] <= 59
*/
static void set_triplet59(char num[3], unsigned int index)
{
int num0, num1, num2;
/* Make sure 0 <= i <= 32508. */
index %= 32509;
/* Note that index 'index' corresponds to triplet 'num0', 'num1', 'num2':
index(num0, num1, num2) = ( (num0 - 174)*num0 + 10091)*num0/6 + (117 - num1)*num1/2 + num2 - 1771
Also note that
index(num0, num0+1, num0+2) = index0(num0)
index(num0, num1, num1+1) = index01(num0, num1) = index0(num0) + index1(num0, num1)
index(num0, num1, num2) = index0(num0) + index1(num0, num1) + (num2 - num1 - 1)
where
index0(num0) = (num0*(num0 - 176) + 10266)*(num0 - 1)/6
index1(num0, num1) = (num1 + num0 - 118)*(num1 - num0 - 1)/2
index01(num0, num1) = index0(num0) + index1(num0, num1)
= 1/6*num0^3 - 29*num0^2 - 1/2*num1^2 + 10091/6*num0 + 119/2*num1 - 1770
= ((num0 - 174)*num0 + 10091)*num0 / 6 - (num1 - 119)*num1/2 - 1770
*/
/* Use a binary search to find the largest possible num0, where
index(num0, num0+1, num0+2) <= index.
The binary search does at most six iterations.
*/
{
const int limit6 = index*6; /* To avoid division by 6 for index0(num0). */
int min0 = 1; /* Inclusive */
int max0 = 58; /* Exclusive */
while (max0 - min0 > 1) {
num0 = (min0 + max0) / 2;
if ((num0*(num0 - 176) + 10266)*(num0 - 1) > limit6)
max0 = num0;
else
min0 = num0;
}
num0 = min0;
}
/* Use a binary search to find the largest possible num1, where
index(num0, num1, num1+1) <= index.
The search does at most six iterations. Note that
((num0 - 174)*num0 + 10091)*num0/6 + (117 - num1)*num1/2 + num1 + 1 - 1771 > index
is can be simplified to
(117 - num1)*num1/2 + num1 > index + 1770 - ((num0 - 174)*num0 + 10091)*num0/6
and further to
(117 - num1)*num1 + 2*num1 > 2*index + 3540 - ((num0 - 174)*num0 + 10091)*num0/3
and finally to
(119 - num1)*num1 > 2*index + 3540 - ((num0 - 174)*num0 + 10091)*num0/3
where the right side is 'limit'.
*/
{
const int limit = 2*index + 3540 - ((num0 - 174)*num0 + 10091)*num0 / 3;
int min1 = num0 + 1; /* Inclusive */
int max1 = 59; /* Exclusive */
while (max1 - min1 > 1) {
num1 = (min1 + max1) / 2;
if ((119 - num1)*num1 > limit)
max1 = num1;
else
min1 = num1;
}
num1 = min1;
}
/* Since we know index, num0, and num1, we can calculate num2 directly. */
num2 = index + 1771 + num1*(num1 - 117)/2 - num0*(10091 - num0*(174 - num0))/6;
num[0] = num0;
num[1] = num1;
num[2] = num2;
}
以上经过验证可以生成与
完全相同的组合
index = 0;
for (num[0] = 1; num[0] <= 57; num[0]++)
for (num[1] = num[0]+1; num[1] <= 58; num[1]++)
for (num[2] = num[1]+1; num[2] <= 59; num[2]++, index++) {
/* index, num[0], num[1], num[2] */
}
在 Intel Core i5-7200U (x86-64) 上,set_triplet59(triplet, index) 在所有有效三元组中需要 145 个周期的中位数。作为比较,choose_3of59(triplet, index) 在所有组合中需要 115 个周期的中位数。 (换句话说,您可以预期 set_triplet() 在 x86-64 上比 choose_3of59() 慢大约四分之一到三分之一。)