fork download
  1. program wardrobe3;
  2. Uses Strutils;
  3. const MAX = 5000000;
  4. type elenco = array[1..MAX] of string[1];
  5. var m,k,i,j,w:qword;
  6. numero, count : qword;
  7. cod : longint;
  8. cifre, stringadainvertire, stringainvertita, stringaordinata, Sconinv : Ansistring;
  9. arrnum :array [1..MAX] of string[1];
  10. arrpsw :array [1..MAX+2] of qword;
  11. uscita:boolean;
  12.  
  13. procedure scambia (var x,y: string);
  14. var t:string;
  15. begin
  16. t:=x;
  17. x:=y;
  18. y:=t;
  19. end;
  20. procedure scambiach (var x,y: char);
  21. var t:char;
  22. begin
  23. t:=x;
  24. x:=y;
  25. y:=t;
  26. end;
  27. Procedure ordinamento (estremoi,estremos: qword; var v : elenco; ordinato:boolean);
  28. var inf, sup, medio:qword;
  29. pivot :string[1];
  30. begin
  31. inf:=estremoi;
  32. sup:=estremos;
  33. medio:= (estremoi+estremos) div 2;
  34. pivot:=v[medio];
  35. repeat
  36. if (ordinato) then
  37. begin
  38. while (v[inf]<pivot) do inf:=inf+1;
  39. while (v[sup]>pivot) do sup:=sup-1;
  40. end;
  41. if inf<=sup then
  42. begin
  43. scambia(v[inf],v[sup]);
  44. inf:=inf+1;
  45. sup:=sup-1;
  46. end;
  47. until inf>sup;
  48. if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
  49. if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
  50. end;
  51.  
  52. Procedure inversione (x:Ansistring; inizio:qword; var Sconinv:ansistring);
  53. var Sdainv,Sprefix : ansistring;
  54. begin
  55. count:=m-inizio+1; Sprefix:='';
  56. Sdainv:=copy(x,inizio, count);
  57. Sprefix:=copy(x, 1, inizio-1);
  58. stringainvertita:=ReverseString(Sdainv);
  59. Sconinv:=Sprefix+stringainvertita;
  60. end;
  61.  
  62. Procedure nextPermutation (var P : ansistring) ;
  63. var pivot:qword;
  64. (* Find the pivot index*)
  65. begin
  66. pivot := 0;
  67. for i := m - 1 downto 1 do
  68. if (P[i] < P[i + 1]) then begin pivot:=i; break; end;
  69. if pivot = 0 then uscita:=true;
  70. (* find the element from the right that is greater than pivot*)
  71. for i := m downto pivot+1 do
  72. if (P[i] > P[pivot]) then begin scambiach (P[i], P[pivot]); break; end;
  73. (*Reverse the elements from pivot + 1 to the end to get the next permutation*)
  74. if pivot+1=m then Sconinv:=P
  75. else inversione (P, pivot+1, Sconinv);
  76.  
  77. end;
  78.  
  79. begin
  80. readln(m,k);
  81. readln(cifre);
  82. (* X:=ReverseString(cifre); writeln (X);*)
  83. for i:=1 to m do arrnum[i]:=copy(cifre,i,1);
  84. ordinamento (1,m,arrnum, true);
  85. stringaordinata:=''; Sconinv:='';
  86. for i:=1 to m do stringaordinata:=stringaordinata+arrnum[i];
  87. (*val(cifre,numero,cod);
  88.   writeln (numero);
  89.   inversione (cifre, 3, stringainvertita);
  90.   writeln(stringainvertita);*)
  91. stringadainvertire:=stringaordinata;
  92. for i:=1 to m do write(arrnum[i]); writeln('oo');
  93. uscita:=false; val(stringaordinata,arrpsw[1],cod); w:=2;
  94. while uscita=false do begin nextPermutation(stringadainvertire); stringadainvertire:=Sconinv; val(Sconinv,arrpsw[w],cod); w:=w+1; end;
  95. writeln(w-1);
  96. for i:=1 to w-1 do
  97. for j:=w-1 downto 1 do if (arrpsw[j]-arrpsw[i]) mod k =0 then begin writeln(arrpsw[i]); writeln(arrpsw[j]); exit; end;
  98. writeln('-1');
  99. end.
  100.  
Success #stdin #stdout 0.01s 5328KB
stdin
6 3
123042
stdout
012234oo
361
12234
432210