Cod sursa(job #2904805)

Utilizator Valentin06Maftei Valentin Valentin06 Data 18 mai 2022 02:01:19
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <bits/stdc++.h>

using namespace std;

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

int matrice[20][250001];
int main()
{
    int nrStramosi, pStramos;
    in >> nrStramosi >> pStramos;
    for (int i = 1; i <= nrStramosi; i++){
        in >> matrice[0][i];
    }
    for(int i = 1; i<= 18; i++)
        for(int j = 1; j <= nrStramosi; j++)
            matrice[i][j] = matrice[i-1][matrice[i-1][j]];
    while (pStramos --)
    {
        int curent, distanta, exponent = 0;
        in >> curent >> distanta;
        while (distanta > 0)
        {
            if (distanta % 2 == 1)
                curent = matrice[exponent][curent];
            exponent ++;
            distanta = distanta/2;
        }
        out << curent << '\n';
    }
    return 0;
}