Pagini recente » Cod sursa (job #611499) | Cod sursa (job #437016) | Cod sursa (job #2825662) | Cod sursa (job #719107) | Cod sursa (job #690962)
Cod sursa(job #690962)
program multiplu;
type vector=array[1..2000000] of longint;
var 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;
rest[1]:=1;cif[1]:=1;
stop:=false;
while not stop do
begin
r:=rest[ic]*10 mod d;
if not caut(r,sfc-1,poz) then
begin
inc(sfc);rest[sfc]:=r;prec[sfc]:=ic;cif[sfc]:=0;
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) then
begin
inc(sfc);rest[sfc]:=r;prec[sfc]:=ic;cif[sfc]:=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.