在 Pari/GP 中迭代向量的不同排列

Iterating over distinct permutations of a vector in Pari/GP

我想遍历向量的所有不同排列。我尝试通过结合使用 vecextract()numtoperm() 来创建排列向量,并使用 vecsort(,,,8) 来删除等效排列。

不幸的是,这不能很好地扩展:在我当前的 4GB 堆栈大小中,向量的最大大小小于 12!,而我的机器只有 16GB。

有没有办法在 运行 内存不足的情况下做到这一点,也许是通过直接生成第 k 个不同的排列?

PARI 中没有为此内置任何内容。我建议阅读 How to generate all the permutations of a multiset?

使用forperm.

forperm([1,1,2,3], v, print(v))

生产

Vecsmall([1, 1, 2, 3])
Vecsmall([1, 1, 3, 2])
Vecsmall([1, 2, 1, 3])
Vecsmall([1, 2, 3, 1])
Vecsmall([1, 3, 1, 2])
Vecsmall([1, 3, 2, 1])
Vecsmall([2, 1, 1, 3])
Vecsmall([2, 1, 3, 1])
Vecsmall([2, 3, 1, 1])
Vecsmall([3, 1, 1, 2])
Vecsmall([3, 1, 2, 1])
Vecsmall([3, 2, 1, 1])

请注意,forperm 的输入向量应该经过排序以获得正确的结果。