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

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

// https://ei1333.github.io/luzhiled/snippets/math/divisor.html
vector<int> divisor(int n) {
  vector<int> ret;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

map<int, int> prime_factor(int n) {
  map<int, int> ret;
  for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

int myGCD(int a, int b) {
  if (b == 0) {
    return a;
  }

  return myGCD(b, a % b);
}

int main() {
  int N;
  int A[1001001];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  bool s1[1001001];
  for (int i = 0; i < 1001001; i++) {
    s1[i] = false;
  }

  int gcd = A[0];

  bool isPairwiseCoprime = true;
  for (int i = 0; i < N; i++) {
    map<int, int> div = prime_factor(A[i]);
    for (auto it = div.begin(); it != div.end(); it++) {
      int num = (*it).first;
      if (num == 1) {
        continue;
      }
      if (s1[num]) {
        isPairwiseCoprime = false;
      } else {
        s1[num] = true;
      }
    }

    if (gcd > 1) {
      gcd = myGCD(gcd, A[i]);
    }
  }

  if (isPairwiseCoprime) {
    cout << "pairwise coprime" << endl;
  } else if (gcd == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

// https://ei1333.github.io/luzhiled/snippets/math/divisor.html
vector<int> divisor(int n) {
  vector<int> ret;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

int myGCD(int a, int b) {
  if (b == 0) {
    return a;
  }

  return myGCD(b, a % b);
}

int main() {
  int N;
  int A[1001001];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  bool s1[1001001];
  for (int i = 0; i < 1001001; i++) {
    s1[i] = false;
  }

  int gcd = A[0];

  bool isPairwiseCoprime = true;
  for (int i = 0; i < N; i++) {
    vector<int> div = divisor(A[i]);
    for (int j = 0; j < div.size(); j++) {
      if (div[j] == 1) {
        continue;
      }
      if (s1[div[j]]) {
        isPairwiseCoprime = false;
      } else {
        s1[div[j]] = true;
      }
    }

    gcd = myGCD(gcd, A[i]);
  }

  if (isPairwiseCoprime) {
    cout << "pairwise coprime" << endl;
  } else if (gcd == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}