Cod sursa(job #693354)

Utilizator alin_maidanmaidan alin alin_maidan Data 27 februarie 2012 11:50:53
Problema Multiplu Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.01 kb
program multiplu;
type vector=array[0..2000000] of longint;
var sr, rest,prec,cif:vector;
    a,b,d,ic,sfc,r,poz:longint;
    stop:boolean;
    f:text;


function caut(r,n:longint;var i:longint):boolean;
    begin
       i:=1;
       while (i<=n) and (r<>rest[i]) do inc(i);
       caut:=i<=n;
    end;

procedure solutie;
var i:longint;
  begin
    assign(f,'multiplu.out');rewrite(f);
    for i:=1 to sfc do
    write(f,rest[i],' '); writeln(f);
        for i:=1 to sfc do
    write(f,prec[i],' '); writeln(f);
        for i:=1 to sfc do
    write(f,cif[i],' '); writeln(f);
    close(f);
  end;

procedure refac;     {solutia iterativa iese din timp :( }
var sol:vector;
     i,k:integer;
begin
   i:=sfc;k:=0;
   while i>1 do
   begin
     inc(k);sol[k]:=cif[i];i:=prec[i];
   end;
   assign(f,'multiplu.out');rewrite(f);
   write(f,1);
   for i:=k downto 1 do write(f,sol[i]);
   close(f);
  end;

procedure refac_r(i:longint);
begin
   if i>0 then begin
                    refac_r(prec[i]);
                    write(f,cif[i]);
               end;
end;


begin
assign(f,'multiplu.in');reset(f);read(f,a,b);close(f);
d:=a*b;
   while a*b > 0 do
      if a>b then a:=a mod b
             else b:=b mod a;
   d:=d div (a+b);
ic:=1;sfc:=1;
fillchar(sr,sizeof(sr),0);
rest[1]:=1;cif[1]:=1;sr[1]:=1;
stop:=false;
while not stop do
begin
r:=rest[ic]*10 mod d;
if {not caut(r,sfc-1,poz)} sr[r]=0 then
   begin
   inc(sfc);rest[sfc]:=r;prec[sfc]:=ic;cif[sfc]:=0;sr[r]:=1;
   end;
if r=0 then begin
             stop:=true; assign(f,'multiplu.out');rewrite(f);
             {solutie;} refac_r(sfc);close(f);
             end;
if not stop then
   begin
r:=(rest[ic]*10+1) mod d;
if {not caut(r,sfc-1,poz)} sr[r]=0 then
   begin
   inc(sfc);rest[sfc]:=r;prec[sfc]:=ic;cif[sfc]:=1;sr[r]:=1;
   end;
if r=0 then begin
             stop:=true; assign(f,'multiplu.out');rewrite(f);
             {solutie; } refac_r(sfc);close(f);
             end;
   end;
inc(ic);
end;


end.