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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef unsigned long long ll;
const int MAX_N = 1e6;
vector<bool> isp(MAX_N + 1, true);
vector<int> primeNumbers;

void sieve() {
  isp[0] = false;
  isp[1] = false;
  for (int i = 2; i * i <= MAX_N; i++) {
    if (isp[i] == true) {
      for (int j = 2; i * j <= MAX_N; j++) {
        isp[i * j] = false;
      }
    }
  }
  for (int i = 2; i <= MAX_N; i++) {
    if (isp[i] == true)
      primeNumbers.push_back(i);
  }
}

bool isPrime(int num) {
  if (num == 0 || num == 1)
    return false;
  int n = primeNumbers.size();
  for (int i = 0; primeNumbers[i] * primeNumbers[i] <= num; i++) {
    if (num == primeNumbers[i])
      return true;
    if (num % primeNumbers[i] == 0)
      return false;
  }
  return true;
}

int gcd(int a, int b) {
  if (a < b)
    return gcd(b, a);
  int r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  sieve();
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  set<int> factorList;
  bool isPairWise = true;

  for (int i = 0; i < n; i++) {
    if (isPrime(A[i]) == true) {
      if (factorList.count(A[i]) != 0) {
        isPairWise = false;
        break;
      } else {
        factorList.insert(A[i]);
      }
    } else {
      for (auto iter = primeNumbers.begin();
           iter != primeNumbers.end() && (*iter) <= A[i]; iter++) {
        if (A[i] % *iter == 0) {
          if (factorList.count(*iter) == 0) {
            factorList.insert(*iter);
          } else {
            isPairWise = false;
            break;
          }
        }
      }
    }
  }

  int cumProduct = gcd(A[0], A[1]);
  for (int i = 2; i < n; i++) {
    cumProduct = gcd(cumProduct, A[i]);
  }
  bool isSetWise;
  if (cumProduct == 1) {
    isSetWise = true;
  } else {
    isSetWise = false;
  }
  if (isPairWise == true) {
    cout << "pairwise coprime" << endl;
  } else if (isSetWise == true) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef unsigned long long ll;
const int MAX_N = 1e6;
vector<bool> isp(MAX_N + 1, true);
vector<int> primeNumbers;

void sieve() {
  isp[0] = false;
  isp[1] = false;
  for (int i = 2; i * i <= MAX_N; i++) {
    if (isp[i] == true) {
      for (int j = 2; i * j <= MAX_N; j++) {
        isp[i * j] = false;
      }
    }
  }
  for (int i = 2; i <= MAX_N; i++) {
    if (isp[i] == true)
      primeNumbers.push_back(i);
  }
}

bool isPrime(int num) {
  if (num == 0 || num == 1)
    return false;
  int n = primeNumbers.size();
  for (int i = 0; primeNumbers[i] * primeNumbers[i] <= num; i++) {
    if (num == primeNumbers[i])
      return true;
    if (num % primeNumbers[i] == 0)
      return false;
  }
  return true;
}

int gcd(int a, int b) {
  if (a < b)
    return gcd(b, a);
  int r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  sieve();
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  set<int> factorList;
  bool isPairWise = true;

  for (int i = 0; i < n; i++) {
    if (isPrime(A[i]) == true) {
      if (factorList.count(A[i]) != 0) {
        isPairWise = false;
        break;
      } else {
        factorList.insert(A[i]);
      }
    } else {
      for (auto iter = primeNumbers.begin(); iter != primeNumbers.end();
           iter++) {
        if (A[i] % *iter == 0) {
          if (factorList.count(*iter) == 0) {
            factorList.insert(*iter);
          } else {
            isPairWise = false;
            break;
          }
        }
      }
    }
  }

  int cumProduct = gcd(A[0], A[1]);
  for (int i = 2; i < n; i++) {
    cumProduct = gcd(cumProduct, A[i]);
  }
  bool isSetWise;
  if (cumProduct == 1) {
    isSetWise = true;
  } else {
    isSetWise = false;
  }
  if (isPairWise == true) {
    cout << "pairwise coprime" << endl;
  } else if (isSetWise == true) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
  return 0;
}