Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
constexpr ll MOD = (1e9 + 7);
constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int lcm(int a, int b) { return a / gcd(a, b) * b; }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll factorial(ll n, ll m = 2) {
  // calculate n!
  m = max(2LL, m);
  ll rtn = 1;
  for (ll i = m; i <= n; i++) {
    rtn = (rtn * i) % MOD;
  }
  return rtn;
}
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  ll cnt = 0, per = 0, start_new = 0;
  vector<ll> already;
  already.push_back(0);
  rep1(i, n - 1) {
    int cur = a[cur];
    bool flag = false;
    cnt++;
    rep(j, already.size()) {
      if (cur == already[j]) {
        start_new = cur;
        per = i - j;
        flag = true;
        break;
      }
    }
    already.push_back(cur);
    if (flag)
      break;
  }
  k -= cnt;
  k %= per;
  ll prev = start_new;
  rep(i, k) prev = a[prev];
  cout << prev + 1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
constexpr ll MOD = (1e9 + 7);
constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int lcm(int a, int b) { return a / gcd(a, b) * b; }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll factorial(ll n, ll m = 2) {
  // calculate n!
  m = max(2LL, m);
  ll rtn = 1;
  for (ll i = m; i <= n; i++) {
    rtn = (rtn * i) % MOD;
  }
  return rtn;
}
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  vector<vector<ll>> nxt(60, vector<ll>(n));
  rep(i, n) cin >> nxt[0][i], nxt[0][i]--;
  rep1(i, 59) rep(j, n) nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
  rep(i, 60) if (k & (1ll << i)) ans = nxt[i][ans];
  cout << ++ans << endl;
  return 0;
}