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

#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

int main() {
  fast();
  int n;
  cin >> n;
  int a[n];
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int allgcd = a[0];
  for (int i = 1; i < n; i++) {
    allgcd = __gcd(a[i], allgcd);
  }
  const int N = 1000001;
  vector<char> is_prime(N + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= N; i++) {
    if (is_prime[i] && (long long)i * i <= N) {
      for (int j = i * i; j <= N; j += i)
        is_prime[j] = false;
    }
  }
  vector<int> c;
  for (int i = 0; i <= N; i++) {
    if (is_prime[i]) {
      c.emplace_back(i);
    }
  }
  // cout<<(int)is_prime[6]<<"\\n";
  for (int i = 0; i < n; i++) {
    int l = a[i];
    if (is_prime[l]) {
      m[l]++;
      // cout<<l<<"\\n";
    } else {
      for (int j = 0; j < (int)c.size(); j++) {
        if (is_prime[l]) {
          m[l]++;
          break;
        }
        if (l % c[j] == 0) {
          m[c[j]]++;
          while (l % c[j] == 0) {
            l = l / c[j];
          }
        }
        if (l < c[j]) {
          break;
        }
      }
    }
  }
  int g = 1;
  for (auto i : m) {
    if (i.second == 1) {
      g = 1;
      // cout<<i.first<<"\\n";
    } else {
      g = 0;
      break;
    }
  }
  if (g) {
    cout << "pairwise coprime"
         << "\\n";
    return 0;
  }
  if (allgcd == 1) {
    cout << "setwise coprime"
         << "\\n";
    return 0;
  }
  cout << "not coprime"
       << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

int main() {
  fast();
  int n;
  cin >> n;
  int a[n];
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int allgcd = a[0];
  for (int i = 1; i < n; i++) {
    allgcd = __gcd(a[i], allgcd);
  }
  const int N = 1000001;
  vector<char> is_prime(N + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= N; i++) {
    if (is_prime[i] && (long long)i * i <= N) {
      for (int j = i * i; j <= N; j += i)
        is_prime[j] = false;
    }
  }
  vector<int> c;
  for (int i = 0; i <= N; i++) {
    if (is_prime[i]) {
      c.emplace_back(i);
    }
  }
  // cout<<(int)is_prime[6]<<"\\n";
  for (int i = 0; i < n; i++) {
    int l = a[i];
    if (is_prime[l]) {
      m[l]++;
      // cout<<l<<"\\n";
    } else {
      for (int j = 0; j < (int)c.size(); j++) {
        if (is_prime[l]) {
          m[l]++;
          break;
        }
        if (l % c[j] == 0) {
          m[c[j]]++;
          while (l % c[j] == 0) {
            l = l / c[j];
          }
        }
      }
    }
  }
  int g = 1;
  for (auto i : m) {
    if (i.second == 1) {
      g = 1;
      // cout<<i.first<<"\\n";
    } else {
      g = 0;
      break;
    }
  }
  if (g) {
    cout << "pairwise coprime"
         << "\\n";
    return 0;
  }
  if (allgcd == 1) {
    cout << "setwise coprime"
         << "\\n";
    return 0;
  }
  cout << "not coprime"
       << "\\n";
  return 0;
}