Cod sursa(job #2791533)

Utilizator vmnechitaNechita Vlad-Mihai vmnechita Data 30 octombrie 2021 17:23:33
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <bits/stdc++.h>
#define NMAX 250005

using namespace std;

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

int t[19][NMAX];

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0),cout.tie(0);
    int n, m, i, j, x, y, z;

    fin >> n >> m;
    for(i = 1; i <= n; i++) fin >> t[0][i];

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

    while(m--)
    {
        fin >> x >> y;
        while(y)
        {
            z = log2(y);
            y -= (1<<z);
            x = t[z][x];
        }

        fout << x << '\n';
    }

    return 0;
}