Cod sursa(job #109948)

Utilizator vanila_CPPIonescu Victor Cristian vanila_CPP Data 25 noiembrie 2007 12:59:27
Problema Ecuatie Scor 0
Compilator fpc Status done
Runda preONI 2008, Runda 1, Clasa a 9-a Marime 4.83 kb
program ecuatie;
var f,g:text;
        a,b,c,k:longint;
        cx1,cc1,cx2,cc2:longint;
        cmd,nrpos:longint;


procedure citire;
begin
        assign(f,'ecuatie.in');reset(f);
        assign(g,'ecuatie.out');rewrite(g);
        readln(f,a,b,c,k);
        close(f);
end;


function cmmdc(x,y:longint):longint;
var r:longint;
begin
        while (y<>0) do
                begin
                        r:=x mod y;
                        x:=y;
                        y:=r;
                end;
        cmmdc:=x;
end;




procedure afla_si_imparte;
begin
        cmd:=cmmdc(a,b);
        cmd:=cmmdc(cmd,c);
        a:=a div cmd;
        b:=b div cmd;
        c:=c div cmd;
        cx1:=a;
        cc1:=c;
        cx2:=1;
        cc2:=1;
        nrpos:=1;
        if (cc1*cx2+cc2*cx1)=b then inc(nrpos);
        cc1:=-c;
        cc2:=-1;
        if (cc1*cx2+cc2*cx1)=b then inc(nrpos);
        cc1:=c;
        cc2:=1;
        if (nrpos=0) then
                begin
                        writeln(g,-1);
                        close(g);
                        halt;
                end;
        if nrpos=1 then
                if (cc1*cx2+cc2*cx1)<>b then
                        begin
                                cc1:=-c;
                                cc2:=-1;
                        end;
end;


function min(x,y:longint):longint;
begin
        if x<y then min:=x else min:=y;
end;

function max(x,y:longint):longint;
begin
        if x>y then max:=x else max:=y;
end;


procedure solve;
var cont:longint;
        diviz,div2:longint;
        pcx1,pcc1,pcx2,pcc2:longint;
        ok:boolean;
begin
        cont:=0;
        for diviz:=min(cmd,-cmd) to max(cmd,-cmd) do
                if (diviz<>0)and(cmd mod diviz=0) then
                        begin
                                cont:=cont+nrpos;
                                if nrpos=2 then
                                        if cont=k then
                                                begin
                                                        ok:=true;
                                                        cc1:=-c;
                                                        cc2:=-1;
                                                end else
                                        if cont=k+1 then
                                                begin
                                                        ok:=true;
                                                end else
                                        ok:=false else
                                if cont=k then ok:=true else
                                ok:=false;
                                if ok then
                        begin
                                if (nrpos=2)and(cont=k+1) then
                                        begin
                                                cc1:=min(cc1,-cc1);
                                                cc2:=c div cc1;
                                        end else
                                if (nrpos=2)and(cont=k+1) then
                                        begin
                                                cc1:=max(cc1,-cc1);
                                                cc2:=c div cc1;
                                        end;
                                div2:=cmd div diviz;
                                pcx1:=cx1*diviz;
                                pcc1:=cc1*diviz;
                                pcx2:=cx2*div2;
                                pcc2:=cc2*div2;
                                write(g,'(');
                                if pcx1=-1 then
                                        write(g,'-x') else
                                if pcx1=1 then
                                        write(g,'x') else
                                write(g,pcx1,'x');
                                if pcc1>0 then
                                        write(g,'+',pcc1) else
                                write(g,pcc1);
                                write(g,')(');
                                if pcx2=-1 then
                                        write(g,'-x') else
                                if pcx2=1 then
                                        write(g,'x') else
                                write(g,pcx2,'x');
                                if pcc2>0 then
                                        write(g,'+',pcc2) else
                                write(g,pcc2);
                                write(g,')');

                                writeln(g);
                                close(g);
                                halt;
                        end;
                        end;
        writeln(g,-1);
        close(g);
end;


begin
        citire;
        afla_si_imparte;
        solve;
end.