Cod sursa(job #1452124)
| Utilizator | Data | 19 iunie 2015 22:00:46 | |
|---|---|---|---|
| Problema | Stramosi | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 0.67 kb |
#include <stdio.h>
const int Dim = 260000;
int N,M,DP[20][Dim];
int main()
{
freopen("stramosi.in","r",stdin);
freopen("stramosi.out","w",stdout);
scanf("%d%d",&N,&M);
for (int i = 1;i <= N;i++)
scanf("%d",&DP[0][i]);
for (int i = 1;(1 << i) <= N;i++)
for (int j = 1;j <= N;j++)
DP[i][j] = DP[i-1][DP[i-1][j]];
while(M--)
{
int P,Q,i = 0;
scanf("%d%d",&Q,&P);
while (P)
{
if (P % 2)
Q = DP[i][Q];
i++;
P /= 2;
}
printf("%d\n",Q);
}
return 0;
}
