|
@@ -20,7 +20,7 @@ type
|
|
|
|
|
|
var
|
|
|
permu, permu_copy, count: TIntegerArray;
|
|
|
- r, n, answer : longint;
|
|
|
+ r, n, answer: longint;
|
|
|
|
|
|
procedure swap(var a, b: longint); inline;
|
|
|
var tmp: longint;
|
|
@@ -40,48 +40,46 @@ begin
|
|
|
end;
|
|
|
end;
|
|
|
|
|
|
+function countflips: longint; inline;
|
|
|
+var
|
|
|
+ last: LongInt;
|
|
|
+ tmp: LongInt;
|
|
|
+begin
|
|
|
+ countflips := 0;
|
|
|
+ last := permu_copy[0];
|
|
|
+ repeat
|
|
|
+ // Reverse part of the array.
|
|
|
+ reverse(last);
|
|
|
+
|
|
|
+ tmp := permu_copy[last];
|
|
|
+ permu_copy[last] := last;
|
|
|
+ last := tmp;
|
|
|
+ inc(countflips);
|
|
|
+ until last = 0;
|
|
|
+end;
|
|
|
+
|
|
|
function NextPermutation: boolean;
|
|
|
var
|
|
|
- r0: longint;
|
|
|
tmp: LongInt;
|
|
|
i : longint;
|
|
|
begin
|
|
|
- r0 := r; // use local variable
|
|
|
NextPermutation := true;
|
|
|
repeat
|
|
|
- if r0 = n then
|
|
|
+ if r = n then
|
|
|
begin
|
|
|
NextPermutation := false;
|
|
|
break;
|
|
|
end;
|
|
|
tmp := permu[0];
|
|
|
- for i := 1 to r0 do
|
|
|
+ for i := 1 to r do
|
|
|
permu[i-1] := permu[i];
|
|
|
- permu[r0] := tmp;
|
|
|
+ permu[r] := tmp;
|
|
|
|
|
|
- dec(count[r0]);
|
|
|
- if count[r0] > 0 then
|
|
|
+ dec(count[r]);
|
|
|
+ if count[r] > 0 then
|
|
|
break;
|
|
|
- inc(r0);
|
|
|
+ inc(r);
|
|
|
until false;
|
|
|
- r := r0;
|
|
|
-end;
|
|
|
-
|
|
|
-function countflips: integer; inline;
|
|
|
-var
|
|
|
- last: LongInt;
|
|
|
- tmp: LongInt;
|
|
|
-begin
|
|
|
- countflips := 0;
|
|
|
- last := permu_copy[0];
|
|
|
- repeat
|
|
|
- // Reverse part of the array.
|
|
|
- reverse(last);
|
|
|
- tmp := permu_copy[ last ];
|
|
|
- permu_copy[ last ] := last;
|
|
|
- last := tmp;
|
|
|
- inc(countflips);
|
|
|
- until last = 0;
|
|
|
end;
|
|
|
|
|
|
function fannkuch: longint;
|