Cod sursa(job #1771673)

Utilizator ducu34Albastroiu Radu Gabriel ducu34 Data 5 octombrie 2016 21:01:39
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
//
//  main.cpp
//  Stramosi
//
//  Created by Albastroiu Radu on 10/5/16.
//  Copyright © 2016 Albastroiu Radu. All rights reserved.
//

#include <fstream>
#include <algorithm>
#include <cmath>
using namespace std;

ifstream fin ("stramosi.in");
ofstream fout("stramosi.out");
int n,p,q,i,j,nod,stramos,D[25][250001];
int main ()
{
    fin>>n>>q;
    for(i=1;i<=n;i++)
        fin>>D[0][i];
    for(i=1;i<=18;i++)
    {
        for(j=1;j<=n;j++)
        {
            D[i][j]=D[i-1][D[i-1][j]];
        }
    }
    for(i=1;i<=q;i++)
    {
        fin>>nod>>stramos;
        p = 0;
        while(stramos)
        {
            if(stramos % 2 == 1)
            {
                nod = D[p][nod];
            }
            p++;
            stramos = stramos/2;
        }
        fout<<nod<<"\n";
    }
    return 0;
}