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

// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define mod (ll)(1e9 + 7)
#define FIX(a) ((a) % mod + mod) % mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define fi first
#define se second
#define pb push_back
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int gcd(int a, int b) {
  int r, tmp;
  if (a < b) {
    tmp = a;
    a = b;
    b = tmp;
  }
  r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
vector<int> prime(1000000, 0);
int amax = 1000001;
vector<bool> isPrime(amax + 1, true);
void pr() {
  int p = 0;
  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 2; i <= amax; ++i) {
    if (isPrime[i]) {
      prime[p] = i;
      ++p;
      for (int j = i * 2; j <= amax; j += i)
        isPrime[j] = false;
    }
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vint a(n);
  REP(i, n) cin >> a[i];
  pr();
  vector<bool> used(1000001, false);
  int x = a[0];
  bool f = true;
  REP(i, n) {
    x = gcd(x, a[i]);
    int t = a[i];
    for (int j = 0; prime[j] * prime[j] <= a[i]; ++j) {
      if (t % prime[j] == 0) {
        if (used[prime[j]] == true) {
          f = false;
        }
        while (t % prime[j] == 0) {
          t /= prime[j];
        }
        used[prime[j]] = true;
      }
    }
    if (t > 1) {
      if (used[t] == true) {
        f = false;
      }
      used[t] = true;
    }
  }
  if (f) {
    cout << "pairwise coprime" << endl;
  } else if (x == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define mod (ll)(1e9 + 7)
#define FIX(a) ((a) % mod + mod) % mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define fi first
#define se second
#define pb push_back
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int gcd(int a, int b) {
  int r, tmp;
  if (a < b) {
    tmp = a;
    a = b;
    b = tmp;
  }
  r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vint a(n);
  REP(i, n) cin >> a[i];
  sort(ALL(a));
  vector<bool> used(1000001, false);
  int x = a[0];
  bool f = true;
  REP(i, n) {
    x = gcd(x, a[i]);
    int t = a[i];
    for (int j = 2; j * j <= a[i]; ++j) {
      if (t % j == 0) {
        if (used[j] == true) {
          f = false;
        }
        while (t % j == 0) {
          t /= j;
        }
        used[j] = true;
      }
    }
    if (t > 1) {
      if (used[t] == true) {
        f = false;
      }
      used[t] = true;
    }
  }
  if (f) {
    cout << "pairwise coprime" << endl;
  } else if (x == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}