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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

template <typename T> using vec2 = vector<vector<T>>;
template <typename T> using vec3 = vec2<vector<T>>;
template <typename T> using vec4 = vec3<vector<T>>;
template <typename T> using vec5 = vec4<vector<T>>;
template <typename T> using vec6 = vec5<vector<T>>;

void factorize(map<ll, ll> &fac, vector<ll> &sieved, ll a) {
  while (a != 1) {
    if (fac.find(sieved[a]) == fac.end())
      fac[sieved[a]] = 0;
    fac[sieved[a]]++;
    a /= sieved[a];
  }
}
void primesieve(vector<ll> &res, ll n) {
  res = vector<ll>(n + 1, -1);
  for (int i = 2; i <= n; i++) {
    if (res[i] != -1)
      continue;
    res[i] = i;
    for (int j = 2; j * i <= n; j++) {
      res[j * i] = i;
    }
  }
}
vector<ll> primesuntil(ll n) {
  vector<ll> res;
  vector<ll> sieved;
  primesieve(sieved, n);
  for (int i = 2; i <= n; i++) {
    if (sieved[i] == i)
      res.push_back(i);
  }
  return res;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  int N;
  cin >> N;
  vector<ll> A;
  int M = 1000010;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  vector<ll> sieved;
  primesieve(sieved, M);
  vector<int> nums(M, 0);
  bool isPair = true;
  map<ll, ll> mp;
  for (int i = 0; i < N; i++) {
    mp.clear();
    factorize(mp, sieved, A[i]);
    // cout<<A[i]<<": ";
    for (const pair<ll, ll> &p : mp) {
      // cout<<p.first<<"^"<<p.second<<" ";
      nums[p.first]++;
      if (nums[p.first] >= 2) {
        isPair = false;
        break;
      }
    }
    // cout<<endl;
    if (!isPair)
      break;
  }
  ll g = A[0];
  for (int i = 0; i < N; i++) {
    g = gcd(g, A[i]);
  }
  bool isSet = g == 1;
  if (isSet && !isPair)
    cout << "setwise coprime" << endl;
  else if (isPair)
    cout << "pairwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

template <typename T> using vec2 = vector<vector<T>>;
template <typename T> using vec3 = vec2<vector<T>>;
template <typename T> using vec4 = vec3<vector<T>>;
template <typename T> using vec5 = vec4<vector<T>>;
template <typename T> using vec6 = vec5<vector<T>>;

void factorize(map<ll, ll> &fac, vector<ll> &primes, ll a) {
  for (int i = 0; primes[i] <= a; i++) {
    ll pr = primes[i];
    ll pow = pr;
    if (a % pr != 0)
      continue;
    fac[pr] = 0;
    while (pow < a) {
      fac[pr]++;
      pow *= pr;
    }
  }
}
vector<ll> primesuntil(ll n) {
  vector<ll> res;
  vector<bool> isprime(n + 1, true);
  for (int i = 2; i <= n; i++) {
    if (!isprime[i])
      continue;
    res.push_back(i);
    for (int j = 2; j * i <= n; j++) {
      isprime[j * i] = false;
    }
  }
  return res;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  int N;
  cin >> N;
  vector<ll> A;
  int M = 1000010;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  vector<ll> primes = primesuntil(M);
  // for(int i = 0;i < primes.size();i++){
  //     cout<<primes[i]<<endl;
  // }
  vector<int> nums(M, 0);
  bool isPair = true;
  map<ll, ll> mp;
  for (int i = 0; i < N; i++) {
    mp.clear();
    factorize(mp, primes, A[i]);
    for (const pair<ll, ll> &p : mp) {
      nums[p.first]++;
      if (nums[p.first] >= 2) {
        isPair = false;
        break;
      }
    }
    if (!isPair)
      break;
  }
  ll g = A[0];
  for (int i = 0; i < N; i++) {
    g = gcd(g, A[i]);
  }
  bool isSet = g == 1;
  if (isSet && !isPair)
    cout << "setwise coprime" << endl;
  else if (isPair)
    cout << "pairwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}