Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

const unsigned long long BASE = (unsigned long long)(1e9 + 7);

#define FROM_0(i, n) for (int i = 0; i < (n); i++)
#define FROM_S(i, s, e) for (int i = (s); i < (e); i++)
#define ITER_BEGIN_END(iter, vec)                                              \\
  for (auto iter = vec.begin(); iter != vec.end(); iter++)

bool isP(int n) {
  int end = sqrt(n);
  FROM_S(j, 2, end + 1) {
    if (n % j == 0)
      return false;
  }
  return true;
}

int gcd(int a, int b) {
  if (b > a)
    std::swap(a, b);
  while (b != 0) {
    a = a % b;
    std::swap(a, b);
  }
  return a;
}

int main() {
  int n;
  cin >> n;
  int moreThan1 = 0;
  int allGcd = 1;
  int *a = new int[n];
  FROM_0(i, n) {
    cin >> a[i];
    if (a[i] > 1)
      moreThan1++;
    if (i == 0)
      allGcd = a[i];
    else
      allGcd = gcd(allGcd, a[i]);
  }
  if (allGcd != 1) {
    cout << "not coprime" << endl;
    return 0;
  } else if (moreThan1 >= 78500) {
    cout << "setwise coprime" << endl;
    return 0;
  }
  // check pairwise coprime
  set<int> prime;
  prime.insert(2);
  for (int i = 3; i < 1000000; i += 2) {
    if (isP(i))
      prime.insert(i);
  }
  FROM_0(i, n) {
    int number = a[i];
    if (number == 1)
      continue;
    if (isP(number)) {
      auto pFind = prime.find(number);
      if (pFind != prime.end()) {
        prime.erase(pFind);
        continue;
      } else {
        cout << "setwise coprime" << endl;
        return 0;
      }
    }
    for (auto pPrime = prime.begin(); pPrime != prime.end();) {
      if (*pPrime > number)
        break;
      if (number % *pPrime == 0) {
        do {
          number /= *pPrime;
        } while (number % *pPrime == 0);
        pPrime = prime.erase(pPrime);
      } else
        pPrime++;
    }
    if (number > 1) {
      if (isP(number)) {
        auto pFind = prime.find(number);
        if (pFind != prime.end()) {
          prime.erase(pFind);
          continue;
        } else {
          cout << "setwise coprime" << endl;
          return 0;
        }
      }
    }
  }
  cout << "pairwise coprime" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

const unsigned long long BASE = (unsigned long long)(1e9 + 7);

#define FROM_0(i, n) for (int i = 0; i < (n); i++)
#define FROM_S(i, s, e) for (int i = (s); i < (e); i++)
#define ITER_BEGIN_END(iter, vec)                                              \\
  for (auto iter = vec.begin(); iter != vec.end(); iter++)

bool isP(int n) {
  int end = sqrt(n);
  FROM_S(j, 2, end + 1) {
    if (n % j == 0)
      return false;
  }
  return true;
}

int gcd(int a, int b) {
  if (b > a)
    std::swap(a, b);
  while (b != 0) {
    a = a % b;
    std::swap(a, b);
  }
  return a;
}

int main() {
  int n;
  cin >> n;
  int moreThan1 = 0;
  int allGcd = 1;
  int *a = new int[n];
  FROM_0(i, n) {
    cin >> a[i];
    if (a[i] > 1)
      moreThan1++;
    if (i == 0)
      allGcd = a[i];
    else
      allGcd = gcd(allGcd, a[i]);
  }
  if (allGcd != 1) {
    cout << "not coprime" << endl;
    return 0;
  } else if (moreThan1 >= 78500) {
    cout << "setwise coprime" << endl;
    return 0;
  }
  // check pairwise coprime
  vector<int> prime;
  prime.reserve(78500);
  prime.push_back(2);
  for (int i = 3; i < 1000000; i += 2) {
    if (isP(i))
      prime.push_back(i);
  }
  set<int> factor;
  FROM_0(i, n) {
    int number = a[i];
    ITER_BEGIN_END(pPrime, prime) {
      if (*pPrime > number)
        break;
      if (number % *pPrime == 0) {
        do {
          number /= *pPrime;
        } while (number % *pPrime == 0);
        auto pFind = factor.find(*pPrime);
        if (pFind != factor.end()) {
          cout << "setwise coprime" << endl;
          return 0;
        }
        factor.insert(*pPrime);
      }
    }
  }
  cout << "pairwise coprime" << endl;
  return 0;
}