Cod sursa(job #487009)

Utilizator zloteanu.adrianzloteanu adrian nichita zloteanu.adrian Data 23 septembrie 2010 16:03:23
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.44 kb
#include<fstream>
using namespace std;
ifstream q("stramosi.in");
ofstream w("stramosi.out");
int n,m,a[18][250001];
int main()
{q>>n>>m;
int i=1,j;
for(i=1;i<=n;++i)
 q>>a[0][i];
for(j=1;j<18;++j)
 for(i=1;i<=n;++i)
  a[j][i]=a[j-1][a[j-1][i]];
for(;m;--m)
{int q,p,x=1,i=0;
 q>>q>>p;
 while(x<p)
 {x<<=1;++i;}
 while(x&&p&&q)
    {if(x<=p)
      {q=a[i][q];
      p-=x;}
      x>>=1;
  --i;}
w<<q<<'\n';}
return 0;
}