Cod sursa(job #109474)

Utilizator CezarMocanCezar Mocan CezarMocan Data 25 noiembrie 2007 11:17:09
Problema Ecuatie Scor 100
Compilator fpc Status done
Runda preONI 2008, Runda 1, Clasele 5-8 Marime 3.27 kb
type pereche=record
                x,y:longint;
                end;

type vector=array[1..8000] of pereche;
var p,q:vector;
   i,j,a,b,c,k,np,nq,nr:longint;
   aux:pereche;

procedure qsort(ls,ld:longint;var v:vector);
var i,j:longint;
begin
  i:=ls;j:=ld;
  while true do begin
    while (v[i].x<=v[j].x)and(i<>j) do inc(i);
    if i=j then break;
    aux:=v[i];v[i]:=v[j];v[j]:=aux;dec(j);
    while (v[i].x<=v[j].x)and(i<>j) do dec(j);
    if i=j then break;
    aux:=v[i];v[i]:=v[j];v[j]:=aux;inc(i);
  end;
  if j-1>ls then qsort(ls,j-1,v);
  if j+1<ld then qsort(j+1,ld,v);
end;


begin
assign(input,'ecuatie.in');reset(input);
assign(output,'ecuatie.out');rewrite(output);
readln(a,b,c,k);
for i:=1 to trunc(sqrt(abs(a))) do
        begin
        if a mod i=0 then
                begin
                inc(np);
                p[np].x:=i;
                p[np].y:=a div i;
                inc(np);
                p[np].x:=-i;
                p[np].y:=-(a div i);
                if i<>a div i then
                        begin
                        inc(np);
                        p[np].y:=i;
                        p[np].x:=a div i;
                        inc(np);
                        p[np].y:=-i;
                        p[np].x:=-(a div i);
                        end;
                end;
        end;
for i:=1 to trunc(sqrt(abs(c))) do
        begin
        if c mod i=0 then
                begin
                inc(nq);
                q[nq].x:=i;
                q[nq].y:=c div i;
                inc(nq);
                q[nq].x:=-i;
                q[nq].y:=-(c div i);
                if i<>c div i then
                        begin
                        inc(nq);
                        q[nq].y:=i;
                        q[nq].x:=c div i;
                        inc(nq);
                        q[nq].y:=-i;
                        q[nq].x:=-(c div i);
                        end;
                end;
        end;
qsort(1,np,p);
qsort(1,nq,q);
nr:=0;
for i:=1 to np do
        for j:=1 to nq do
                begin
                if p[i].x*q[j].y+p[i].y*q[j].x=b then
                        inc(nr);
                if nr=k then
                        begin
                        write('(');
                        if p[i].x=-1 then
                                write('-')
                        else
                                if p[i].x<>1 then
                                        write(p[i].x);
                        write('x');
                        if q[j].x>=0 then
                                write('+');
                        write(q[j].x);
                        write(')(');
                        if p[i].y=-1 then
                                write('-')
                        else
                                if p[i].y<>1 then
                                        write(p[i].y);
                        write('x');
                        if q[j].y>=0 then
                                write('+');
                        write(q[j].y);
                        writeln(')');
                        close(output);
                        halt;
                        end;
                end;
writeln(-1);
close(input);close(output);
end.