program hazard;//runda 3-B 30.09.2015
const nmax=1000000;
type Ti=longint;
function Euclid(m,n:Ti):Ti;
begin if n=0 then Euclid:=m
else Euclid:=Euclid(n,m mod n)
end; //Euclid
var i,j,io,ja,ausum:Ti;//suma wyplat w jednym cyklu
n,m,NWD,lgier:qword;
ch: char;
koniec:boolean;
so,sopocz: array[1..nmax] of Ti; //sumy oszczęd aktualne i pocz
au: array[1..nmax] of Ti; //wyniki automatu: W lub P
begin
readln(n);
for i:=1 to n do read(so[i]);
sopocz:=so; ausum:=0;
readln(m); NWD:=Euclid(n,m);
for j:=1 to m do
begin read(ch);
if ch='W' then au[j]:=1 else au[j]:=-1;
ausum+=au[j]
end;//j
io:=0; ja:=0; lgier:=0; //init liczniki
repeat
for i:=1 to n div NWD do
for j:=1 to m do
begin Inc(lgier); Inc(io); Inc(ja); so[io]+=au[ja];
if so[io]=0 then begin writeln(lgier); exit end;
if io=n then io:=0; if ja=m then ja:=0
end;//j,i
koniec:=true; //spr, czy koniec
for i:=1 to n do koniec:=koniec and (so[i]-sopocz[i]>=0);
if koniec then begin writeln(-1); exit end;
until false;
end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | program hazard;//runda 3-B 30.09.2015 const nmax=1000000; type Ti=longint; function Euclid(m,n:Ti):Ti; begin if n=0 then Euclid:=m else Euclid:=Euclid(n,m mod n) end; //Euclid var i,j,io,ja,ausum:Ti;//suma wyplat w jednym cyklu n,m,NWD,lgier:qword; ch: char; koniec:boolean; so,sopocz: array[1..nmax] of Ti; //sumy oszczęd aktualne i pocz au: array[1..nmax] of Ti; //wyniki automatu: W lub P begin readln(n); for i:=1 to n do read(so[i]); sopocz:=so; ausum:=0; readln(m); NWD:=Euclid(n,m); for j:=1 to m do begin read(ch); if ch='W' then au[j]:=1 else au[j]:=-1; ausum+=au[j] end;//j io:=0; ja:=0; lgier:=0; //init liczniki repeat for i:=1 to n div NWD do for j:=1 to m do begin Inc(lgier); Inc(io); Inc(ja); so[io]+=au[ja]; if so[io]=0 then begin writeln(lgier); exit end; if io=n then io:=0; if ja=m then ja:=0 end;//j,i koniec:=true; //spr, czy koniec for i:=1 to n do koniec:=koniec and (so[i]-sopocz[i]>=0); if koniec then begin writeln(-1); exit end; until false; end. |
English