alexjercan/bugnet huggingface dataset:
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define Rep(i, n) for (long long i = 1; i < n; i++)
#define ll long long
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define PI acos(-1)
using P = pair<int, int>;

ll ketasu(ll a) {
  ll num = 1;
  while (a / 10) {
    num++;
    a /= 10;
  }
  return num;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll kosuu(ll a) {
  ll sum = 0;
  for (ll i = 1; i * i <= a; i++) {
    if (a % i == 0) {
      if (a != 1 && i * i != a) {
        sum += 2;
      } else {
        sum++;
      }
    }
  }
  return sum;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  rep(i, n) {
    ll a;
    cin >> a;
    v[i] = a - 1;
  }
  ll now = 0;

  vector<ll> c(n, -1);

  for (ll l = 0; l < k; l++) {
    if (c[now] == -1) {
      c[now] = l;
    } else {
      ll m = l - c[now];
      ll b = k - l;
      ll d = b % m;
      for (ll j = 0; j < d; j++) {
        now = v[now];
        if (j == d - 1) {
          cout << now + 1 << endl;
          return 0;
        }
      }
    }

    now = v[now];
  }
  cout << now + 1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define Rep(i, n) for (long long i = 1; i < n; i++)
#define ll long long
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define PI acos(-1)
using P = pair<int, int>;

ll ketasu(ll a) {
  ll num = 1;
  while (a / 10) {
    num++;
    a /= 10;
  }
  return num;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll kosuu(ll a) {
  ll sum = 0;
  for (ll i = 1; i * i <= a; i++) {
    if (a % i == 0) {
      if (a != 1 && i * i != a) {
        sum += 2;
      } else {
        sum++;
      }
    }
  }
  return sum;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  rep(i, n) {
    ll a;
    cin >> a;
    v[i] = a - 1;
  }
  ll now = 0;

  vector<ll> c;

  for (ll l = 0; l < k; l++) {
    c.push_back(now);
    rep(i, c.size() - 1) {
      if (now == c[i]) {
        ll m = l - i;
        ll b = k - l;
        ll d = b % m;
        cout << c[i + d] + 1 << endl;
        return 0;
      }
    }
    now = v[now];
  }
  cout << now + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

  vector<int> dest(N + 1, 0);
  for (int i = 1; i < N + 1; i++)
    cin >> dest.at(i);

  vector<int> l(0);
  int now = 1;
  l.push_back(now);
  int loop_start = 0;
  bool isKmin = false;
  vector<bool> isVisit(200001, false);

  while (true) {
    if (K < l.size()) {
      isKmin = true;
      break;
    }
    now = dest.at(now);

    if (isVisit.at(now)) {
      auto f = find(l.begin(), l.end(), now);
      loop_start = f - l.begin();
      break;
    }
    isVisit.at(now) = true;
    l.push_back(now);
  }
  // cout << "list : ";
  // for(int ele : l)cout << ele << " ";
  // cout << endl;

  int index = (K - l.size()) % (l.size() - loop_start) + loop_start;
  // cout << "K-size: " << K-l.size() << "\\tsize - ls: " << l.size()-loop_start
  // << endl;
  if (isKmin)
    cout << l.at(K);
  else
    cout << l.at(index);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  vector<int> dest(N + 1, 0);
  for (int i = 1; i < N + 1; i++)
    cin >> dest.at(i);

  vector<int> l(0);
  int now = 1;
  l.push_back(now);
  int loop_start = 0;
  bool isKmin = false;
  while (true) {
    if (K < l.size()) {
      isKmin = true;
      break;
    }

    now = dest.at(now);

    auto f = find(l.begin(), l.end(), now);
    if (f != l.end()) {
      loop_start = f - l.begin();
      break;
    }
    l.push_back(now);
  }
  // cout << "list : ";
  // for(int ele : l)cout << ele << " ";
  // cout << endl;

  int index = (K - l.size()) % (l.size() - loop_start) + loop_start;
  // cout << "K-size: " << K-l.size() << "\\tsize - ls: " << l.size()-loop_start
  // << endl;
  if (isKmin)
    cout << l.at(K);
  else
    cout << l.at(index);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define f(i, a, b) for (ll i = a; i < (ll)b; i++)
#define af(i, a, b) for (ll i = a; i >= (ll)b; i--)
#define rep(i, a, b, k) for (ll i = a; i < (ll)b; i += k)
#define arep(i, a, b, k) for (ll i = a; i >= (ll)b; i -= k)
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort(a.begin(), a.end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define inter                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nuintptr)
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;

const ll MAX = 2e5 + 6;
const ll inf = 1e9;

ll a[MAX], b[MAX];

int main() {
  ll n, k, x;
  cin >> n >> k;
  f(i, 1, n + 1) cin >> a[i];
  vi ans;
  ans.push_back(1);
  x = a[1];
  b[1] = 1;
  while (b[x] != 1) {
    b[x] = 1;
    ans.push_back(x);
    x = a[x];
  }
  //	cout<<x<<endl;
  ll l = 0, r = 0;
  f(i, 0, ans.size()) {
    if (ans[i] != x)
      l++;
    else
      break;
  }
  r = ans.size() - l;
  //	cout<<l<<" "<<r<<endl;
  if (k < l)
    cout << ans[k] << endl;
  else
    cout << ans[l + ((k - l) % (r))] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define f(i, a, b) for (ll i = a; i < (ll)b; i++)
#define af(i, a, b) for (ll i = a; i >= (ll)b; i--)
#define rep(i, a, b, k) for (ll i = a; i < (ll)b; i += k)
#define arep(i, a, b, k) for (ll i = a; i >= (ll)b; i -= k)
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort(a.begin(), a.end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define inter                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nuintptr)
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;

const ll MAX = 3e3 + 6;
const ll inf = 1e9;

ll a[MAX], b[MAX];

int main() {
  ll n, k, x;
  cin >> n >> k;
  f(i, 1, n + 1) cin >> a[i];
  vi ans;
  ans.push_back(1);
  x = a[1];
  b[1] = 1;
  while (b[x] != 1) {
    b[x] = 1;
    ans.push_back(x);
    x = a[x];
  }
  //	cout<<x<<endl;
  ll l = 0, r = 0;
  f(i, 0, ans.size()) {
    if (ans[i] != x)
      l++;
    else
      break;
  }
  r = ans.size() - l;
  //	cout<<l<<" "<<r<<endl;
  if (k <= l)
    cout << ans[l] << endl;
  else
    cout << ans[l + ((k - l) % (r))] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pdd;

#define INF 0x7f7f7f
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(i, x, n) for (int i = x; i <= n; i++)
// const ll mod = 1e9 + 7;
// const int maxn = 1e5 + 10;
// const double eps = 1e-6;

ll town[200005];
int vis[200005];
ll rul1[200005];
ll rul2[200005];

ll n, k;

void solve() {
  ll cnt1 = 1;
  ll cnt2 = 1;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> town[i];
  }
  ll ans = 1;
  ll m = k;
  while (m--) // 判断循环一次需要传送多少
  {
    ans = town[ans]; // 传送
    if (vis[ans] > 2)
      break;
    if (vis[ans] == 0)
      rul1[cnt1++] = ans;
    else if (vis[ans] == 1)
      rul2[cnt2++] = ans;
    vis[ans]++;
  }
  cnt1--;
  cnt2--;
  if (k <= cnt1)
    cout << rul1[k] << endl;
  else if (cnt2 == -1 && k > cnt1) {
    ll a = k % cnt1;
    if (!a)
      a = cnt1;
  } else if (cnt2 != -1 && k > cnt1) {
    k -= cnt1;
    ll a = k % cnt2;
    if (!a)
      a = cnt2;
    cout << rul2[a] << endl;
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  // cin.tie(nullptr);
  // cout.tie(nullptr);
#ifdef FZT_ACM_LOCAL
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#else
  ios::sync_with_stdio(false);
  int T = 1;
  // cin >> T;
  while (T--)
    solve();
#endif
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> d;
bool been[5000000];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  map<int, int> dic;
  for (int i = 1; i <= n; i++) {
    int tmp;
    scanf("%d", &tmp);
    dic.insert({i, tmp});
  }
  int at = 1, cnt = 0;

  while (!been[at]) {
    been[at] = true;
    cnt++;
    d.push_back(at);
    at = dic[at];
  }

  printf("%d", d[m % cnt]);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pdd;

#define INF 0x7f7f7f
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(i, x, n) for (int i = x; i <= n; i++)
// const ll mod = 1e9 + 7;
// const int maxn = 1e5 + 10;
// const double eps = 1e-6;

const int maxn = 2e5 + 10;
int a[maxn], vis[maxn];
int ans, pos;
vector<int> v;

void dfs(int u) {
  ans++;
  vis[u] = ans;
  v.push_back(u);
  if (!vis[a[u]])
    dfs(a[u]);
  else {
    pos = vis[a[u]];
    v.push_back(a[u]);
    return;
  }
}

void solve() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dfs(1);
  if (k < v.size())
    cout << v[k] << endl;
  else {
    k -= v.size();
    ll len = v.size() - pos;
    cout << v[pos + k % len] << endl;
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  // cin.tie(nullptr);
  // cout.tie(nullptr);
#ifdef FZT_ACM_LOCAL
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#else
  ios::sync_with_stdio(false);
  int T = 1;
  // cin >> T;
  while (T--)
    solve();
#endif
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;

long long a[2000005];
bool b[2000005];
int main() {
  long long n;
  long long k;
  cin >> n >> k;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = 0;
  }
  long long i = 1, x = 0;
  while (i <= n && k--) {
    b[i] = 1;
    x = i;
    i = a[i];
    if (b[i] == 1) {
      k = k % (x - i);
    }
  }
  cout << i << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  vector<int> to;
  to.push_back(0);
  rep(i, n) {
    int now = to.back();
    to.push_back(a[now]);
  }
  int visited[n + 1];
  rep(i, n) visited[i] = 0;
  int s, end;
  rep(i, n) {
    if (visited[to[i]]) {
      end = i;
      break;
    }
    visited[to[i]] = 1;
  }
  rep(i, n) {
    if (to[i] == to[end]) {
      s = i;
      break;
    }
  }
  int len = end - s;

  if (k <= s) {
    cout << to[k] + 1 << endl;
    return 0;
  }
  k -= s;
  cout << to[k % len + s] + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  vector<int> to;
  to.push_back(0);
  rep(i, n) {
    int now = to.back();
    to.push_back(a[now]);
  }
  int r = to.back();
  int len;
  for (int i = n; i >= 0; i--) {
    if (r == to[i]) {
      len = i - r + 1;
      break;
    }
  }
  int s;
  rep(i, n) {
    if (to[i] == to[i + len]) {
      s = i;
      break;
    }
  }
  if (k < s) {
    cout << to[k] << endl;
    return 0;
  }
  k -= s;
  cout << to[k % len + s] + 1 << endl;

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int a[200005];
vector<int> path;
long long ind[200005] = {};

bool check_cycle(int cur_index) {
  if (ind[cur_index])
    return true;
  return false;
}

int main() {
  long long n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  long long temp_k = 0, cur_index = 1;
  while (!ind[cur_index] && temp_k < k) {
    ind[cur_index] = temp_k++;
    path.push_back(cur_index);
    cur_index = a[cur_index];
  }
  if (temp_k == k) {
    cout << cur_index;
  } else {
    cout << path[((k - ind[cur_index]) % (path.size() - ind[cur_index])) +
                 ind[cur_index]];
  }

  return 0;
}

Error: WA

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int a[200005];
vector<int> path;
long long ind[200005] = {};

bool check_cycle(int cur_index) {
  if (ind[cur_index])
    return true;
  return false;
}

int main() {
  long long n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  long long temp_k = 0, cur_index = 1;
  while (!ind[cur_index] && temp_k < k) {
    ind[cur_index] = temp_k++;
    path.push_back(cur_index);
    cur_index = a[cur_index];
  }
  if (temp_k == k) {
    cout << cur_index;
  } else {
    cout
        << path[((k - ind[cur_index]) % (path.size() - cur_index)) + cur_index];
  }

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

long long n, k, a[300000], tot = 1, x = 0, t = 0;
int f[300000], s[300000];
int main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  int i = 1;
  s[tot] = 1;
  f[1] = 1;
  while (1) {
    i = a[i];
    if (f[i] == 1) {
      x = i;
      break;
    }
    f[i] = 1;
    s[++tot] = i;
  }
  if (k < tot)
    printf("%d\\n", s[k + 1]);
  else {
    if (k == tot) {
      printf("%d\\n", x);
      return 0;
    }
    if (x == 1)
      printf("%d\\n", s[k % tot + 1]);
    else {
      for (int i = 1; i <= tot; ++i)
        if (s[i] == x)
          t = i;
      printf("%d\\n", s[(k - t + 1) % (tot - t + 1) + t]);
    }
  }
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

long long n, k, a[300000], tot = 1, x = 0, t = 0;
int f[300000], s[300000];
int main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", a[i]);
  int i = 1;
  s[tot] = 1;
  f[1] = 1;
  while (1) {
    i = a[i];
    if (f[i] == 1) {
      x = i;
      break;
    }
    f[i] = 1;
    s[++tot] = i;
  }
  if (k <= tot)
    printf("%d\\n", s[tot + 1]);
  else {
    if (x == 1)
      printf("%d\\n", s[k % tot + 1]);
    else {
      for (int i = 1; i <= tot; ++i)
        if (s[i] == x)
          t = i;
      printf("%d\\n", s[(k - t + 1) % (tot - t + 1) + t]);
    }
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define INX(x) (cin >> x)
#define OUTX(x) (cout << x << endl)

#define ADD emplace_back
#define MP make_pair

int main() {
  ll N, K;
  cin >> N >> K;

  vector<ll> A;
  for (ll i = 0; i < N; i++) {
    ll tmp;
    cin >> tmp;
    A.ADD(tmp);
  }

  ll lp;
  vector<ll> step;
  set<ll> stepset;
  step.ADD(1);
  stepset.emplace(1);
  for (ll i = 0; i < N; i++) {
    ll next = A[step[step.size() - 1] - 1];

    if (stepset.find(next) == stepset.end()) { // 次が未出現
      step.ADD(next);
      stepset.emplace(next);
      continue;
    } else { // 既に出現しているもの；ループの開始点を求めて終了
      lp = distance(step.begin(), find(step.begin(), step.end(), next));
      break;
    }
  }

  ll loopcount = step.size() - lp; // 周期

  if (K < lp) {
    OUTX(step[K]);
  } else {
    OUTX(step[((K - lp) % loopcount) + lp]);
  }

  return 0;
}


Error: TLE

Fixed submission:

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

#define INX(x) (cin >> x)
#define OUTX(x) (cout << x << endl)

#define ADD emplace_back
#define MP make_pair

int main() {
  ll N, K;
  cin >> N >> K;

  vector<ll> A;
  for (ll i = 0; i < N; i++) {
    ll tmp;
    cin >> tmp;
    A.ADD(tmp);
  }

  ll lp;
  vector<ll> step;
  step.ADD(1);
  for (ll i = 0; i < N; i++) {
    ll next = A[step[step.size() - 1] - 1];

    // lp = distance(step.begin(), find(step.begin(), step.end(), next));
    for (lp = 0; lp < step.size(); lp++) {
      if (next == step[lp]) {
        break;
      }
    }

    if (lp == step.size()) {
      step.ADD(next);
      continue;
    } else {
      break;
    }
  }

  ll loopcount = step.size() - lp; // 周期

  if (K < lp) {
    OUTX(step[K]);
  } else {
    OUTX(step[((K - lp) % loopcount) + lp]);
  }

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int next[70][200008];
int main() {
  int N;
  ll K;
  scanf("%d%lld", &N, &K);
  rep(i, N) {
    scanf("%d", &next[0][i]);
    next[0][i]--;
  }
  rep(j, 69) {
    rep(i, N) { next[j + 1][i] = next[j][next[j][i]]; }
  }
  int cur = 0;
  rep(j, 70) {
    if (K % 2)
      cur = next[j][cur];
    K /= 2;
  }
  printf("%d\\n", cur + 1);
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int next[70][200008];
int main() {
  int N;
  ll K;
  scanf("%d%lld", &N, &K);
  rep(i, N) {
    scanf("%d", &next[0][i]);
    next[0][i]--;
  }
  rep(j, 70) {
    rep(i, N) { next[j + 1][i] = next[j][next[j][i]]; }
  }
  int cur = 0;
  rep(j, 70) {
    if (K % 2)
      cur = next[j][cur];
    K /= 2;
  }
  printf("%d\\n", cur + 1);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

int main() {
  int n, next, repeat = 0;
  long long k, step;
  cin >> n >> k;
  vector<int> A(n), path(n), log(0);

  for (int i = 0; i < n; i++) {
    cin >> A[i];
    path[i] = 0;
  }

  int flag = 0, flagB = 0;
  step = 0;
  next = 1;
  while (flag == 0) {
    if (path[next - 1] == 1 && flagB == 0) {
      flagB = 1;
      int j = 0;
      while (log[j] != next - 1) {
        j++;
      }
      repeat = step - j;
      step = step + ((k - step) / repeat) * repeat;
      if (step == k) {
        flag = 1;
      }

    } else {
      path[next - 1] = 1;
      log.push_back(next - 1);
      step++;
      next = A[next - 1];

      if (step == k) {
        flag = 1;
      }
    }
  }

  cout << next << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

int main() {
  int n, step, next, repeat = 0;
  long long k;
  cin >> n >> k;
  vector<int> A(n), path(n), log(0);

  for (int i = 0; i < n; i++) {
    cin >> A[i];
    path[i] = 0;
  }

  int flag = 0, flagB = 0;
  step = 0;
  next = 1;
  while (flag == 0) {
    if (path[next - 1] == 1 && flagB == 0) {
      flagB = 1;
      int j = 0;
      while (log[j] != next - 1) {
        j++;
      }
      repeat = step - j;
      step = ((k - step) / repeat) * repeat;
      if (step == k) {
        flag = 1;
      }

    } else {
      path[next - 1] = 1;
      log.push_back(next - 1);
      step++;
      next = A[next - 1];

      if (step == k) {
        flag = 1;
      }
    }
  }

  cout << next << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
using Graph = vector<vector<ll>>;

int d[61][200005];

int main() {
  int n;
  cin >> n;
  ll k;
  cin >> k;
  for (size_t i = 1; i <= n; i++) {
    cin >> d[0][i];
  }

  for (size_t i = 1; i < 61; i++) {
    for (size_t j = 1; j <= n; j++) {
      d[i][j] = d[i - 1][d[i - 1][j]];
    }
  }
  int ans = 1;
  for (size_t i = 0; i < 61; i++) {
    if (k & (1LL << i)) {
      ans = d[i][ans];
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
using Graph = vector<vector<ll>>;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (auto &&i : a) {
    cin >> i;
  }

  ll now = 0;
  vector<bool> seen(n + 10, false);
  vector<ll> loop;
  ll count = 0;
  ll i;
  ll last;
  for (i = 0; i <= INT64_MAX; i++) {
    // cout << "now: " << now + 1 << endl;
    if (seen.at(now)) {
      last = now;
      break;
    } else {
      ll nextposix = a.at(now) - 1;
      loop.push_back(now);
      seen.at(now) = true;
      now = nextposix;
      count++;
    }
  }

  // cout << "last: " << last << endl;
  /*
  for (int i = 0; i < 5; i++)
  {
    cout << loop.at(i) << endl;
  }
  */

  ll j;
  for (ll i = 0; i < loop.size(); i++) {
    if (loop.at(i) == last) {
      // cout << "i: " << i <<endl;
      j = i;
    }
  }
  /*
    for (ll i = j; i < loop.size(); i++)
    {
      cout << loop.at(i) + 1 << " ";
    }
    cout << endl;
  */

  ll eles = loop.size() - j;
  ll ans;
  if (k < count - eles) {
    ll now = 0;
    for (int i = 0; i < k; i++) {
      ll nextposit = a.at(now);
    }

    ans = now;
  } else {
    ll rep = k - count; // あとの矢印の数
    ll diff = rep % eles;
    // cout << j << " " << count << " " << rep << " " << eles << " " <<  diff <<
    // endl;
    ans = loop.at(j + diff);
  }

  cout << ans + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<int> A(N);
  for (int n1 = 0; n1 < N; ++n1) {
    cin >> A[n1];
  }

  // ついた町リスト
  vector<int> B;
  vector<int> C(N + 1);
  map<int, int> map;

  int nNow = 1;
  int a = 0;
  int b = 0;
  for (int n2 = 0; n2 < N; ++n2) {
    B.push_back(nNow);
    C[nNow] = 1;
    map[nNow] = n2;
    nNow = A[nNow - 1];

    if (1 == C[nNow]) {
      a = map[nNow];
      break;
    }
  }
  // abcd(b)
  // a= 1

  b = B.size() - a;

  int nRet = -1;
  if (K < (long long)B.size()) {
    nRet = B[K];
  } else {
    long long c = (K - a) % (b) + a;
    nRet = B[c];
  }

  cout << nRet << endl;
  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<int> A(N);
  for (int n1 = 0; n1 < N; ++n1) {
    cin >> A[n1];
  }

  // ついた町リスト
  vector<int> B;
  vector<int> C(N);
  map<int, int> map;

  int nNow = 1;
  int a = 0;
  int b = 0;
  for (int n2 = 0; n2 < N; ++n2) {
    B.push_back(nNow);
    C[nNow] = 1;
    map[nNow] = n2;
    nNow = A[nNow - 1];

    if (1 == C[nNow]) {
      a = map[nNow];
      break;
    }
  }
  // abcd(b)
  // a= 1
  cout << "2" << endl;
  return 0;

  b = B.size() - a;

  int nRet = -1;
  if (K < (long long)B.size()) {
    nRet = B[K];
  } else {
    long long c = (K - a) % (b) + a;
    nRet = B[c];
  }

  cout << nRet << endl;
  return 0;
}