Cod sursa(job #304797)

Utilizator FllorynMitu Florin Danut Flloryn Data 15 aprilie 2009 13:21:14
Problema Economie Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.51 kb
program pascal;
var f,g:text;    t:array[1..100000] of integer; x,p:array[1..100] of longint;
                 max,i,k,j,n,aux,c:longint;
  procedure citire;
  begin
  assign(f,'economie.in'); reset(f);
  assign(g,'economie.out'); rewrite(g);
  readln(f,n);
  max:=0;
  for i:=1 to n do
    begin
    readln(f,x[i]);
    if x[i]>max then max:=x[i];
    end;
  close(f);
  end;

  procedure sort(l,r:longint);
  var i,j,pivot:longint;
  begin
    i:=1; j:=r; pivot:=x[random(r-1)+1];
    repeat
    while x[i]<pivot do i:=i+1;
    while pivot<x[j] do j:=j-1;
    if i<=j then
            begin
            aux:=x[i];
            x[i]:=x[j];
            x[j]:=aux;
            i:=i+1; j:=j-1;
            end;
    until i>j;
    if i<r then sort(i,r);
    if l<j then sort(l,j);
  end;

  procedure sorte;
  begin
  k:=1; p[k]:=x[1];
  for i:=1 to max do
  if (i mod p[k]=0) then t[i]:=1
           else
             begin
             c:=i+p[k];
             if (t[i]=1) and (c<=max) then t[i+p[k]]:=1;
             end;

  for j:=2 to n do
  if  t[x[j]]=0 then
       begin
       k:=k+1; p[k]:=x[j];
       for i:=1 to max do
       if (i mod p[k]=0) then t[i]:=1
           else
             begin
             c:=i+p[k];
             if (t[i]=1) and (c<=max) then t[c]:=1;
             end;
       end;
  end;

  procedure afisare;
  begin
  writeln(g,k);
  for i:=1 to k do writeln(g,p[i]);
  close(g);
  end;

begin
citire;
sort(1,n);
sorte;
afisare;
exit;
end.