Problem p04022 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define seg_size 524288
map<long long, long long> kouho;
vector<long long> sosuu;
long long gogo[1000000];
long long sol(long long now) {
  long long ans = 1;
  for (int i = 0; i < sosuu.size(); ++i) {
    if (now % sosuu[i] == 0) {
      if (now % (sosuu[i] * sosuu[i]) == 0) {
        ans *= sosuu[i];
        if (ans > 1e11) {
          return ans;
        }
        now /= (sosuu[i] * sosuu[i]);
      } else {
        ans *= sosuu[i];
        if (ans > 1e11) {
          return ans;
        }
        ans *= sosuu[i];
        if (ans > 1e11) {
          return ans;
        }
        now /= sosuu[i];
      }
      i--;
    }
  }
  long long geko = sqrt(now);
  if (geko * geko == now) {
    if (gogo[geko] == 0) {
      // prime number
      if (ans > 1e18 / geko) {
        return 1e18;
      }
      return ans * geko;
    }
  }
  if (ans > 1e18 / now) {
    return 1e18;
  }
  ans *= now;
  if (ans > 1e18 / now) {
    return 1e18;
  }
  ans *= now;
  return ans;
}
int main() {
  for (long long i = 2; i < 1000000; ++i) {
    if (gogo[i] == 0) {
      if (i < 10000) {
        sosuu.push_back(i);
      }
      for (long long q = 2; i * q < 10000; ++q) {
        gogo[i * q] = 1;
      }
    }
  }
  long long n;
  cin >> n;
  long long ans = 0;
  REP(i, n) {
    long long a;
    cin >> a;
    for (long long q = 0; q < sosuu.size(); ++q) {
      long long geko = sosuu[q] * sosuu[q] * sosuu[q];
      if (geko > a) {
        break;
      }
      if (a % geko == 0) {
        a /= geko;
        q--;
      }
    }
    if (a == 1)
      ans = 1;
    else
      kouho[a]++;
  }
  for (auto i = kouho.begin(); i != kouho.end(); ++i) {
    long long nya = sol(i->first);
    ans += max(i->second, kouho[nya]);
    kouho[nya] = 0;
    i->second = 0;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define seg_size 524288
map<long long, long long> kouho;
vector<long long> sosuu;
long long gogo[1000000];
long long sol(long long now) {
  long long ans = 1;
  for (int i = 0; i < sosuu.size(); ++i) {
    if (now % sosuu[i] == 0) {
      if (now % (sosuu[i] * sosuu[i]) == 0) {
        ans *= sosuu[i];
        if (ans > 1e11) {
          return ans;
        }
        now /= (sosuu[i] * sosuu[i]);
      } else {
        ans *= sosuu[i];
        if (ans > 1e11) {
          return ans;
        }
        ans *= sosuu[i];
        if (ans > 1e11) {
          return ans;
        }
        now /= sosuu[i];
      }
      i--;
    }
  }
  long long geko = sqrt(now);
  if (geko * geko == now) {
    if (gogo[geko] == 0) {
      // prime number
      if (ans > 1e18 / geko) {
        return 1e18;
      }
      return ans * geko;
    }
  }
  if (ans > 1e18 / now) {
    return 1e18;
  }
  ans *= now;
  if (ans > 1e18 / now) {
    return 1e18;
  }
  ans *= now;
  return ans;
}
int main() {
  for (long long i = 2; i < 1000000; ++i) {
    if (gogo[i] == 0) {
      if (i < 10000) {
        sosuu.push_back(i);
      }
      for (long long q = 2; i * q < 10000; ++q) {
        gogo[i * q] = 1;
      }
    }
  }
  long long n;
  cin >> n;
  long long ans = 0;
  REP(i, n) {
    long long a;
    cin >> a;
    for (long long q = 0; q < sosuu.size(); ++q) {
      long long geko = sosuu[q] * sosuu[q] * sosuu[q];
      if (geko > a) {
        break;
      }
      if (a % geko == 0) {
        a /= geko;
        q--;
      }
    }
    if (a == 1)
      ans = 1;
    else
      kouho[a]++;
  }
  for (auto i = kouho.begin(); i != kouho.end(); ++i) {
    long long nya = sol(i->first);
    if (kouho.find(nya) == kouho.end()) {
      ans += i->second;
      i->second = 0;
      continue;
    }
    ans += max(i->second, kouho[nya]);
    kouho[nya] = 0;
    i->second = 0;
  }
  cout << ans << endl;
  return 0;
}