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


/* You are being watched. */

//~ while (clock() <= 69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")

using namespace std;

//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template <typename T>
//~ using ordered_set =
//~ tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) (x).begin(), (x).end()
#define show(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\\n"
#define FastIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
// using li = __int128;
// using uli = unsigned __int128;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

double getTime() { return clock() / (double)CLOCKS_PER_SEC; }

void read(){};

template <typename T, typename... Args> void read(T &a, Args &...args) {
  cin >> a;
  read(args...);
}

void print(){};

template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " \\n"[sizeof...(args) == 0];
  print(args...);
}

const int N = 2e5 + 5;

int n;
int go[N];

void solve() {
  cin >> n;
  ll k;
  cin >> k;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    go[i] = x;
  }
  int p = 1;
  int who = -1;
  set<int> s;
  while (true) {
    p = go[p];
    if (s.count(p)) {
      who = p;
      break;
    }
    s.insert(p);
  }
  int old = who;
  vector<int> cyc;
  cyc.push_back(who);
  while (true) {
    who = go[who];
    if (who == old)
      break;
    cyc.push_back(who);
  }
  int cur = 1;
  while (true) {
    cur = go[cur];
    k--;
    if (cur == old) {
      break;
    }
    if (k == 0) {
      print(cur);
      return;
    }
  }
  int m = cyc.size();
  cout << cyc[k % m] << '\\n';
}

int main() {
  int t;
  t = 1;
  while (t--)
    solve();
}


Error: WA

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef tuple<int, int, int> ti3;
typedef tuple<int, int, int, int> ti4;
typedef stack<int> si;
typedef queue<int> qi;
typedef priority_queue<int> pqi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef tuple<ll, ll, ll> tl3;
typedef tuple<ll, ll, ll, ll> tl4;
typedef stack<ll> sl;
typedef queue<ll> ql;
typedef priority_queue<ll> pql;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {0, 0, 1, 1, 1, -1, -1, -1}, ddy[8] = {1, -1, 1, 0, -1, 1, 0, -1};
ll MOD = 1000000000;
ll POW(ll a, ll b, ll MMM = MOD) {
  ll ret = 1;
  for (; b; b >>= 1, a = (a * a) % MMM)
    if (b & 1)
      ret = (ret * a) % MMM;
  return ret;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
  if (a == 0 || b == 0)
    return a + b;
  return a * (b / gcd(a, b));
}
bool OOB(int x, int y, int N, int M) {
  return 0 > x || x >= N || 0 > y || y >= M;
}
#define X first
#define Y second
#define rep(i, a, b) for (int i = a; i < b; i++)
#define pb(x) push_back(x)
#define all(x) (x).begin(), (x).end()
#define sf1(a) cin >> a
#define sf2(a, b) cin >> a >> b
#define sf3(a, b, c) cin >> a >> b >> c
#define sf4(a, b, c, d) cin >> a >> b >> c >> d
#define sf5(a, b, c, d, e) cin >> a >> b >> c >> d >> e
#define sf6(a, b, c, d, e, f) cin >> a >> b >> c >> d >> e >> f
#define pf1(a) cout << a << ' '
#define pf2(a, b) cout << a << ' ' << b << ' '
#define pf3(a, b, c) cout << a << ' ' << b << ' ' << c << ' '
#define pf4(a, b, c, d) cout << a << ' ' << b << ' ' << c << ' ' << d << ' '
#define pf5(a, b, c, d, e)                                                     \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << ' '
#define pf6(a, b, c, d, e, f)                                                  \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << ' ' << f << ' '
#define pf1l(a) cout << a << '\\n'
#define pf2l(a, b) cout << a << ' ' << b << '\\n'
#define pf3l(a, b, c) cout << a << ' ' << b << ' ' << c << '\\n'
#define pf4l(a, b, c, d) cout << a << ' ' << b << ' ' << c << ' ' << d << '\\n'
#define pf5l(a, b, c, d, e)                                                    \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << '\\n'
#define pf6l(a, b, c, d, e, f)                                                 \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << ' ' << f << '\\n'
int K, N;
ll prime[104];
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  sf2(K, N);
  set<ll> S;
  rep(i, 0, K) {
    sf1(prime[i]);
    S.insert(prime[i]);
  }
  int cnt = 0;
  rep(i, 0, N - 1) {
    ll t = *S.begin();
    S.erase(S.begin());
    rep(i, 0, K) {
      ll new_elem = t * prime[i];
      if (new_elem > 0x7fffffff)
        continue;
      if (S.find(new_elem) != S.end())
        continue;
      if (cnt > N && new_elem > *prev(S.end()))
        continue;
      cnt++;
      S.insert(new_elem);
    }
  }
  pf1(*S.begin());
}