Cod sursa(job #111971)

Utilizator taloibogdanTaloi Bogdan Cristian taloibogdan Data 2 decembrie 2007 18:26:28
Problema Ecuatie Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 7.29 kb
Program ecuatie;
Var f:text;
    a,b,c,k,d,i,l,j:longint;
    sd,x1,x2:real;
    p1,p2,q1,q2:array[1..100000000] of integer;
procedure intorc(i,j:longint);
var aux:longint;
begin
 aux:=p1[i];
 p1[i]:=p1[j];
 p1[j]:=aux;
 aux:=p2[i];
 p2[i]:=p2[j];
 p2[j]:=aux;
 aux:=q1[i];
 q1[i]:=q1[j];
 q1[j]:=aux;
 aux:=q2[i];
 q2[i]:=q2[j];
 q2[j]:=aux;
end;
function cauta:longint;
var j:longint;
begin
 cauta:=1;
 for j:=1 to l-1 do if (p1[l]=p1[j])and(p2[l]=p2[j])and(q1[l]=q1[j])and(q2[l]=q2[j]) then begin cauta:=0; exit; end;
end;
{procedure sort(s1,s2:longint);
var i1,j1,v1,v2:longint;
begin
  i1:=s1; j1:=s2;
  v1:=p1[(s1+s2) div 2];
  repeat
    while p1[i1]<v1 do i1:=i1+1;
    while v1<p1[j1] do j1:=j1-1;
    if i1<=j1 then
      begin
        v2:=a[i1]; a[i1]:=a[j1]; a[j1]:=v2;
        i1:=i1+1; j1:=j1-1;
      end;
  until i1>j1;
  if s1<j1 then sort(s1,j1);
  if i1<s2 then sort(i1,s2);
end;}
Begin
  assign(f,'ecuatie.in');
  reset(f);
  readln(f,a,b,c,k);
  close(f);
  assign(f,'ecuatie.out');
  rewrite(f);
  d:=sqr(b)-4*a*c;
  if (d<0){ or (sqrt(d)<>trunc(sqrt(d)))} then begin Writeln(f,'-1'); close(f); exit; end;
  sd:=sqrt(d);
  x1:=(-b+sd)/(2*a);
  x2:=(-b-sd)/(2*a);
  l:=0;
  for i:=1 to trunc(sqrt(abs(a))) do
     if a mod i=0 then
      if a>0 then
       begin



        if (-x1)*i=trunc((-x1)*i) then
          if (-x2)*(a div i)=trunc((-x2)*(a div i)) then
            begin
             inc(l);
             p1[l]:=i;
             p2[l]:=a div i;
             q1[l]:=trunc((-x1)*i);
             q2[l]:=trunc((-x2)*(a div i));
             if cauta=0 then dec(l);
            end;
        if (-x2)*i=trunc((-x2)*i) then
          if (-x1)*(a div i)=trunc((-x1)*(a div i)) then
            begin
             inc(l);
             p1[l]:=i;
             p2[l]:=a div i;
             q1[l]:=trunc((-x2)*i);
             q2[l]:=trunc((-x1)*(a div i));
             if cauta=0 then dec(l);
            end;



        if (-x1)*i=trunc((-x1)*i) then
          if (-x2)*(a div i)=trunc((-x2)*(a div i)) then
            begin
             inc(l);
             p1[l]:=-i;
             p2[l]:=-(a div i);
             q1[l]:=trunc(-(-x1)*i);
             q2[l]:=trunc(-(-x2)*(a div i));
             if cauta=0 then dec(l);
            end;
        if (-x2)*i=trunc((-x2)*i) then
          if (-x1)*(a div i)=trunc((-x1)*(a div i)) then
            begin
             inc(l);
             p1[l]:=-i;
             p2[l]:=-(a div i);
             q1[l]:=trunc(-(-x2)*i);
             q2[l]:=trunc(-(-x1)*(a div i));
             if cauta=0 then dec(l);
            end;



        if (-x1)*i=trunc((-x1)*i) then
          if (-x2)*(a div i)=trunc((-x2)*(a div i)) then
            begin
             inc(l);
             p2[l]:=i;
             p1[l]:=a div i;
             q2[l]:=trunc((-x1)*i);
             q1[l]:=trunc((-x2)*(a div i));
             if cauta=0 then dec(l);
            end;
        if (-x2)*i=trunc((-x2)*i) then
          if (-x1)*(a div i)=trunc((-x1)*(a div i)) then
            begin
             inc(l);
             p2[l]:=i;
             p1[l]:=a div i;
             q2[l]:=trunc((-x2)*i);
             q1[l]:=trunc((-x1)*(a div i));
             if cauta=0 then dec(l);
            end;



        if (-x1)*i=trunc((-x1)*i) then
          if (-x2)*(a div i)=trunc((-x2)*(a div i)) then
            begin
             inc(l);
             p2[l]:=-i;
             p1[l]:=-(a div i);
             q2[l]:=trunc(-(-x1)*i);
             q1[l]:=trunc(-(-x2)*(a div i));
             if cauta=0 then dec(l);
            end;
        if (-x2)*i=trunc((-x2)*i) then
          if (-x1)*(a div i)=trunc((-x1)*(a div i)) then
            begin
             inc(l);
             p2[l]:=-i;
             p1[l]:=-(a div i);
             q2[l]:=trunc(-(-x2)*i);
             q1[l]:=trunc(-(-x1)*(a div i));
             if cauta=0 then dec(l);
            end;



       end
             else
       begin



        if (-x1)*i=trunc((-x1)*i) then
          if (-x2)*(a div i)=trunc((-x2)*(a div i)) then
            begin
             inc(l);
             p1[l]:=-i;
             p2[l]:=-(a div i);
             q1[l]:=trunc(-(-x1)*i);
             q2[l]:=trunc(-(-x2)*(a div i));
             if cauta=0 then dec(l);
            end;
        if (-x2)*i=trunc((-x2)*i) then
          if (-x1)*(a div i)=trunc((-x1)*(a div i)) then
            begin
             inc(l);
             p1[l]:=-i;
             p2[l]:=-(a div i);
             q1[l]:=trunc(-(-x2)*i);
             q2[l]:=trunc(-(-x1)*(a div i));
             if cauta=0 then dec(l);
            end;



        if (-x1)*i=trunc((-x1)*i) then
          if (-x2)*(a div i)=trunc((-x2)*(a div i)) then
            begin
             inc(l);
             p1[l]:=i;
             p2[l]:=a div i;
             q1[l]:=trunc((-x1)*i);
             q2[l]:=trunc((-x2)*(a div i));
             if cauta=0 then dec(l);
            end;
        if (-x2)*i=trunc((-x2)*i) then
          if (-x1)*(a div i)=trunc((-x1)*(a div i)) then
            begin
             inc(l);
             p1[l]:=i;
             p2[l]:=a div i;
             q1[l]:=trunc((-x2)*i);
             q2[l]:=trunc((-x1)*(a div i));
             if cauta=0 then dec(l);
            end;



        if (-x1)*i=trunc((-x1)*i) then
          if (-x2)*(a div i)=trunc((-x2)*(a div i)) then
            begin
             inc(l);
             p2[l]:=-i;
             p1[l]:=-(a div i);
             q2[l]:=trunc(-(-x1)*i);
             q1[l]:=trunc(-(-x2)*(a div i));
             if cauta=0 then dec(l);
            end;
        if (-x2)*i=trunc((-x2)*i) then
          if (-x1)*(a div i)=trunc((-x1)*(a div i)) then
            begin
             inc(l);
             p2[l]:=-i;
             p1[l]:=-(a div i);
             q2[l]:=trunc(-(-x2)*i);
             q1[l]:=trunc(-(-x1)*(a div i));
             if cauta=0 then dec(l);
            end;



        if (-x1)*i=trunc((-x1)*i) then
          if (-x2)*(a div i)=trunc((-x2)*(a div i)) then
            begin
             inc(l);
             p2[l]:=i;
             p1[l]:=a div i;
             q2[l]:=trunc((-x1)*i);
             q1[l]:=trunc((-x2)*(a div i));
             if cauta=0 then dec(l);
            end;
        if (-x2)*i=trunc((-x2)*i) then
          if (-x1)*(a div i)=trunc((-x1)*(a div i)) then
            begin
             inc(l);
             p2[l]:=i;
             p1[l]:=a div i;
             q2[l]:=trunc((-x2)*i);
             q1[l]:=trunc((-x1)*(a div i));
             if cauta=0 then dec(l);
            end;



       end;
  for i:=1 to l-1 do
     for j:=i+1 to l do
        if p1[i]>p1[j] then intorc(i,j)
                       else if p1[i]=p1[j] then if q1[i]>q1[j] then intorc(i,j);

  if l<k then begin Writeln(f,'-1'); close(f); exit; end;

  {for i:=1 to l do Writeln(f,p1[i],' ',q1[i],' ',p2[i],' ',q2[i]);}
  Write(f,'(');
  if p1[k]=-1 then Write(f,'-')
              else if p1[k]<>1 then Write(f,p1[k]);
  Write(f,'x');
  if q1[k]>=0 then Write(f,'+');
  Write(f,q1[k],')(');
  if p2[k]=-1 then Write(f,'-')
              else if p2[k]<>1 then Write(f,p2[k]);
  Write(f,'x');
  if q2[k]>=0 then Write(f,'+');
  Write(f,q2[k],')');
  close(f);
End.