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. Sdainv:='';Sprefix:='';
  56. count:=m-inizio+1;
  57. Sdainv:=copy(x,inizio, count);
  58. Sprefix:=copy(x, 1, inizio-1);
  59. stringainvertita:=ReverseString(Sdainv);
  60. Sconinv:=Sprefix+stringainvertita;
  61. end;
  62.  
  63. Procedure nextPermutation (var P : ansistring) ;
  64. var pivot:qword;
  65. (* Find the pivot index*)
  66. begin
  67. pivot := 0;
  68. for i := m - 1 downto 1 do
  69. if (P[i] < P[i + 1]) then begin pivot:=i; break; end;
  70. (*If pivot point does not exist, exit dalla procedura perchè, essendo l'array ordinato, ho già trovato tutte le permutazioni*)
  71. if pivot = 0 then begin uscita:=true; exit; end;
  72. (* find the element from the right that is greater than pivot*)
  73. for i := m downto pivot+1 do
  74. if (P[i] > P[pivot]) then begin scambiach (P[i], P[pivot]); break; end;
  75. (*Reverse the elements from pivot + 1 to the end to get the next permutation*)
  76. if pivot+1=m then Sconinv:=P
  77. else inversione (P, pivot+1, Sconinv);
  78. end;
  79.  
  80. begin
  81. (*assign(input, 'input.txt'); reset(input);
  82.   assign(output, 'output.txt'); rewrite(output);*)
  83. readln(m,k);
  84. readln(cifre);
  85. for i:=1 to m do arrnum[i]:=copy(cifre,i,1); (*trasformo la stringa di input in un array per ordinarlo in modo crescente*)
  86. ordinamento (1,m,arrnum, true);
  87. stringaordinata:=''; Sconinv:='';
  88. for i:=1 to m do stringaordinata:=stringaordinata+arrnum[i]; (*ritorno da array a stringa per applicare le procedure*)
  89. stringadainvertire:=stringaordinata;
  90. uscita:=false; w:=1;
  91. while (uscita=false) do
  92. begin
  93. if Stringadainvertire[1]<>'0' then (*se la stringadainvertire rappresenta un numero che non inizia con zero lo trasformo in numero e lo memorizzo nell'array arrpsw*)
  94. begin
  95. val(Sconinv,arrpsw[w],cod);
  96. w:=w+1;
  97. end;
  98. nextPermutation(stringadainvertire);
  99. stringadainvertire:=Sconinv;
  100. end;
  101. for i:=1 to w -2 do (*confronto a coppie tutti i numeri di arrpsw per trovare la coppia la cui differenza è multipla di k*)
  102. for j:=w-2 downto 1 do
  103. if ((arrpsw[j] mod k-arrpsw[i] mod k) mod k =0) and ((arrpsw[j] <> arrpsw[i] )) then
  104. begin
  105. writeln(arrpsw[i]); writeln(arrpsw[j]);
  106. exit;
  107. end;
  108. writeln('-1');
  109.  
  110. end.
Success #stdin #stdout 0s 5324KB
stdin
6 3
123042
stdout
102234
432201