Cod sursa(job #163238)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 21 martie 2008 19:27:02
Problema Ecuatie Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.3 kb
type patru = record
     p1,p2,q2,q1: longint;
     end;
var a,b,c,k,i,z,d,nr : longint;
    v : array[0..10000] of patru;
    f,g : text;
    x1,x2 : real;
procedure calcul_x1x2;
var delta : real;
begin
  delta:=sqr(b)-4*a*c;
  x1:=(-b-(sqrt(delta)))/(2*a);
  x2:=(-b+(sqrt(delta)))/(2*a);
end;

procedure baga_vector(d : longint);
var q1,q2 : real;
begin
  q1:=-x1*d;
  q2:=-x2*(a div d);
  if (q1=trunc(q1))and(q2=trunc(q2))then
begin
  inc(z);
  v[z].p1:=d;
  v[z].q1:=trunc(q1);
  v[z].p2:=a div d;
  v[z].q2:=trunc(q2);
end;
  q1:=-x2*d;
  q2:=-x1*(a div d);
  if (q1=trunc(q1))and(q2=trunc(q2))then
begin
  inc(z);
  v[z].p1:=d;
  v[z].q1:=trunc(q1);
  v[z].p2:=a div d;
  v[z].q2:=trunc(q2);
end;
end;

procedure sort(l,r : longint);
var i,j : longint;
    aux,x,y : longint;
begin
  i:=l;j:=r;x:=v[(l+r)div 2].p1;
  y:=v[(l+r)div 2].q1;
  repeat
    while (v[i].p1<x)or((v[i].p1=x)and(v[i].q1<y))do inc(i);
    while (x<v[j].p1)or((v[j].p1=x)and(y<v[j].q1))do dec(j);
    if i<=j then
    begin
      aux:=v[i].p1;v[i].p1:=v[j].p1;v[j].p1:=aux;
      aux:=v[i].q1;v[i].q1:=v[j].q1;v[j].q1:=aux;
      aux:=v[i].p2;v[i].p2:=v[j].p2;v[j].p2:=aux;
      aux:=v[i].q2;v[i].q2:=v[j].q2;v[j].q2:=aux;
      inc(i);dec(j);
    end;
  until i>j;
  if l<j then sort(l,j);
  if i<r then sort(i,r);
end;

begin
assign(f,'ecuatie.in');reset(f);
assign(g,'ecuatie.out');rewrite(g);
read(f,a,b,c,k);
calcul_x1x2;
for d:=1 to trunc(sqrt(a)) do
    if a mod d=0 then
    begin
      baga_vector(d);
      baga_vector(-d);
      baga_vector(a div d);
      baga_vector(-a div d);
    end;
sort(1,z);
nr:=0;i:=0;
while nr<k do
begin
inc(i);
    if ((v[i].p1<>v[i-1].p1)and(nr<k)) then inc(nr)
       else if (v[i].q1<>v[i-1].q1)and(nr<k) then inc(nr);
end;
{for i:=1 to z do
begin   }
   if (v[i].p1>1)or(v[i].p1<-1) then write(g,'(',v[i].p1,'x')
        else if v[i].p1=-1 then write(g,'(-x')
        else write(g,'(x');
      if v[i].q1<0 then write(g,v[i].q1,')')
      else write(g,'+',v[i].q1,')');

      if (v[i].p2>1)or(v[i].p2<-1) then write(g,'(',v[i].p2,'x')
        else if v[i].p2=-1 then write(g,'(-x')
        else write(g,'(x');
      if v[i].q2<0 then write(g,v[i].q2,')')
      else write(g,'+',v[i].q2,')');
      writeln(g);
{end;}
close(g);
end.