Cod sursa(job #118487)

Utilizator m123pop manu m123 Data 26 decembrie 2007 10:04:11
Problema Ecuatie Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.86 kb
Program ecuatie;{pre ONI 2008 runda1}
label 1;
type
elem=record
      p1,q1,p2,q2:longint;
      end;
var f,g:text;
a,b,c,p1,p2,q1,q2,k,delta,i,n,d,ok,j,d1,l:longint;
x1,x2,y1,y2:real;
s:array[1..1000] of elem;
x:elem;
begin
assign(f,'ecuatie.in');reset(f);
assign(g,'ecuatie.out');rewrite(g);
read(f,a,b,c,k);
delta:=b*b-4*a*c;
if delta <0 then begin writeln(g,-1); goto 1;end
            else if sqrt(delta)<>trunc(sqrt(delta))
                     then begin writeln(g,-1); goto 1;end;
x1:=(-b+sqrt(delta))/(2*a);
x2:=(-b-sqrt(delta))/(2*a);
for d:=-a to a do
   if d<>0 then
   if a mod d =0 then begin
                       p1:=d;p2:=a div d;
                       y1:=-x1*d;y2:=-x2*p2;
                      if( trunc(y1)=y1) and (trunc(y2)=y2) then
                           begin
                           q1:=trunc(y1);q2:=trunc(y2);
                           ok:=0;
                           for i:=1 to n do
                             if (s[i].p1=p1) and(s[i].p2=p2) and
                                  (s[i].q1=q1) and (s[i].q2=q2)
                                then ok:=1;
                           if ok=0 then begin
                               n:=n+1; s[n].p1:=p1;s[n].q1:=q1;
                               s[n].p2:=p2;s[n].q2:=q2
                               end;
                           end;
                     y1:=-x2*d;y2:=-x1*p2;
                      if( trunc(y1)=y1) and (trunc(y2)=y2) then
                           begin
                           q1:=trunc(y1);q2:=trunc(y2);
                           ok:=0;
                           for i:=1 to n do
                             if (s[i].p1=p1) and(s[i].p2=p2) and
                                  (s[i].q1=q1) and (s[i].q2=q2)
                                then ok:=1;
                           if ok=0 then begin
                               n:=n+1; s[n].p1:=p1;s[n].q1:=q1;
                               s[n].p2:=p2;s[n].q2:=q2
                               end;
                           end;
                       end;

for i:=1 to n-1 do

   if s[i].p1 =s[i+1].p1 then
        if s[i].q1>s[i+1].q1 then begin
                                  x:=s[i];s[i]:=s[i+1];s[i+1]:=x;
                                  end;

if n<k then writeln(g,-1)
       else      begin
                 write(g,'(');
                 if (s[k].p1 <>1) and (s[k].p1<>-1) then write(g,s[k].p1);
                 if s[k].p1=-1 then write(g,'-');
                 write(g,'x');
                  if s[k].q1>0 then write(g,'+');
                  write(g,s[k].q1,')(');
                  if (s[k].p2 <>1) and (s[k].p2<>-1) then write(g,s[k].p2);
                 if s[k].p2=-1 then write(g,'-');
                 write(g,'x');
                if s[k].q2>0 then write(g,'+');
                write(g, s[k].q2,')');
                 end;
1:close(f);close(g);
end.