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

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#define ul unsigned long long int
#define vi vector<int>
#define vl vector<int64_t>
#define all(x) x.begin(), x.end()
#define pb push_back
#define fo(x, a, b) for (int x = a; x < b; x++)
#define rfo(x, a, b) for (int x = a; x >= b; x--)
#define fi first
#define sec second
#define mp make_pair
int sgn(int x) { return x > 0 ? 1 : (x < 0 ? -1 : 0); }
typedef pair<int, int> pairs;
double pi = 3.14159265358979323846;
ll mod = 1000000007;
void solve() {
  ll n;
  cin >> n;
  vi a(n);
  fo(i, 0, n) cin >> a[i];
  sort(all(a));
  bool vis[1000001] = {false};
  bool g1 = true;
  int gcd = a[0];
  vi pri(1000001, 0);
  pri[1] = 1;
  fo(i, 2, 1000001) {
    if (!pri[i]) {
      pri[i] = i;
      for (int j = 2 * i; j < 1000001; j += i) {
        pri[j] = i;
      }
    }
  }
  fo(i, 0, n) {
    gcd = __gcd(a[i], gcd);
    if (g1) {
      while (a[i] > 1) {
        int temp = pri[a[i]];
        if (!vis[temp]) {
          vis[temp] = 1;
          a[i] /= temp;
          while (a[i] % temp == 0) {
            a[i] /= temp;
          }
        } else {
          g1 = 0;
          break;
        }
      }
    }
  }
  if (g1)
    cout << "pairwise coprime";
  else if (gcd == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
  cout << "\\n";
  return;
}
int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll t;
  // cin>>t;
  // while(t--)
  { solve(); }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#define ul unsigned long long int
#define vi vector<int>
#define vl vector<int64_t>
#define all(x) x.begin(), x.end()
#define pb push_back
#define fo(x, a, b) for (int x = a; x < b; x++)
#define rfo(x, a, b) for (int x = a; x >= b; x--)
#define fi first
#define sec second
#define mp make_pair
int sgn(int x) { return x > 0 ? 1 : (x < 0 ? -1 : 0); }
typedef pair<int, int> pairs;
double pi = 3.14159265358979323846;
ll mod = 1000000007;
void solve() {
  ll n;
  cin >> n;
  vi a(n);
  fo(i, 0, n) cin >> a[i];
  sort(all(a));
  bool vis[1000001] = {false};
  bool g1 = true;
  int gcd = a[0];
  vi pri;
  fo(i, 2, 1000001) {
    bool good = true;
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        good = false;
        break;
      }
    }
    if (good)
      pri.pb(i);
  }
  fo(i, 0, n) {
    if (g1) {
      int j = 0;
      int temp = a[i];
      while (temp > 1) {
        while (temp % pri[j])
          j++;
        if (vis[pri[j]]) {
          g1 = 0;
          break;
        }
        vis[pri[j]] = 1;
        while (temp % pri[j] == 0)
          temp /= pri[j];
      }
    }
    gcd = __gcd(a[i], gcd);
  }
  if (g1)
    cout << "pairwise coprime";
  else if (gcd == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
  cout << "\\n";
  return;
}
int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll t;
  // cin>>t;
  // while(t--)
  { solve(); }
  return 0;
}