Cod sursa(job #114810)

Utilizator ionescu88alex ionescu ionescu88 Data 15 decembrie 2007 22:51:37
Problema Pairs Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.31 kb
var fi,fo:text;
    rez,n,x,max,ct,nr_div:int64;
    i,j:longint;
    nr:array[1..1100000]of byte;
    m:array[1..1100000]of longint;
function prim(nr:int64):boolean;
var i,vari:longint;
begin
  i:=2; nr_div:=0; prim:=true; ct:=1;
  while i<=trunc(sqrt(nr)) do
    begin
      if nr mod (i*i)=0 then begin ct:=2; prim:=false; exit; end;
      if nr mod i=0 then begin inc(nr_div); nr:=nr div i; end;
      inc(i);
    end;
    if nr>1 then inc(nr_div);
    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
   if (sqrt(i)<>trunc(sqrt(i)))or(i and 2 <> 0) then
    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.