Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, b) for (int i = a; i <= b; i++)
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define sq(a) (a) * (a)
#define nl "\\n"
#define all(x) (x).begin(), (x).end()
using namespace std;
// using namespace __gnu_pbds;
// typedef tree<int,null_type,less<int>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

#ifdef LOCAL
#define eprintf(...)                                                           \\
  fprintf(stderr, __VA_ARGS__);                                                \\
  fflush(stderr);
#else
#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template <typename T> using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using vi = vector<int>;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vvi = vector<vi>;
using vpi = vector<pii>;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

clock_t startTime;
double getCurrentTime() {
  return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
const int N = 100100;
ll mod = 1000000007;
const ll oo = 9876543210;

vll operator*(const vll &a, const vll &b) {
  vll ans(a.size());
  rep(i, a.size()) { ans[i] = a[b[i]]; }
  return ans;
}

vll powe(vll a, ll k) {
  vll ans(a.size());
  iota(ans.begin(), ans.end(), 0);
  while (k > 0) {
    if (k & 1)
      ans = ans * a;
    a = a * a;
    k >>= 1;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  startTime = clock();
  srand(time(0));
  ll n, k;
  cin >> n >> k;
  vll a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  vll ans = powe(a, k);
  cout << ans[1] << nl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, b) for (int i = a; i <= b; i++)
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define sq(a) (a) * (a)
#define nl "\\n"
#define all(x) (x).begin(), (x).end()
using namespace std;
// using namespace __gnu_pbds;
// typedef tree<int,null_type,less<int>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

#ifdef LOCAL
#define eprintf(...)                                                           \\
  fprintf(stderr, __VA_ARGS__);                                                \\
  fflush(stderr);
#else
#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template <typename T> using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using vi = vector<int>;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vvi = vector<vi>;
using vpi = vector<pii>;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

clock_t startTime;
double getCurrentTime() {
  return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
const int N = 100100;
ll mod = 1000000007;
const ll oo = 9876543210;
long long n, k;
vector<ll> a(N);

ll solve() {
  vector<ll> pos(n + 1);
  vector<bool> vis(n + 1, false);
  for (int i = 1; i <= n; i++) {
    pos[a[i]] = i;
  }
  vector<ll> st;
  ll limit = -1, cnt = 0;
  for (int i = 1;;) {
    if (vis[i]) {
      limit = i;
      break;
    }
    vis[i] = 1;
    st.push_back(i);
    i = a[i];
    cnt = cnt + 1;
  }
  ll c = 0;
  for (int i = 1; i != limit;) {
    c++;
    i = a[i];
  }
  cnt -= c;
  if (k < c) {
    return st[k];
  } else {
    return st[c + (k - c) % cnt];
  }
}

ll brute() {
  ll i = 1, sz = 1;
  for (int l = 0; l < k; l++) {
    i = a[i];
  }
  return i;
}

void test_solution() {
  while (true) {
    n = rand() % 100000;
    k = rand() % 1000000000000000000;
    debug("here");
    cout << n << " " << k << nl;
    for (int i = 1; i <= n; i++) {
      a[i] = rand() % n + 1;
      cout << a[i] << " ";
    }
    cout << nl;
    //		ll ans1 = brute();
    ll ans2 = solve();
    cout << ans2 << nl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  startTime = clock();
  srand(time(0));

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  // ll ans1 = brute();
  ll ans = solve();
  cout << ans << nl;
  // cout<<ans1<<nl;

  //	test_solution();
  return 0;
}