Problem p04022 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define long long long
#define up(i, a, b) for (int i = a; i <= b; i++)
#define down(i, a, b) for (int i = a; i >= b; i--)
#define endl '\n'
#define X first
#define Y second
#define II pair<int, int>
#define III pair<int, pair<int, int>>
#define debug(X) cerr << #X << " = " << X << endl
#define debug2(X, Y) cerr << #X << " = " << X << "," << #Y << " = " << Y << endl
#define show(X, a, b)                                                          \
  {                                                                            \
    cerr << #X << " = ";                                                       \
    up(__, a, b) cerr << X[__] << ' ';                                         \
    cerr << endl;                                                              \
  }
#define gc getchar
#define pc putchar
using namespace std;

inline void read(int &x) {
  register int c = gc();
  x = 0;
  int neg = 0;
  for (; ((c < 48 || c > 57) && c != '-'); c = gc())
    ;
  if (c == '-') {
    neg = 1;
    c = gc();
  }
  for (; c > 47 && c < 58; c = gc()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg)
    x = -x;
}
inline void writeln(int x) {

  char buffor[21];
  register int i = 0;
  int neg = 0;
  if (x < 0) {
    neg = 1;
    x = -x;
  }
  do {
    buffor[i++] = (x % 10) + '0';
    x /= 10;
  } while (x);
  i--;
  if (neg)
    pc('-');
  while (i >= 0)
    pc(buffor[i--]);
  pc('\n');
}

const int N = (int)1e5 + 10;
const long oo = (long)1e10 + 1;
int n;
long a[N], rev[N];
map<long, int> cnt;

void input() {
  cin >> n;
  up(i, 1, n) cin >> a[i];
}

void solve() {

  up(i, 1, n) {
    rev[i] = 1;
    long A = a[i];
    long remain = a[i];
    a[i] = 1;
    for (long j = 2; j * j * j <= A; j++) {
      long j3 = j * j * j;
      while (remain % j3 == 0)
        remain /= j3;
      long j2 = j * j;
      if (remain % j2 == 0) {
        rev[i] *= j;
        a[i] *= j2;
      } else if (remain % j == 0) {
        rev[i] *= j2;
        a[i] *= j;
      };
      if (rev[i] > oo)
        rev[i] = oo;

      while (remain % j == 0)
        remain /= j;
    }

    a[i] *= remain;
    long can = (long)sqrt(remain);
    if (can * can == remain) {
      rev[i] *= can;
      if (rev[i] > oo)
        rev[i] = oo;
    } else {
      rev[i] *= remain;
      if (rev[i] > oo)
        rev[i] = oo;
      rev[i] *= remain;
      if (rev[i] > oo)
        rev[i] = oo;
    };

    cnt[a[i]]++;
  }
  int res = 0;
  up(i, 1, n) {
    if (cnt[a[i]] == 0)
      continue;
    if (a[i] == 1) {
      res++;
      cnt[a[i]] = 0;
    } else {
      res += max(cnt[a[i]], cnt[rev[i]]);
      cnt[a[i]] = cnt[rev[i]] = 0;
    }
  }
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); // don't use when interactive

#ifdef I_Love_Pork
#define TASK "tmp"
  freopen(TASK ".inp", "r", stdin);
  freopen(TASK ".out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define long long long
#define up(i, a, b) for (int i = a; i <= b; i++)
#define down(i, a, b) for (int i = a; i >= b; i--)
#define endl '\n'
#define X first
#define Y second
#define II pair<int, int>
#define III pair<int, pair<int, int>>
#define debug(X) cerr << #X << " = " << X << endl
#define debug2(X, Y) cerr << #X << " = " << X << "," << #Y << " = " << Y << endl
#define show(X, a, b)                                                          \
  {                                                                            \
    cerr << #X << " = ";                                                       \
    up(__, a, b) cerr << X[__] << ' ';                                         \
    cerr << endl;                                                              \
  }
#define gc getchar
#define pc putchar
using namespace std;

inline void read(int &x) {
  register int c = gc();
  x = 0;
  int neg = 0;
  for (; ((c < 48 || c > 57) && c != '-'); c = gc())
    ;
  if (c == '-') {
    neg = 1;
    c = gc();
  }
  for (; c > 47 && c < 58; c = gc()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg)
    x = -x;
}
inline void writeln(int x) {

  char buffor[21];
  register int i = 0;
  int neg = 0;
  if (x < 0) {
    neg = 1;
    x = -x;
  }
  do {
    buffor[i++] = (x % 10) + '0';
    x /= 10;
  } while (x);
  i--;
  if (neg)
    pc('-');
  while (i >= 0)
    pc(buffor[i--]);
  pc('\n');
}

const int N = (int)1e5 + 10;
const long oo = (long)1e10 + 1;
int n;
long a[N], rev[N];
map<long, int> cnt;

void input() {
  cin >> n;
  up(i, 1, n) cin >> a[i];
}

void solve() {

  up(i, 1, n) {
    rev[i] = 1;
    long A = a[i];
    long remain = a[i];
    a[i] = 1;
    for (long j = 2; j <= 320; j++) {
      long j3 = j * j * j;
      while (remain % j3 == 0)
        remain /= j3;
      long j2 = j * j;
      if (remain % j2 == 0) {
        rev[i] *= j;
        a[i] *= j2;
      } else if (remain % j == 0) {
        rev[i] *= j2;
        a[i] *= j;
      };
      if (rev[i] > oo)
        rev[i] = oo;

      while (remain % j == 0)
        remain /= j;
    }

    a[i] *= remain;
    long can = (long)sqrt(remain);
    if (can * can == remain) {
      rev[i] *= can;
      if (rev[i] > oo)
        rev[i] = oo;
    } else {
      rev[i] *= remain;
      if (rev[i] > oo)
        rev[i] = oo;
      rev[i] *= remain;
      if (rev[i] > oo)
        rev[i] = oo;
    };

    cnt[a[i]]++;
  }
  int res = 0;
  up(i, 1, n) {
    if (cnt[a[i]] == 0)
      continue;
    if (a[i] == 1) {
      res++;
      cnt[a[i]] = 0;
    } else {
      res += max(cnt[a[i]], cnt[rev[i]]);
      cnt[a[i]] = cnt[rev[i]] = 0;
    }
  }
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); // don't use when interactive

#ifdef I_Love_Pork
#define TASK "tmp"
  freopen(TASK ".inp", "r", stdin);
  freopen(TASK ".out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}