Cod sursa(job #2236957)
| Utilizator | Data | 31 august 2018 02:34:09 | |
|---|---|---|---|
| Problema | Stramosi | Scor | 90 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 0.56 kb |
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int N,M,V[18][250005];
int main(){
fin>>N>>M;
for(int i=1;i<=N;i++)
fin>>V[0][i];
for(int i=1;(1<<i)<=N;i++)
for(int j=1;j<=N;j++)
V[i][j]=V[i-1][V[i-1][j]];
for(int k=1;k<=M;k++){
int X,Y;
fin>>X>>Y;
for(int i=0;X!=0 && Y!=0;i++){
if(Y&1)
X=V[i][X];
Y=Y/2;
}
fout<<X<<'\n';
}
return 0;
}
