Problem p04022 (C++)

Buggy submission (status: Runtime Error):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

constexpr ll inf = 1e+14;

class DAnticube {
public:
  ll find_pair(ll p, vector<ll> &primes) {
    map<ll, int> mp;
    int idx = 0;
    while (primes[idx] * primes[idx] <= p) {
      while (p % primes[idx] == 0) {
        mp[primes[idx]]++;
        p /= primes[idx];
      }
      idx++;
    }
    if (p > 1)
      mp[p]++;

    ll ret = 1;
    for (auto e : mp) {
      if (e.second == 1) {
        if (ret < inf / (e.first * e.first)) {
          ret *= e.first;
          ret *= e.first;
        } else {
          return inf;
        }
      } else {
        assert(e.second == 2);
        if (ret < inf / e.first) {
          ret *= e.first;
        } else {
          return inf;
        }
      }
    }
    return ret;
  }

  void solve(istream &cin, ostream &cout) {
    SPEED;
    int n;
    cin >> n;

    vector<bool> check(100001, true);
    check[0] = false;
    check[1] = false;
    for (int i = 2; i <= 100000; i++) {
      if (check[i]) {
        for (int j = i + i; j <= 100000; j += i) {
          check[j] = false;
        }
      }
    }
    vector<ll> primes;
    for (int i = 2; i <= 100000; i++) {
      if (check[i])
        primes.push_back(i);
    }

    vector<ll> s(n);
    rep(i, n) { cin >> s[i]; }
    map<ll, int> mp;
    rep(i, n) {
      for (ll j = 2; j * j * j <= s[i]; j++) {
        while (s[i] % (j * j * j) == 0) {
          s[i] /= j * j * j;
        }
      }
      mp[s[i]]++;
    }

    int ans = 0;
    for (auto &e : mp) {
      if (e.first == 1) {
        ans++;
        continue;
      }
      ll p = find_pair(e.first, primes);
      int now = e.second;
      if (mp.find(p) != mp.end()) {
        chmax(now, mp[p]);
        mp[p] = 0;
        e.second = 0;
      }
      ans += now;
    }

    cout << ans << endl;
  }
};

signed main() {
  DAnticube solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}

Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

constexpr ll inf = 1e+14;

class DAnticube {
public:
  ll find_pair(ll p, vector<ll> &primes) {
    map<ll, int> mp;
    int idx = 0;
    while (idx < primes.size() && primes[idx] * primes[idx] <= p) {
      while (p % primes[idx] == 0) {
        mp[primes[idx]]++;
        p /= primes[idx];
      }
      idx++;
    }
    if (p > 1)
      mp[p]++;

    ll ret = 1;
    for (auto e : mp) {
      if (e.second == 1) {
        if (ret < inf / (e.first * e.first)) {
          ret *= e.first;
          ret *= e.first;
        } else {
          return inf;
        }
      } else {
        assert(e.second == 2);
        if (ret < inf / e.first) {
          ret *= e.first;
        } else {
          return inf;
        }
      }
    }
    return ret;
  }

  void solve(istream &cin, ostream &cout) {
    SPEED;
    int n;
    cin >> n;

    vector<bool> check(100001, true);
    check[0] = false;
    check[1] = false;
    for (int i = 2; i <= 100000; i++) {
      if (check[i]) {
        for (int j = i + i; j <= 100000; j += i) {
          check[j] = false;
        }
      }
    }
    vector<ll> primes;
    for (int i = 2; i <= 100000; i++) {
      if (check[i])
        primes.push_back(i);
    }

    vector<ll> s(n);
    rep(i, n) { cin >> s[i]; }
    map<ll, int> mp;
    rep(i, n) {
      for (ll j = 2; j * j * j <= s[i]; j++) {
        while (s[i] % (j * j * j) == 0) {
          s[i] /= j * j * j;
        }
      }
      mp[s[i]]++;
    }

    int ans = 0;
    for (auto &e : mp) {
      if (e.first == 1) {
        ans++;
        continue;
      }
      ll p = find_pair(e.first, primes);
      int now = e.second;
      if (mp.find(p) != mp.end()) {
        chmax(now, mp[p]);
        mp[p] = 0;
        e.second = 0;
      }
      ans += now;
    }

    cout << ans << endl;
  }
};

signed main() {
  DAnticube solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}