在数学中,n阶的排列σ是从整数1.n到其自身的双射函数。这份清单:
2 1 4 3表示置换σ,使得σ(1) = 2,σ(2) = 1,σ( 3 ) = 4,σ(4) =3。
置换σ的平方根是一个置换,当应用到它自己时,就会给出σ。例如,2 1 4 3有平方根τ=3 4 2 1。
k 1 2 3 4
τ(k) 3 4 2 1
τ(τ(k)) 2 1 4 3因为τ(τ(k)) =σ(k)对所有1≤k≤n.
表示排列的n>0整数的列表,所有这些整数都在1和n之间。置换总是有一个平方根的。
只要输入和输出一致,就可以使用0.n-1的列表。
置换的平方根,也是数组。
您的算法必须在n的多项式时间内运行,这意味着您不能只循环遍历所有的n!n阶排列
任何建筑物都是允许的。
注意,许多输入都有多个可能的输出。
2 1 4 3
3 4 2 1
1
1
3 1 2
2 3 1
8 3 9 1 5 4 10 13 2 12 6 11 7
12 9 2 10 5 7 4 11 3 1 13 8 6
13 7 12 8 10 2 3 11 1 4 5 6 9
9 8 5 2 12 4 11 7 13 6 3 10 1发布于 2016-03-07 00:34:05
p([],_,[]). p([H|T],B,[I|U]):-p(T,B,U),nth1(H,B,I). f(X,Y):-p(Y,Y,X).解释:
permutate([], _, []). % An empty permutation is empty
permutate([X|Xs], List, [Y|Ys]) :- % To permutate List
permutate(Xs, List, Ys), % Apply the rest of the permutation
nth1(X, List, Y). % Y is the Xth element of List
root(Permutation, Root) :- % The root of Permutation
permutate(Root, Root, Permutation). % Applied to itself, is Permutationhttps://codegolf.stackexchange.com/questions/74953
复制相似问题