Cod sursa(job #1697115)

Utilizator llalexandruLungu Alexandru Ioan llalexandru Data 30 aprilie 2016 19:35:59
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <fstream>
#define NM 250005

using namespace std;

ifstream fin("stramosi.in");
ofstream fout("stramosi.out");


int M[20][NM], n, m;



int main()
{
    int i, x, j, c, y;
    fin>>n>>m;
    for (i=1; i<=n; i++)
    {
        fin>>x;
        c=1;
        M[0][i]=x;
        for (j=2; j<=n; j=j*2)
        {
            M[c][i]=M[c-1][M[c-1][i]];
            c++;
        }
    }
    for (i=1; i<=m; i++)
    {
        fin>>x>>y;
        c=0;
        if (y>n)
        {
            fout<<0<<'\n';
            continue;
        }
        while (y>0)
        {
            if (y%2==1)
            {
                x=M[c][x];
            }
            c++;
            y=y/2;
        }
        fout<<x<<'\n';
    }
    return 0;
}