Cod sursa(job #111845)

Utilizator ionescu88alex ionescu ionescu88 Data 1 decembrie 2007 23:42:11
Problema Ecuatie Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 4.59 kb
type elem=record
       q,p:int64;
     end;
var fi,fo:text;
    solutii:array[1..700000]of elem;
    A,B,C,K,ct:longint;
    DELTA,p2,q2:int64;
    X1,X2:extended;
    epsilon:real;
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;
function verif(d:longint; x:extended):boolean;
var t1,t2:extended;
begin
  if x=X1 then t2:=-X2*(A/d)
          else t2:=-X1*(A/d);
  t1:=-x*d;
  if (abs(t1-trunc(t1))<=epsilon)and(abs(t2-trunc(t2))<=epsilon) then verif:=true
                                                                 else verif:=false;
end;
procedure Introduce(d:longint; x:extended);
begin
  inc(ct);
  solutii[ct].p:=d;
  solutii[ct].q:=trunc(-x*d);
end;
procedure Afla_Div(A:longint);
var i:longint;
begin
     for i:=1 to trunc(sqrt(abs(A))) do
        if A mod i = 0 then
           begin
             if (i<>(A div i))and(X1<>X2) then
               begin
                 if (verif(i,X1)) then
                   begin
                    Introduce(i,X1);
                    Introduce(A div i,X2); end;

                 if (verif(i,X2)) then
                   begin
                    Introduce(i,X2);
                    Introduce(A div i,X1); end;

                 if (verif(-i,X1)) then
                   begin
                    Introduce(-i,X1);
                    Introduce((-1)*(A div i),X2); end;

                 if (verif(-i,X2)) then
                   begin
                    Introduce(-i,X2);
                    Introduce((-1)*(A div i),X1); end;
               end
             else
              if (i=(A div i))and(X1=X2) then
                begin
                  if verif(i,X1) then
                    Introduce(i,X1);

                  if verif(-i,X1) then
                    Introduce(-i,X1);
                end
              else
               if (i=(A div i))and(X1<>X2) then
                 begin
                   if verif(i,X1) then
                     begin
                       Introduce(i,X1);
                       Introduce(i,X2); end;

                   if verif(-i,X1) then
                     begin
                       Introduce(-i,X1);
                       Introduce(-i,X2); end;
                 end
               else
                 if (X1=X2)and(i<>(A div i)) then
                   begin
                     if verif(i,X1) then
                       begin
                         Introduce(i,X1);
                         Introduce(A div i,X1); end;

                     if verif(-i,X1) then
                       begin
                         Introduce(-i,X1);
                         Introduce((-1)*(A div i),X1);
                       end;
                   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,')')
              else write(fo,q2,')');
end;
procedure rezolv;
begin
   read(fi,A,B,C,K);
   DELTA:=B*B-4*A*C;
   epsilon:=0.000000001;
   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:=round((A/solutii[k].q)*(X1*X2));
          Afis;
        end;
end;
var i:longint;
begin
   assign(fi,'ecuatie.in'); reset(fi);
   assign(fo,'ecuatie.out'); rewrite(fo);
   rezolv;
   writeln(fo);
{   for i:=1 to ct do
      write(fo,solutii[i].p,' ');}
   close(fi);
   close(fo);
end.