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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  int N;
  cin >> N;
  VI A(N);
  rep(i, N) cin >> A[i];
  int X = A[0];
  rep(i, N) X = gcd(X, A[i]);
  if (X != 1) {
    cout << "not coprime" << endl;
    return 0;
  }
  VI PP;
  PP.pb(2);
  rep(i, 1000) {
    int F = 1;
    rep(j, PP.size()) {
      if ((i + 3) % PP[j] == 0) {
        F = 0;
        break;
      }
    }
    if (F)
      PP.pb(i + 3);
  }
  map<int, int> M;
  rep(i, N) {
    rep(j, PP.size()) {
      if (A[i] % PP[j] == 0) {
        M[PP[j]]++;
        while (A[i] % PP[j] == 0) {
          A[i] /= PP[j];
        }
      }
    }
    if (A[i] != 1)
      M[A[i]]++;
  }
  auto itr = M.begin();
  int ans = 0;
  rep(i, M.size()) {
    if (itr->second != 1)
      ans = 1;
    itr++;
  }

  if (ans)
    cout << "setwise coprime" << endl;
  else
    cout << "pairwise coprime" << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  int N;
  cin >> N;
  VI A(N);
  rep(i, N) cin >> A[i];
  int X = A[0];
  rep(i, N) X = gcd(X, A[i]);
  if (X != 1) {
    cout << "not coprime" << endl;
    return 0;
  }
  map<int, int> M;
  rep(i, N) {
    rep(j, sqrt(A[i])) {
      if (A[i] % (j + 2) == 0) {
        M[j + 2]++;
        while (A[i] % (j + 2) == 0) {
          A[i] /= (j + 2);
        }
      }
    }
    if (A[i] != 1)
      M[A[i]]++;
  }
  auto itr = M.begin();
  int ans = 0;
  rep(i, M.size()) {
    if (itr->second != 1)
      ans = 1;
    itr++;
  }

  if (ans)
    cout << "setwise coprime" << endl;
  else
    cout << "pairwise coprime" << endl;
}