Problem p04022 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
// #define int long long
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;
typedef complex<ld> com;
constexpr ll INF = 1000000000000000000;
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<bool> is_prime(100010, true);
vector<ll> prime;

void prime_init() {
  for (int i = 2; i < 100010; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j < 100010; j += i) {
        is_prime[j] = false;
      }
      prime.pb(i);
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  prime_init();

  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> mp;
  rep(i, n) {
    for (ll j = 2; j * j * j <= a[i]; j++) {
      while (a[i] % (j * j * j) == 0)
        a[i] /= j * j * j;
    }
    mp[a[i]]++;
  }
  int ans = 0;
  ll maxval = 10000000010;
  for (auto i : mp) {
    if (i.second == 0)
      continue;
    ll num = i.first;
    if (num == 1)
      ans++;
    else {
      ll rev = 1;
      ll k = num;
      bool flag = true;
      for (ll idx = 0; prime[idx] * prime[idx] <= k; idx++) {
        ll j = prime[idx];
        if (k % (j * j) == 0) {
          k /= j * j;
          if (j >= maxval / rev) {
            flag = false;
            break;
          }
          rev *= j;
        }
        if (k % j == 0) {
          k /= j;
          if (j >= maxval / rev) {
            flag = false;
            break;
          }
          rev *= j;
          if (j >= maxval / rev) {
            flag = false;
            break;
          }
          rev *= j;
        }
      }
      if (k > 1) {
        if (k >= maxval / rev) {
          flag = false;
        }
        if (flag)
          rev *= k;
        if (k >= maxval / rev) {
          flag = false;
        }
        if (flag)
          rev *= k;
      }
      if (flag) {
        ans += max(i.second, mp[rev]);
        mp[rev] = 0;
      } else {
        ans += i.second;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
// #define int long long
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;
typedef complex<ld> com;
constexpr ll INF = 1000000000000000000;
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<bool> is_prime(100010, true);
vector<ll> prime;

void prime_init() {
  for (int i = 2; i < 100010; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j < 100010; j += i) {
        is_prime[j] = false;
      }
      prime.pb(i);
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  prime_init();

  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> mp;
  rep(i, n) {
    for (int idx = 0; prime[idx] * prime[idx] * prime[idx] <= a[i]; idx++) {
      ll j = prime[idx];
      while (a[i] % (j * j * j) == 0)
        a[i] /= j * j * j;
    }
    mp[a[i]]++;
  }
  int ans = 0;
  ll maxval = 10000000010;
  for (auto i : mp) {
    if (i.second == 0)
      continue;
    ll num = i.first;
    if (num == 1)
      ans++;
    else {
      ll rev = 1;
      ll k = num;
      bool flag = true;
      for (ll idx = 0; prime[idx] * prime[idx] <= k; idx++) {
        ll j = prime[idx];
        if (k % (j * j) == 0) {
          k /= j * j;
          if (j >= maxval / rev) {
            flag = false;
            break;
          }
          rev *= j;
        }
        if (k % j == 0) {
          k /= j;
          if (j >= maxval / rev) {
            flag = false;
            break;
          }
          rev *= j;
          if (j >= maxval / rev) {
            flag = false;
            break;
          }
          rev *= j;
        }
      }
      if (k > 1) {
        if (k >= maxval / rev) {
          flag = false;
        }
        if (flag)
          rev *= k;
        if (k >= maxval / rev) {
          flag = false;
        }
        if (flag)
          rev *= k;
      }
      if (flag) {
        ans += max(i.second, mp[rev]);
        mp[rev] = 0;
      } else {
        ans += i.second;
      }
    }
  }
  cout << ans << endl;
}