Listing SCUTURI.PAS
Program
constnmax=30;
typescut=array[1..nmax] of Longint;
varn,i,j,p,k,max,timp:Longint;
r:scut;
per,poz,pp,sens:array[1..2] of Longint;
a:array[1..2,1..2*nmax] of 1..2*nmax;
b:array[1..2] of Longint;
c:array[1..2*nmax] of Longint;
valid:Boolean; f:Text;
functiongata:Boolean;
vari,j:Word;
begin
gata:=false;
for i:=1 to 2 do
for j:=1 to n do
if (r[j]>0) and (a[i,j]>0) then Exit;
gata:=true
end;
function cmmdc(a,b:Longint):Longint;
var r:Longint;
begin
repeat
r:=a mod b;
a:=b; b:=r
until r=0;
cmmdc:=a
end;
Begin
Assign(f,'SCUT.IN'); Reset(f); Readln(f,n);
poz[1]:=1; poz[2]:=n; Read(f,r[1]);
for i:=2 to n-1 do
begin Read(f,r[i]); Inc(r[i]) end;
Read(f,r[n]);
Readln(f,per[1],per[2]);
{ se determina perioada principala }
{ a fiecarui tragator }
sens[1]:=1; sens[2]:=-1;
for i:=1 to 2 do
begin
p:=poz[i];
repeat
Inc(a[i,p]);
for j:=1 to per[i] do
begin
if (p=n) and (sens[i]=1) then sens[i]:=-1;
if (p=1) and (sens[i]=-1) then sens[i]:=1;
p:=p+sens[i]
end;
Inc(b[i],per[i])
until p=poz[i]
end;
{ se determina obiectivele distructibile (!) }
k:=0;
for i:=1 to n do
if a[1,i]+a[2,i]>0 then Inc(k);
Writeln; Writeln(k);
{ se determina perioada ansamblului }
k:=b[1]*b[2] div cmmdc(b[1],b[2]);
for j:=1 to n do
begin
c[j]:=0;
for i:=1 to 2 do
c[j]:=c[j]+a[i,j]*k div b[i]
end;
{ se determina numarul de perioade necesare }
max:=0;
for j:=1 to n do
if (c[j]>0) and (r[j] div c[j]>max)
then
begin max:=r[j] div c[j]; i:=j end;
{ se distrug scuturile corespunzatoare }
{ numarului de perioade -1 pentru ca ultima }
{ perioada nu se executa neaparat complet }
timp:=timp+(max-1)*k;
for i:=1 to n do
begin
j:=r[i]-(max-1)*c[i];
if j>0 then r[i]:=j
else r[i]:=0
end;
{ se simuleaza ultima perioada }
sens[1]:=1; sens[2]:=-1;
repeat
for i:=1 to 2 do
begin
if (poz[i]=n) and (sens[i]=1)
then sens[i]:=-1;
if (poz[i]=1) and (sens[i]=-1)
then sens[i]:=1;
poz[i]:=poz[i]+sens[i];
pp[i]:=(pp[i]+1) mod per[i];
if (pp[i]=0) and (r[poz[i]]>0)
then Dec(r[poz[i]])
end;
Inc(timp)
until gata;
Writeln(timp)
End.