Cod sursa(job #114727)

Utilizator Pepelea_FlaviuFlaviu Pepelea Pepelea_Flaviu Data 15 decembrie 2007 16:49:53
Problema Pairs Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.3 kb
var fi,fo:text;
    i,rez,n,x,max,ct,j,nr_div:longint;
    nr:array[1..1000000]of byte;
    m:array[1..1000000]of longint;
function prim(nr:longint):boolean;
var i:longint;
begin
  i:=2; nr_div:=0; prim:=true;
  while i<=nr do
    if nr mod i=0 then
      begin
       ct:=0;
       while nr mod i=0 do
        begin
          nr:=nr div i;
          inc(ct);
          if ct>1 then begin prim:=false; exit; end;
        end;
       inc(nr_div);
       inc(i);
      end
    else inc(i);
    if nr_div>1 then prim:=false;
end;
procedure verif(i:longint);
var ok:boolean;
begin
  ok:=prim(i);
  if ok=true then rez:=rez+(m[i]*(m[i]-1) shr 1)
    else
      if nr_div>1 then
        if ct=1 then
          if nr_div and 1 = 1 then rez:=rez+(m[i]*(m[i]-1)) shr 1
                              else rez:=rez-(m[i]*(m[i]-1)) shr 1;
end;
begin
  assign(fi,'pairs.in'); reset(fi);
  assign(fo,'pairs.out'); rewrite(fo);
  readln(fi,n);
  max:=-maxint;
  rez:=0;
  for i:=1 to n do
    begin
      readln(fi,x);
      nr[x]:=1;
      if x>max then max:=x; end;
  for i:=2 to max do
    begin
      for j:=1 to max div i do
         if nr[i*j]=1 then inc(m[i]);
      if m[i]>0 then verif(i);
    end;
  rez:=n*(n-1) shr 1 - rez;
  writeln(fo,rez);
  close(fi);
  close(fo);
end.