Cod sursa(job #2902078)

Utilizator kanyjmkSabau Eduard kanyjmk Data 15 mai 2022 15:27:34
Problema Stramosi Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int n, m;
int stramosi[22][250005];
int get_stramos(int membru, int stramos)
{
    int sol = membru;
    while(stramos)
    {
        int pow2 = 0;
        while((1<<pow2) <= stramos)
            pow2++;
        pow2--;
        sol = stramosi[pow2][sol];
        stramos -= (1<<pow2);
    }
    return sol;
}
int main()
{
    fin >> n >> m;
    for(int i = 1; i <= n; i ++)
        fin >> stramosi[0][i];
    for(int j = 1; (1<<j) <= n; j++)
        for(int i = 1; i <= n; i++)
            stramosi[j][i] = stramosi[j-1][stramosi[j-1][i]];
    for(int i = 1; i <= m; i++)
    {
        int stramos, membru;
        fin >> membru >> stramos;
        fout << get_stramos(membru, stramos) << '\n';
    }
}