Cod sursa(job #255691)

Utilizator madutzaAnton Madalina madutza Data 10 februarie 2009 11:46:41
Problema Fractii Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.37 kb
program fractii;
var f,g:text;
    p,pp:array[1..1000000]of longint;
    n,x,ci,ok,y,cj,c,s,max,i,j:longint;
begin
assign(f,'fractii.in');
assign(g,'fractii.out');
reset(f);
rewrite(g);
read(f,n);
y:=0;
for i:=2 to n do
  begin
  x:=2;
  while(i*x<=n)do
    begin
    if(p[x]=0)and(x>max)and ((trunc(sqrt(x)))*(trunc(sqrt(x)))<>x) then begin y:=y+1;
                                   pp[y]:=x;
                                   max:=x;
                               end;
    p[i*x]:=1;
    x:=x+1;
    end;
  end;
s:=s+(n-1)*2+1;
for i:=2 to n do begin
  if(p[i]=0)then
     begin
     c:=i-2;
     s:=s+c*2;
     for j:=i to n do
       if(j mod i<>0)and(p[j]<>0)then s:=s+2;
     end;
  if(p[i]<>0) then
     {for j:=i to n do
       if(p[j]<>0)then begin
       ci:=i;
       cj:=j;
       while(ci<>cj)do
         if(ci>cj)then ci:=ci-cj
                  else cj:=cj-ci;
       if(ci=1)then s:=s+2;
       end;    }
       for j:=i to n do
         if(p[j]<>0)then begin
         ci:=i;
         cj:=j;
         ok:=0;
         for c:=1 to y do
            if(ci mod pp[c]=0)and(cj mod pp[c]=0)then begin ok:=1;
                                                            break;
                                                            end;
       if(ok=0)then s:=s+2;
       end;
     end;
write(g,s);
close(f);
close(g);
end.