Cod sursa(job #1813)

Utilizator AlxCojocaru Alexandru Alx Data 14 decembrie 2006 19:56:03
Problema Stramosi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>
#include <math.h>
double doi=2;
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int main()
{
 int (*a)[250000],*nrs,n,m,p,q,i;
 double j;
 f>>n>>m;
 nrs=new int[250000];
 a=new int[18][250000];
 a[0][0]=0;
 for (i=1;i<=n;i++)
 {
  f>>a[0][i];
  nrs[i]=1;
  if (i<18)
   a[i][0]=0;
 }
 for (i=1;i<=n;i++)
  for (j=1;j<18;j++)
  {
   int y=j;
   a[y][i]=a[y-1][a[y-1][i]];
  }
 for (i=0;i<m;i++)
 {
  f>>q>>p;
  j=0;
  while (pow(doi,j)<p)
   j++;
  while (pow(doi,j)!=p)
  {
   p-=pow(doi,j-1);
   int y=j-1;
   q=a[y][q];
   j=0;
   while (pow(doi,j)<p)
    j++;
  }
  int y=j;
  g<<a[y][q]<<"\n";
 }
 return 0;
}