Cod sursa(job #254387)
Utilizator | Data | 7 februarie 2009 11:48:43 | |
---|---|---|---|
Problema | Kdrum | Scor | 0 |
Compilator | fpc | Status | done |
Runda | Stelele Informaticii 2009, clasele 9-10, ziua 2 | Marime | 5.72 kb |
program alex;
var f:text;
a:array[0..60,0..60]of longint;
c,b,exp,st:array[1..3001]of longint;
viz:array[1..13001]of integer;
l,col:array[0..3001]of longint;
p:array[1..13001]of 0..1;
n,m,k,x,y,i,j,x1,y1,v,t,z,min,ex:longint;
as,ev,e:boolean;
begin
assign(f,'kdrum.in');reset(f);
readln(f,n,m,k);
readln(f,x,y,x1,y1);
for i:=1 to m do
begin
for j:=1 to n do
read(f,a[i,j]);
readln(f);
end;
close(f);
p[1]:=1;
i:=1;
v:=0;
repeat
i:=i+1;
if p[i]=0 then begin
j:=i*i;
while j<=2000 do
begin
p[j]:=1;
j:=j+i;
end;
v:=v+1;
b[v]:=i;
end;
until(i>=2000);
i:=1;
t:=0;
z:=k;
while(b[i]*b[i]<z)and(z>1)do
begin
if z mod b[i]=0 then begin
t:=t+1;
c[t]:=b[i];
while z mod b[i]=0 do
begin
exp[t]:=exp[t]+1;
z:=z div b[i];
end;
end;
i:=i+1;
end;
if z>1 then begin
t:=t+1;
c[t]:=z;
exp[t]:=1;
end;
i:=1;
while(b[i]*b[i]<a[x,y])and(a[x,y]>1)do
begin
if a[x,y] mod b[i]=0 then begin
ex:=0;
while a[x,y] mod b[i]=0 do
begin
ex:=ex+1;
a[x,y]:=a[x,y] div b[i];
end;
viz[b[i]]:=ex;
end;
i:=i+1;
end;
if a[x,y]>1 then viz[a[x,y]]:=1;
j:=1;
st[j]:=0;
l[0]:=x;
col[0]:=y;
min:=500000;
while j>0 do
begin
repeat
if st[j]<4 then begin
st[j]:=st[j]+1;
as:=true;
end
else as:=false;
if as then begin
case st[j] of
1:begin
col[j]:=col[j-1]+1;
l[j]:=l[j-1];
end;
2:begin
l[j]:=l[j-1]+1;
col[j]:=col[j-1];
end;
3:begin
col[j]:=col[j-1]-1;
l[j]:=l[j-1];
end;
4:begin
l[j]:=l[j-1]+1;
col[j]:=col[j-1];
end;
end;
z:=a[l[j],col[j]];
if z=0 then ev:=false
else if j>=min then ev:=false
else begin
ev:=true;
i:=1;
while(b[i]*b[i]<z)and(z>1)do
begin
if z mod b[i]=0 then begin
ex:=0;
while z mod b[i]=0 do
begin
ex:=ex+1;
z:=z div b[i];
end;
viz[b[i]]:=viz[b[i]]+ex;
end;
i:=i+1;
end;
if z>1 then viz[z]:=viz[z]+1;
end;
end;
until(not as)or(as and ev);
if as then if(l[j]=x1)and(col[j]=y1)then begin
e:=true;
for i:=1 to t do
if viz[c[t]]<exp[t] then begin
e:=false;
break;
end;
if e=true then if j+1<min then min:=j+1;
end
else begin
j:=j+1;
st[j]:=0;
end
else begin
j:=j-1;
z:=a[l[j],col[j]];
i:=1;
while(b[i]*b[i]<z)and(z>1)do
begin
if z mod b[i]=0 then begin
ex:=0;
while z mod b[i]=0 do
begin
ex:=ex+1;
z:=z div b[i];
end;
viz[b[i]]:=viz[b[i]]-ex;
end;
i:=i+1;
end;
if z>1 then viz[z]:=viz[z]-1;
end;
end;
assign(f,'kdrum.out');rewrite(f);
writeln(f,min);
close(f);
end.