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,w, calcolomod, psw:qword;
  6. count : qword;
  7. cod : longint;
  8. cifre, stringadainvertire, stringainvertita, stringaordinata, Sconinv : Ansistring;
  9. arrnum :array [1..MAX] of string[1];
  10. modpsw :array [1..MAX] of Ansistring; (*in questo array l'indice indica il resto della divisione per k; nell'array l'elemento di posto i è la stringa il cui valore numerico diviso per k ha resto i*)
  11. pot : array[0..MAX] of qword;
  12. uscita:boolean;
  13.  
  14. procedure scambia (var x,y: string);
  15. var t:string;
  16. begin
  17. t:=x;
  18. x:=y;
  19. y:=t;
  20. end;
  21. procedure scambiach (var x,y: char);
  22. var t:char;
  23. begin
  24. t:=x;
  25. x:=y;
  26. y:=t;
  27. end;
  28. Procedure ordinamento (estremoi,estremos: qword; var v : elenco; ordinato:boolean);
  29. var inf, sup, medio:qword;
  30. pivot :string[1];
  31. begin
  32. inf:=estremoi;
  33. sup:=estremos;
  34. medio:= (estremoi+estremos) div 2;
  35. pivot:=v[medio];
  36. repeat
  37. if (ordinato) then
  38. begin
  39. while (v[inf]<pivot) do inf:=inf+1;
  40. while (v[sup]>pivot) do sup:=sup-1;
  41. end;
  42. if inf<=sup then
  43. begin
  44. scambia(v[inf],v[sup]);
  45. inf:=inf+1;
  46. sup:=sup-1;
  47. end;
  48. until inf>sup;
  49. if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
  50. if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
  51. end;
  52.  
  53. Procedure inversione (x:Ansistring; inizio:qword; var Sconinv:ansistring);
  54. var Sdainv,Sprefix : ansistring;
  55. begin
  56. Sdainv:='';Sprefix:='';
  57. count:=m-inizio+1;
  58. Sdainv:=copy(x,inizio, count);
  59. Sprefix:=copy(x, 1, inizio-1);
  60. stringainvertita:=ReverseString(Sdainv);
  61. Sconinv:=Sprefix+stringainvertita;
  62. end;
  63.  
  64. Procedure nextPermutation (var P : ansistring) ;
  65. var pivot:qword;
  66. (* Find the pivot index*)
  67. begin
  68. pivot := 0;
  69. for i := m - 1 downto 1 do
  70. if (P[i] < P[i + 1]) then begin pivot:=i; break; end;
  71. (*If pivot point does not exist, exit dalla procedura perchè, essendo l'array ordinato, ho già trovato tutte le permutazioni*)
  72. if pivot = 0 then begin uscita:=true; exit; end;
  73. (* find the element from the right that is greater than pivot*)
  74. for i := m downto pivot+1 do
  75. if (P[i] > P[pivot]) then begin scambiach (P[i], P[pivot]); break; end;
  76. (*Reverse the elements from pivot + 1 to the end to get the next permutation*)
  77. if pivot+1=m then Sconinv:=P
  78. else inversione (P, pivot+1, Sconinv);
  79. end;
  80.  
  81. procedure calcolapsw (ss :ansistring);
  82. var h :qword;
  83. begin
  84. psw:= 0;
  85. for h:= 0 to m - 1 do
  86. begin
  87. psw := psw+pot[h] * (ord(ss[m - h]) -48);
  88. calcolomod:=psw mod k;
  89. end;
  90. writeln('psw', psw) ;
  91. end;
  92.  
  93. begin
  94. (*assign(input, 'input.txt'); reset(input);
  95.   assign(output, 'output.txt'); rewrite(output);*)
  96. readln(m,k);
  97. readln(cifre);
  98. 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*)
  99. ordinamento (1,m,arrnum, true);
  100. stringaordinata:=''; Sconinv:='';
  101. pot[0]:=1;
  102. for i:=1 to m -1 do pot[i]:=10*pot[i-1];
  103. for i:=0 to k-1 do modpsw[i]:='';
  104. for i:=1 to m do stringaordinata:=stringaordinata+arrnum[i]; (*ritorno da array a stringa per applicare le procedure*)
  105. stringadainvertire:=stringaordinata;
  106. uscita:=false; w:=1;
  107. while stringadainvertire[w]='0' do w:=w+1; (*tolgo gli zeri iniziali, mettendo in prima posizione la prima cifra diversa da zero*)
  108. scambiach(stringadainvertire[1], stringadainvertire[w]);
  109. while (uscita=false) do
  110. begin
  111. calcolapsw(stringadainvertire);
  112. if modpsw[calcolomod]='' then modpsw[calcolomod]:=stringadainvertire
  113. else begin writeln(stringadainvertire); writeln(modpsw[calcolomod]); uscita:=true; exit; end;
  114. w:=w+1;
  115. nextPermutation(stringadainvertire);
  116. stringadainvertire:=Sconinv;
  117. end;
  118. writeln('-1');
  119. end.
Success #stdin #stdout 0.03s 5308KB
stdin
3 4
120
stdout
psw102
psw120
psw201
psw210
210
102