Cod sursa(job #154112)

Utilizator AndreiDDiaconeasa Andrei AndreiD Data 10 martie 2008 22:06:55
Problema Stramosi Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.71 kb
var a,b:array[1..250500] of longint;
    i,j,n,m,x,y,s:longint;
    f,g:text;
begin
assign(f,'stramosi.in');reset(f);
assign(g,'stramosi.out');rewrite(g);
readln(f,n,m);
for i:=1 to n do read(f,a[i]);
for j:=1 to n do begin
    s:=j;
    for i:=1 to 10 do
    if s=0 then break
       else s:=a[s];
    b[j]:=s;
end;
for j:=1 to m do begin
    readln(f,x,y);
    s:=x;
    while y<>0 do
          if s=0 then break
          else
          if y>=10 then begin
                         s:=b[s];
                         y:=y-10;
                         end
          else begin
               s:=a[s];
               dec(y);
          end;
    writeln(g,s);
end;
close(f);
close(g);
end.