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

#include <bits/stdc++.h>
#define vc vector<int>
#define um unordered_map<int, int>
#define ll long long
#define rep(i, a, b) for (__typeof(a) i = a; i < b; i++)
#define si(a) a.size()
#define all(a) a.begin(), a.end()
#define ss(a) a.second
#define ff(a) a.first
#define M 1000000
#define pb push_back
#define repit(i, a) for (auto i = a.begin(); i != a.end(); i++)
#define take(arr, n)                                                           \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> arr[i]
#define out(arr, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << arr[i] << ' '
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define reprev(i, b, a) for (__typeof(b) i = b; i >= a; i--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define doublearray vector<vector<int>>
#define mod 1000000007
using namespace std;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int binaryS(int x, int sb[], int m) {
  int l = 0, r = m;
  int mid = (l + r) / 2;
  while (l < r) {
    mid = (l + r) / 2;
    if (sb[mid] < x)
      l = mid + 1;
    else if (sb[mid] > x)
      r = mid - 1;
    else
      return mid;
  }
  return l;
}
int main() {
  fastio;
  vector<vc> pf(1000001);
  rep(i, 2, 1000001) {
    if (pf[i].empty()) {
      for (int j = i; j <= 1000001; j += i) {
        pf[j].pb(i);
      }
    }
  }
  int n;
  cin >> n;
  int arr[n];
  take(arr, n);
  um mp;
  bool pairw = true, setw = true;
  rep(i, 0, n) {
    repit(it, pf[arr[i]]) {
      mp[*it]++;
      if (mp[*it] > 1)
        pairw = false;
      if (mp[*it] == n)
        setw = false;
    }
  }
  if (pairw)
    cout << "pairwise coprime";
  else if (setw)
    cout << "setwise coprime";
  else
    cout << "not coprime";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define vc vector<int>
#define um unordered_map<int, int>
#define ll long long
#define rep(i, a, b) for (__typeof(a) i = a; i < b; i++)
#define si(a) a.size()
#define all(a) a.begin(), a.end()
#define ss(a) a.second
#define ff(a) a.first
#define M 1000000
#define pb push_back
#define repit(i, a) for (auto i = a.begin(); i != a.end(); i++)
#define take(arr, n)                                                           \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> arr[i]
#define out(arr, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << arr[i] << ' '
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define reprev(i, b, a) for (__typeof(b) i = b; i >= a; i--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define doublearray vector<vector<int>>
#define mod 1000000007
using namespace std;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int binaryS(int x, int sb[], int m) {
  int l = 0, r = m;
  int mid = (l + r) / 2;
  while (l < r) {
    mid = (l + r) / 2;
    if (sb[mid] < x)
      l = mid + 1;
    else if (sb[mid] > x)
      r = mid - 1;
    else
      return mid;
  }
  return l;
}
int main() {
  fastio;
  bool pf[1000001];
  vc primes;
  rep(i, 0, 1000001) pf[i] = true;
  rep(i, 2, 1000001) {
    if (pf[i]) {
      primes.pb(i);
      for (int j = 2 * i; j <= 1000001; j += i) {
        if (pf[j]) {
          pf[j] = false;
        }
      }
    }
  }
  int n;
  cin >> n;
  int arr[n];
  take(arr, n);
  // int g = arr[0];
  // rep(i,0,n){
  //     g = gcd(g,arr[i]);
  // }
  bool pairw = true, setw = true;
  repit(it, primes) {
    int c = 0;
    rep(i, 0, n) {
      if (arr[i] % (*it) == 0 && c >= 1) {
        pairw = false;
        c++;
      } else if (arr[i] % (*it) == 0) {
        c++;
      }
    }
    if (c == n) {
      setw = false;
      break;
    }
  }
  if (!setw)
    cout << "not coprime";
  else if (pairw)
    cout << "pairwise coprime";
  else
    cout << "setwise coprime";
  // repit(it,primes) cout<<(*it)<<endl;
  return 0;
}