Cod sursa(job #109896)

Utilizator loriKis Levente Lorand lori Data 25 noiembrie 2007 12:54:20
Problema Ecuatie Scor 0
Compilator fpc Status done
Runda preONI 2008, Runda 1, Clasele 5-8 Marime 1.36 kb
var z,i,j,x1,x2,a,b,c,d,ad,dt,k,p1,q1,p2,q2,aa,ab,ac,r:longint;
    v:array [1..1000000] of longint;
begin
 assign(input,'ecuatie.in'); reset(input);
 assign(output,'ecuatie.out'); rewrite(output);
  readln(a,b,c,k); aa:=a; ab:=b; ac:=c; r:=aa mod ab;
  while r<>0 do begin
   aa:=ab; ab:=r; r:=aa mod ab;
  end;
  r:=ab mod ac;
  while r<>0 do begin
   ab:=ac; ac:=r; r:=ab mod ac;
  end;
  d:=ac; a:=a div d; b:=b div d; c:=c div d;
  dt:=b*b-4*a*c;
  if dt<0 then writeln(-1)
          else begin
   x1:=(-b+trunc(sqrt(dt))) div (2*a);
   x2:=(-b-trunc(sqrt(dt))) div (2*a);
   if x1<x2 then begin
    x1:=x1+x2; x2:=x1-x2; x1:=x1-x2;
   end;
   ad:=abs(d);
   for i:=-ad to ad do
    if (i<>0) and (ad mod i=0) then begin
     inc(j); v[j]:=i; inc(j); v[j]:=i;
    end;
   z:=v[k]; p1:=z; p2:=d div z; q1:=-x1*z; q2:=-x2*(d div z);
    if k mod 2<>0 then begin
     write('(',p1,'x');
     if q1>0 then write('+',q1)
             else write(q1);
     write(')(',p2,'x');
     if q2>0 then write('+',q2,')')
                  else write(q2,')');
     writeln;
    end
                 else begin
     write('(',p2,'x');
     if q2>0 then write('+',q2)
             else write(q2);
     write(')(',p1,'x');
     if q1>0 then write('+',q1,')')
                  else write(q1,')');
     writeln;
    end
  end;
 close(input); close(output);
end.