Language: C++

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  long long int K;

  cin >> N >> K;
  //cout << K;
  vector <int> A(N),a(N),b(N);


  for(int i=0; i<N; i++){
    cin >> A.at(i);

  }

  for(int i=0; i<N; i++){
    if(i==0){
      a.at(i)=A.at(i);

    }
    else{
      a.at(i)=A.at(a.at(i-1)-1);
    }
    //cout << a.at(i) << " " ;
  }
  bool frag=false;
  b = a;
  sort(b.begin(),b.end());
  for(int i=0; i<N-1;i++){
    if(b.at(i)==b.at(i+1)){
      frag = true;
    }
  }

  //cout << endl;

  long long int count=0,cycle=N;
  for(long long int i=0; i<N; i++){
    if(frag==false){
      break;
    }
    for(long long int j=1;i+j<N;j++){
      if(a.at(i)==a.at(i+j)){
        count = i;
        cycle = j;
        cout << i << " " << j <<endl;
        frag = false;
      }
      if(frag==false){
        break;
      }
    }
  }
  //cout << count << " " << cycle <<"asdfk"<<endl;
  K -= count;
  K %= cycle;
  //cout << K<<endl;
  if(count != 0 && K ==0){
    K = cycle;
  }#include <bits/stdc++.h>
  using namespace std;

  int main() {
    int N;
    long long int K;

    cin >> N >> K;
    //cout << K;
    vector <int> A(N),a(N),b(N);


    for(int i=0; i<N; i++){
      cin >> A.at(i);

    }

    for(int i=0; i<N; i++){
      if(i==0){
        a.at(i)=A.at(i);

      }
      else{
        a.at(i)=A.at(a.at(i-1)-1);
      }
      //cout << a.at(i) << " " ;
    }
    bool frag=false;
    b = a;
    sort(b.begin(),b.end());
    for(int i=0; i<N-1;i++){
      if(b.at(i)==b.at(i+1)){
        frag = true;
      }
    }

    //cout << endl;

    long long int count=0,cycle=N;
    for(long long int i=0; i<N; i++){
      if(frag==false){
        break;
      }
      for(long long int j=1;i+j<N;j++){
        if(a.at(i)==a.at(i+j)){
          count = i;
          cycle = j;
          //cout << i << " " << j <<endl;
          frag = false;
        }
        if(frag==false){
          break;
        }
      }
    }
    //cout << count << " " << cycle <<"asdfk"<<endl;
    K -= count;
    K %= cycle;
    //cout << K<<endl;
    if(count != 0 && K ==0){
      K = cycle;
    }

    cout << a.at(count+K-1)<<endl;

    //cout << goal << endl;

  }


  cout << a.at(count+K-1)<<endl;

  //cout << goal << endl;

}


Compiler output:
a.cc:62:4: error: stray '#' in program
   62 |   }#include <bits/stdc++.h>
      |    ^
a.cc: In function 'int main()':
a.cc:62:5: error: 'include' was not declared in this scope
   62 |   }#include <bits/stdc++.h>
      |     ^~~~~~~
a.cc:62:14: error: 'bits' was not declared in this scope
   62 |   }#include <bits/stdc++.h>
      |              ^~~~
a.cc:62:19: error: 'stdc' was not declared in this scope; did you mean 'std'?
   62 |   }#include <bits/stdc++.h>
      |                   ^~~~
      |                   std
a.cc:63:3: error: expected primary-expression before 'using'
   63 |   using namespace std;
      |   ^~~~~
a.cc:65:11: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   65 |   int main() {
      |           ^~
a.cc:65:11: note: remove parentheses to default-initialize a variable
   65 |   int main() {
      |           ^~
      |           --
a.cc:65:11: note: or replace parentheses with braces to value-initialize a variable
a.cc:65:14: error: a function-definition is not allowed here before '{' token
   65 |   int main() {
      |              ^