Cod sursa(job #132755)

Utilizator you_reheroMihai Gojinetchi you_rehero Data 6 februarie 2008 16:17:28
Problema Economie Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.15 kb
var fi,fo:text;
    n,i,j,ct:longint;
    v,sol:array[1..50010]of word;
    s:array[1..60000]of byte;
function part(st,dr:longint):longint;
var i,j,aux,s:longint;
begin
  i:=st; j:=dr; s:=-s;
  while i<j do
    begin
      if v[i]>v[j] then
        begin
          aux:=v[i];
          v[i]:=v[j];
          v[j]:=aux;
        end;
      if s=1 then inc(i)
             else dec(j);
    end;
  part:=i;
end;
procedure qsort(st,dr:longint);
var p:longint;
begin
  if st<dr then
    begin
      p:=part(st,dr);
      qsort(st,p-1);
      qsort(p+1,dr);
    end;
end;
begin
  assign(fi,'economie.in'); reset(fi);
  assign(fo,'economie.out'); rewrite(fo);
  read(fi,n);
  for i:=1 to n do
    read(fi,v[i]);
  qsort(1,n);
  ct:=0;
  for i:=1 to n do
    if s[v[i]]=0 then
      begin
        inc(ct);
        sol[ct]:=v[i];
        j:=1;
        while j*v[i]<v[n] do
          begin
            s[v[i]*j]:=1;
            inc(j);
          end;
        for j:=1 to v[n]-v[i] do
          if s[j]=1 then s[j+v[i]]:=1;
      end;
  writeln(fo,ct);
  for i:=1 to ct do
    writeln(fo,sol[i]);
  close(fi);
  close(fo);
end.