Cod sursa(job #109739)

Utilizator sandu2508Grigoroi Alexandru sandu2508 Data 25 noiembrie 2007 12:35:32
Problema Ecuatie Scor 0
Compilator fpc Status done
Runda preONI 2008, Runda 1, Clasele 5-8 Marime 2.1 kb
program ecuatie;

var
a, b, c, k, d, q1, q2, i, j, l, q: longint;
f: text;
s, p1, p2, r1, r2: array[1..1000] of longint;

begin
 j:= 1;
 q:= 1;
 assign(f, 'ecuatie.in');
 reset(f);
 readln(f, a, b, c, k);
 close (f);
 d:= b*b - 4*a*c;
 q1:= (trunc(sqrt(d) -b)) div (2*a) ;
 q2:= (trunc(0- sqrt(D)-b)) div (2*a) ;
 for i:= 1 to a do
  if a mod i = 0 then
   begin
    s[j]:=i;
    j:= j+1;
   end;
 l:= j-1;
 for i:= l downto 1 do
  begin
   j:= 0-s[i];
   p1[q]:= j;
   p2[q]:= a div j;
   r1[q]:= q1 * j;
   r2[q]:= q2 * (a div j);
   q:= q+1;
  end;
 for i:= 1 to l do
  begin
   p1[q]:= s[i];
   p2[q]:= a div s[i];
   r1[q]:= q1 * s[i];
   r2[q]:= q2 * (a div s[i]);
   q:= q + 1;
  end;
 for i := q to 2*q do
  begin
   p1[i]:=p2[i-q];
   r1[i]:=r2[i-q];
   p2[i]:=p1[i-q];
   r2[i]:=r1[i-q];
  end;
 for i := 1 to 2*q do
  for j := i+1 to 2*q do
   begin
    if p1[i] > p1[j] then
     begin
      d:=p1[i];
      l:=p2[i] ;
      p1[i]:=p1[j];
      p2[i]:=p2[j];
      p1[j]:=d;
      p2[j]:=l;
      d:=r1[i];
      l:=r2[i] ;
      r1[i]:=r1[j];
      r2[i]:=r2[j];
      r1[j]:=d;
      r2[j]:=l;
     end;
    if p1[i] = p1[j] then
     if r1[i] < r1[j] then
      begin
      d:=p1[i];
      l:=p2[i];
      p1[i]:=p1[j];
      p2[i]:=p2[j];
      p1[j]:=d;
      p2[j]:=l;
      d:=r1[i];
      l:=r2[i];
      r1[i]:=r1[j];
      r2[i]:=r2[j];
      r1[j]:=d;
      r2[j]:=l;
     end;
   end;
 i:=p1[k];
 j:=p2[k];
 q1:=r1[k];
 q2:=r2[k];
 assign(f, 'ecuatie.out');
 rewrite(f);
 if (i <> 1) and (i <> -1) then
  write(f, '(', i, 'x');
 if i = 1 then
  write(f, '(', 'x');
 if i= -1 then
  write(f, '(', '-', 'x');
 if q1 > 0 then
  write(f, '-', q1);
 if q1 < 0 then
  begin
   q1:= abs(q1);
   write(f, '+', q1);
  end;
 write(f, ')');
 if (j<>1) and (j<> -1)then
  write(f, '(', j, 'x');
 if j = 1 then
  write (f, '(', 'x');
 if j = - 1 then
  write (f, '(', '-', 'x');
 if q2 > 0 then
  write (f, '-', q2);
 if q2 < 0 then
  begin
   q2:= abs(q2);
   write(f, '+', q2);
  end;
 write(f, ')');
 close(f);
end.