Cod sursa(job #2136435)

Utilizator blacktundraTudorescu Ana-Maria blacktundra Data 19 februarie 2018 22:01:19
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("stramosi.in");
ofstream g("stramosi.out");

int n,m,mat[19][250010],p,x,y,k;

int main()
{
    int i,j;

    f>>n>>m;
    for(i=1; i<=n; i++)
        f>>mat[1][i];

    for(i=2; i<=18; i++)
        for(j=1; j<=n; j++)
            mat[i][j]=mat[i-1][mat[i-1][j]];

    for(i=1; i<=m; i++)
    {
        f>>x>>y;
        while(y>0 && x)
        {
            p=1,k=0;
            while(p<=y)
                p*=2,k++;

            p/=2;
            y-=p;
            x=mat[k][x];
        }
        g<<x<<'\n';
    }
}