Cod sursa(job #2904303)

Utilizator elenaa_g23Elena Georgescu elenaa_g23 Data 17 mai 2022 22:58:04
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.58 kb
#include<iostream>
#include<fstream>

using namespace std;

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

int A[21][250001];

int main()
{
   int n,m,i,j,om,stramos,k;
   f>>n>>m;
   for(i=1;i<=n;i++)
    f>>A[0][i];

   for(int i=1;i<=20;i++)
   {
       for(j=1;j<=n;j++)
        A[i][j]=A[i-1][A[i-1][j]];
   }

   for(i=1;i<=m;i++)
   {
       f>>om>>stramos;
       k=0;
       while(stramos>0)
       {
           if(stramos%2==1)
              om=A[k][om];
           k++;
           stramos/=2;
       }
       g<<om<<'\n';

   }

}