Advertisement
karlicoss

good old days

Sep 1st, 2011
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.18 KB | None | 0 0
  1. program o247;
  2. {$APPTYPE CONSOLE}
  3. var n,m,i,s:integer;
  4. b:array[1..1000] of integer;
  5. a:array[1..1000] of integer;
  6. procedure QSort(left, right : integer);
  7. var m, i, j, t : integer;
  8. begin
  9.   m := a[(left+right) div 2];
  10.   i := left; j := right;
  11.   repeat
  12.     while a[i] > m do inc(i); {ïåðâûé while}
  13.     while a[j] < m do dec(j); {âòîðîé while}
  14.     if i <= j then
  15.     begin
  16.       t := a[i]; a[i] := a[j]; a[j] := t;
  17.       t:=b[i];b[i]:=b[j];b[j]:=t;
  18.       inc(i); dec(j);
  19.     end;
  20.   until i > j;
  21.   if j > left then QSort(left, j);
  22.   if i < right then QSort(i, right);
  23. end;
  24. function delit(k:integer):integer;
  25. var d,i,j:integer;
  26. begin
  27. d:=2;
  28. for i := 2 to (k div 2)+1 do if k mod i=0 then inc(d);
  29. delit:=d;
  30. end;
  31. begin
  32. reset(input,'input.txt');
  33. rewrite(output,'output.txt');
  34. readln(n,m);
  35. a[1]:=1;
  36. a[2]:=2;
  37. for i:= 1 to n do b[i]:=i;  
  38. for i:=3 to n do a[i]:=delit(i);
  39. Qsort(1,n);
  40. s:=0;
  41. i:=1;
  42. while i<=m do
  43. begin
  44.   if a[i+1]<>a[i] then
  45.   begin
  46.   if a[i-1]=a[i] then  inc(s,b[i-1]) else inc(s,b[i]);
  47.   inc(i);
  48.   end
  49.   else
  50.   begin
  51.     while a[i+1]=a[i] do
  52.     begin
  53.       inc(i);
  54.       inc(m);
  55.     end;
  56.   end;
  57. end;
  58. write(s);  
  59. close(output);
  60. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement