Pagini recente » Cod sursa (job #154154) | Cod sursa (job #2110655) | Cod sursa (job #2803326) | Cod sursa (job #2305899) | Cod sursa (job #1813)
Cod sursa(job #1813)
#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;
}