Cod sursa(job #165425)

Utilizator bogdan005Aldescu Bogdan bogdan005 Data 25 martie 2008 22:36:26
Problema Stramosi Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.06 kb
 type matr=array[0..18,0..250000] of longword;

 var f,g:text;
     j,i,n,m,a,b,ct:longint;
     s : matr;
     p:array[1..32768] of byte;
     c:char;

function getnn:longint;
 var rez:longint;
 begin
 rez:=0;
 while (ord(c) >=48) and (ord(c) <= 57) do
         begin
         rez:=rez*10 + ord(c) - 48;
         read(f,c);
         end;
 getnn:=rez;
 while (((ord(c)  < 48) or ( ord(c) > 57))) and ( ord(c) <> 26)  do
        read(f,c);
 end;


 begin

  assign(f,'stramosi.in');
  reset(f);
  settextbuf(f,p); read(f,c);
  assign(g,'stramosi.out'); rewrite(g);
  readln(f,n,m);
  n:=getnn; m:=getnn;
  for i:=1 to n do
   begin
   read(f,s[0,i]);
   s[0,i]:=getnn;
   end;
  while (1 shl ct)<=n do
    inc(ct);
  dec(ct);
  for i:=1 to ct do
   for j:=1 to n do
     s[i,j]:=s[i-1,s[i-1,j]];
 for i:=1 to m do
 begin
 readln(f,a,b);
  a:=getnn; b:=getnn;
   while b>0 do
   begin
    ct:=0;
    while (1 shl ct) <= b do
     inc(ct);
    dec(ct);
    b:=b-(1 shl ct);
    a:=s[ct,a];
   end;
 writeln(g,a);
 end;
 close(g);
end.