Cod sursa(job #111671)

Utilizator ionescu88alex ionescu ionescu88 Data 1 decembrie 2007 17:37:49
Problema Ecuatie Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 3.34 kb
type elem=record
       q,p:longint;
     end;
var fi,fo:text;
    solutii:array[1..200000000]of elem;
    i,A,B,C,K,p2,ct:longint;  DELTA:int64; X1,X2,q2:extended;
function part(st,dr:longint):longint;
var s:integer;                                               i,j:longint;
    aux:elem;
begin
     i:=st; j:=dr; s:=-1;
     while i<j do
        begin
          if (solutii[i].p>solutii[j].p)or((solutii[i].p=solutii[j].p)and(solutii[i].q>solutii[j].q)) then
             begin
               aux:=solutii[i]; solutii[i]:=solutii[j]; solutii[j]:=aux; s:=-s; 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;
procedure Introduce(d:longint; x:real);
begin
     inc(ct);
     solutii[ct].p:=d;
     solutii[ct].q:=trunc(-x*d);
end;

procedure Afla_Div(A:longint);
begin
     for i:=1 to trunc(sqrt(abs(A))) do
        if A mod i = 0 then
           begin
             if (i<>A/i)and(X1<>X2) then
               begin
                 Introduce(i,X1);
                 Introduce(i,X2);
                 Introduce(A div i,X1);
                 Introduce(A div i,X2);
                 Introduce(-i,X1);
                 Introduce(-i,X2);
                 Introduce(A div -i,X1);
                 Introduce(A div -i,X2);
               end
             else
              if (i=A/i)and(X1=X2) then
                begin
                  Introduce(i,X1);
                  Introduce(-i,X1);
                end
              else
               if (i=a/i)and(X1<>X2) then
                 begin
                   Introduce(i,X1);
                   Introduce(i,X2);
                   Introduce(-i,X1);
                   Introduce(-i,X2);
                 end
               else
                 if (X1=X2)and(i<>a/i) then
                   begin
                     Introduce(i,X1);
                     Introduce(A div i,X1);
                     Introduce(-i,X1);
                     Introduce(A div -i,X1);
                   end;
           end;
end;
procedure afis;
begin
     write(fo,'(');
     if solutii[K].p=1 then write(fo,'x')
       else
         if solutii[K].p=-1 then write(fo,'-x')
           else
             write(fo,solutii[K].p,'x');
     if solutii[K].q>=0 then write(fo,'+',solutii[K].q,')(')
                        else write(fo,solutii[k].q,')(');
     if p2=1 then write(fo,'x')
       else
        if p2=-1 then write(fo,'-x')
          else
            write(fo,p2,'x');
     if q2>=0 then write(fo,'+',q2:0:0,')')
              else write(fo,q2:0:0,')');
end;
begin
   assign(fi,'ecuatie.in'); reset(fi);
   assign(fo,'ecuatie.out'); rewrite(fo);
   read(fi,A,B,C,K);
   DELTA:=B*B-4*A*C;
   ct:=0;
   if DELTA>=0 then  begin
   if (trunc(sqrt(DELTA))=sqrt(DELTA)) then
      begin
           X1:=(-B+sqrt(DELTA))/(2*A);
           X2:=(-B-sqrt(DELTA))/(2*A);
           Afla_Div(A);
           qsort(1,ct);
      end; end;
   if ct<K then write(fo,'-1')
      else
        begin
          p2:=A div solutii[K].p;
          q2:=(trunc(X1)/ solutii[K].q)*trunc(X2)*A;
          Afis;
        end;
   close(fi);
   close(fo);
end.