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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define MOOD 1000000007

using namespace std;
using ll = long long;
using P = pair<int, int>;

int cmpnum(const void *a, const void *b) {
  long long *A = (long long *)a;
  long long *B = (long long *)b;
  if (*A > *B)
    return 1;
  if (*A < *B)
    return -1;
  return 0;
}

int char_sort_max(const void *a, const void *b) {
  return strcmp((char *)a, (char *)b);
}

int char_sort_min(const void *a, const void *b) {
  return strcmp((char *)a, (char *)b);
}

int gcd(int x, int y) {
  int t;

  while (y != 0) {
    t = x % y;
    x = y;
    y = t;
  }
  return x;
}
ll lcm(ll a, ll b) { return (ll)((ll)(a * b) / (ll)(gcd(a, b))); }

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1){};

  int find(int x) {
    if (d[x] < 0) {
      return x;
    } else {
      return d[x] = find(d[x]);
    }
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);

    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

struct mint {
  ll x;

  mint(ll x) : x((x % MOOD + MOOD) % MOOD) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOOD)
      x -= MOOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOOD - a.x) >= MOOD)
      x -= MOOD;
    return *this;
  }

  mint &operator*=(const mint a) {
    if ((x *= a.x) %= MOOD)
      ;
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  mint inv() const { return pow(MOOD - 2); }

  mint &operator/=(const mint a) { return (*this) *= a.inv(); }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

mint kaijyo(int n) {
  if (n == 0)
    return 1;
  mint x = kaijyo(n / 2);
  x *= x;
  if (n % 2 == 1)
    x *= 2;
  return x;
}

mint choose(int n, int a) {
  mint x = 1, y = 1;
  rep(i, a) {
    x *= n - i;
    y *= i + 1;
  }
  return x / y;
}

int shakutori(ll x, ll n, ll k, ll a[]) {
  long long int ai, res, ok, ng, mid;
  res = 0;
  for (int i = 0; i < n; i++) {
    // printf("%lld\\n", res);
    ai = a[i];
    if (ai == 0) {
      if (x < 0) {
        res += 0;
      } else {
        res += n - 1;
      }
    } else {
      if (ai <= -1) {
        if (ai * ai <= x) {
          res -= 1;
        }
        ok = n;
        ng = -1;
        while (ok - ng > 1) {
          mid = (ok + ng) / 2;
          if (a[mid] * ai <= x) {
            ok = mid;
          } else {
            ng = mid;
          }
        }
        res += n - ok;
      } else {
        if (ai * ai <= x) {
          res -= 1;
        }
        ok = -1;
        ng = n;
        while (ng - ok > 1) {
          mid = (ok + ng) / 2;
          if (a[mid] * ai <= x) {
            ok = mid;
          } else {
            ng = mid;
          }
        }
        res += ok + 1;
      }
    }
  }
  // printf("%lld\\n", res);
  return k <= res / 2;
}

int two_n(int tei, int jyousuu) {
  if (jyousuu == 0)
    return 1;
  int x = two_n(tei, jyousuu / 2);
  x *= x;
  if (jyousuu % 2 == 1) {
    x *= tei;
  }
  return x;
}

vector<vector<int>> bit_search(int N) {
  vector<vector<int>> bit_sum(two_n(2, N));
  int num = 0;
  for (int bit = 0; bit < (1 << N); ++bit) {
    for (int i = 0; i < N; ++i) {
      if (bit & (1 << i)) {
        bit_sum[num].push_back((i));
      }
    }
    num++;
  }
  return bit_sum;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N + 1, 0);
  vector<int> roop(N + 1, 0);
  vector<int> jyunkai(N + 1, 0);
  for (int i = 1; i <= N; ++i) {
    cin >> A[i];
  }
  ll genzai = 1;
  ll count = 1;

  while (roop[genzai] == 0) {
    genzai = A[genzai];

    if (jyunkai[genzai] == 0) {
      jyunkai[genzai] = count;
    } else {
      roop[genzai] = count - jyunkai[genzai];
    }
    count++;
  }
  genzai = 1;
  count = 1;
  while (1) {

    if (roop[genzai] != 0) {
      K %= roop[genzai];
    }
    if (K == 0) {
      printf("%lld", genzai);
      return 0;
    }
    genzai = A[genzai];
    K--;
  }
}

int A() {
  char S[15], T[15];
  scanf("%s", S);
  scanf("%s", T);
  if (strncmp(S, T, strlen(S)) == 0) {
    printf("Yes");
    return 0;
  }
  printf("No");
}

int B() {
  ll A, B, C, K;
  scanf("%lld %lld %lld %lld", &A, &B, &C, &K);
  if (K <= A) {
    printf("%lld", K);
  } else if (K <= A + B) {
    printf("%lld", A);
  } else {
    printf("%lld", A - (K - A - B));
  }

  return 0;
}

int C() {
  int N, M, X;
  cin >> N >> M >> X;
  vector<vector<int>> A(N, vector<int>(M + 1, 0));

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M + 1; ++j) {
      cin >> A[i][j];
    }
  }

  vector<vector<int>> bit_sum = bit_search(N);
  int COST_MIN = 100000000;

  for (int i = 0; i < bit_sum.size(); ++i) {
    vector<int> SUM(M + 1, 0);
    for (int j = 0; j < bit_sum[i].size(); ++j) {
      for (int S = 0; S < M + 1; ++S) {
        SUM[S] += A[bit_sum[i][j]][S];
      }
    }
    for (int j = 1; j < M + 1; ++j) {
      if (SUM[j] < X) {
        break;
      }

      else if (j == M) {
        if (COST_MIN > SUM[0])
          COST_MIN = SUM[0];
      }
    }
  }
  if (COST_MIN == 100000000) {
    printf("-1");
    return 0;
  }
  printf("%d", COST_MIN);

  return 0;
}

Error: -8

Fixed submission:

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define MOOD 1000000007

using namespace std;
using ll = long long;
using P = pair<int, int>;

int cmpnum(const void *a, const void *b) {
  long long *A = (long long *)a;
  long long *B = (long long *)b;
  if (*A > *B)
    return 1;
  if (*A < *B)
    return -1;
  return 0;
}

int char_sort_max(const void *a, const void *b) {
  return strcmp((char *)a, (char *)b);
}

int char_sort_min(const void *a, const void *b) {
  return strcmp((char *)a, (char *)b);
}

int gcd(int x, int y) {
  int t;

  while (y != 0) {
    t = x % y;
    x = y;
    y = t;
  }
  return x;
}

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1){};

  int find(int x) {
    if (d[x] < 0) {
      return x;
    } else {
      return d[x] = find(d[x]);
    }
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);

    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

struct mint {
  ll x;

  mint(ll x) : x((x % MOOD + MOOD) % MOOD) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOOD)
      x -= MOOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOOD - a.x) >= MOOD)
      x -= MOOD;
    return *this;
  }

  mint &operator*=(const mint a) {
    if ((x *= a.x) %= MOOD)
      ;
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  mint inv() const { return pow(MOOD - 2); }

  mint &operator/=(const mint a) { return (*this) *= a.inv(); }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

mint kaijyo(int n) {
  if (n == 0)
    return 1;
  mint x = kaijyo(n / 2);
  x *= x;
  if (n % 2 == 1)
    x *= 2;
  return x;
}

mint choose(int n, int a) {
  mint x = 1, y = 1;
  rep(i, a) {
    x *= n - i;
    y *= i + 1;
  }
  return x / y;
}

int shakutori(ll x, ll n, ll k, ll a[]) {
  long long int ai, res, ok, ng, mid;
  res = 0;
  for (int i = 0; i < n; i++) {
    // printf("%lld\\n", res);
    ai = a[i];
    if (ai == 0) {
      if (x < 0) {
        res += 0;
      } else {
        res += n - 1;
      }
    } else {
      if (ai <= -1) {
        if (ai * ai <= x) {
          res -= 1;
        }
        ok = n;
        ng = -1;
        while (ok - ng > 1) {
          mid = (ok + ng) / 2;
          if (a[mid] * ai <= x) {
            ok = mid;
          } else {
            ng = mid;
          }
        }
        res += n - ok;
      } else {
        if (ai * ai <= x) {
          res -= 1;
        }
        ok = -1;
        ng = n;
        while (ng - ok > 1) {
          mid = (ok + ng) / 2;
          if (a[mid] * ai <= x) {
            ok = mid;
          } else {
            ng = mid;
          }
        }
        res += ok + 1;
      }
    }
  }
  // printf("%lld\\n", res);
  return k <= res / 2;
}

int main() {
  ll N, K;
  scanf("%lld %lld", &N, &K);
  ll A[N];
  for (int i = 0; i < N; ++i) {
    scanf("%lld", &A[i]);
  }
  int jyunkai[N];
  int roop[N];
  for (int i = 0; i < N; ++i) {
    jyunkai[i] = 0;
    roop[i] = 0;
  }

  ll genzai = 1;
  int aa = 0;

  while (jyunkai[genzai - 1] != 2) {
    ++aa;
    if (jyunkai[genzai - 1] != 0) {
      roop[genzai - 1] = aa - jyunkai[genzai - 1];
      break;
    }
    jyunkai[genzai - 1] = aa;
    genzai = A[genzai - 1];
  }

  ll count = 0;
  ll ans = 1;
  for (int i = 0; i < N; ++i) {
    if (roop[ans - 1] == 0) {
      count++;
      ans = A[ans - 1];
    } else {
      for (int j = 0; j < (K - count) % roop[ans - 1]; ++j) {
        ans = A[ans - 1];
      }
      printf("%lld", ans);
      return 0;
    }
  }
}

int A() {
  char S[15], T[15];
  scanf("%s", S);
  scanf("%s", T);
  if (strncmp(S, T, strlen(S)) == 0) {
    printf("Yes");
    return 0;
  }
  printf("No");
}

int B() {
  ll A, B, C, K;
  scanf("%lld %lld %lld %lld", &A, &B, &C, &K);
  if (K <= A) {
    printf("%lld", K);
  } else if (K <= A + B) {
    printf("%lld", A);
  } else {
    printf("%lld", A - (K - A - B));
  }

  return 0;
}

int C() {
  ll N, M, X;

  scanf("%lld %lld %lld", &N, &M, &X);
  ll A[N][M];
  ll Cost[N];
  ll max = 0;
  for (int i = 0; i < N; ++i) {
    scanf("%lld", &Cost[i]);
    for (int j = 0; j < M; ++j) {
      scanf("%lld", &A[i][j]);
    }
  }
}