Problem p04022 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-7;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

const ll MAX = 1e10;

vl SieveEratos(int n, map<ll, ll> &m) {
  vl a(n + 1, 1), b;
  for (ll i = 2; i <= n; i++)
    if (a[i]) {
      if (i * i * i <= MAX)
        b.push_back(i);
      m[i * i] = i;
      b.push_back(i);
      for (int j = i; j <= n; j += i)
        a[j] = 0;
    }
  return b;
}

int n;
vl a;

int main() {
  cin >> n;
  set<ll> st;
  map<ll, ll> m, m1, m2;
  a = SieveEratos(1e5, m);
  int res = 0, S = a.size(), B = 1;
  for (int i = 0; i < n; i++) {
    ll A, Norm = 1, Pair = 1;
    cin >> A;
    for (int j = 0; j < S; j++) {
      int cnt = 0, p = a[j];
      while (A % p == 0) {
        A /= p;
        cnt++;
      }
      cnt %= 3;
      if (cnt == 1) {
        Norm *= p;
        Pair *= p * p;
      }
      if (cnt == 2) {
        Norm *= p * p;
        Pair *= p;
      }
    }
    Norm *= A;
    if (Norm == 1) {
      res += 2 * B;
      B = 0;
      continue;
    }
    st.insert(Norm);
    m1[Norm]++;
    if (m.find(A) != m.end())
      Pair *= m[A];
    else if (A < 1000000)
      Pair *= A * A;
    else {
      res++;
      continue;
    }
    st.insert(Pair);
    m2[Pair]++;
  }
  for (auto i = st.begin(); i != st.end(); i++)
    res += max(m1[*i], m2[*i]);
  cout << res / 2 << endl;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-7;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

const ll MAX = 1e10;

vl SieveEratos(int n, map<ll, ll> &m) {
  vl a(n + 1, 1), b;
  for (ll i = 2; i <= n; i++)
    if (a[i]) {
      if (i * i * i <= MAX)
        b.push_back(i);
      m[i * i] = i;
      for (int j = i; j <= n; j += i)
        a[j] = 0;
    }
  return b;
}

int n;
vl a;

int main() {
  cin >> n;
  set<ll> st;
  map<ll, ll> m, m1, m2;
  a = SieveEratos(1e5, m);
  int res = 0, S = a.size(), B = 1;
  for (int i = 0; i < n; i++) {
    ll A, Norm = 1, Pair = 1;
    cin >> A;
    for (int j = 0; j < S; j++) {
      int cnt = 0, p = a[j];
      while (A % p == 0) {
        A /= p;
        cnt++;
      }
      cnt %= 3;
      if (cnt == 1) {
        Norm *= p;
        Pair *= p * p;
      }
      if (cnt == 2) {
        Norm *= p * p;
        Pair *= p;
      }
    }
    Norm *= A;
    if (Norm == 1) {
      res += 2 * B;
      B = 0;
      continue;
    }
    st.insert(Norm);
    m1[Norm]++;
    if (m.find(A) != m.end())
      Pair *= m[A];
    else if (A < 1000000)
      Pair *= A * A;
    else {
      res++;
      continue;
    }
    st.insert(Pair);
    m2[Pair]++;
  }
  for (auto i = st.begin(); i != st.end(); i++)
    res += max(m1[*i], m2[*i]);
  cout << res / 2 << endl;
}