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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<ld> vld;
typedef vector<string> vstr;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll; // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll; // 大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"

ll MOD = 1000000007;
ll INF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }     // 最小公倍数

void p() { cout << "pairwise coprime" << endl; }
void s() { cout << "setwise coprime" << endl; }
void no() { cout << "not coprime" << endl; }

//-----------------------------------------
vector<ll> prime_table(ll n) {
  vector<bool> flag(n + 1, true);
  vector<ll> res(n + 1);
  for (ll i = 0; i < n + 1; i++)
    res[i] = i;

  flag[0] = flag[1] = false;
  res[0] = 0;
  res[1] = 1;

  for (ll i = 2; i * i <= n; i++) {
    if (flag[i]) {
      for (ll j = i + i; j <= n; j += i) {
        flag[j] = false;
        res[j] = i;
      }
    }
  }
  return res;
}
//-----------------------------------------

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;
  vll a(n);
  REP(i, n) cin >> a[i];
  ll tmp = gcd(a[0], a[1]);
  for (ll i = 2; i < n; i++) {
    tmp = gcd(tmp, a[i]);
  }
  if (tmp != 1) {
    no();
    return 0;
  }

  vll pt = prime_table(1000000);
  vector<bool> cnt(1000000 + 1, false);

  for (ll i = 0; i < n; i++) {
    ll x = a[i];
    while (x > 1) {
      ll k = pt[x];
      if (cnt[k]) {
        s();
        return 0;
      }
      cnt[k] = true;
      while (x % k == 0)
        x /= k;
    }
  }
  p();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<ld> vld;
typedef vector<string> vstr;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll; // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll; // 大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"

ll MOD = 1000000007;
ll INF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }     // 最小公倍数

void p() { cout << "pairwise coprime" << endl; }
void s() { cout << "setwise coprime" << endl; }
void no() { cout << "not coprime" << endl; }

//-----------------------------------------
vector<ll> prime_table(ll n) {
  vector<bool> flag(n + 1, true);
  vector<ll> res(n + 1, 1);

  flag[0] = flag[1] = false;
  res[0] = 0;
  res[1] = 1;

  for (ll i = 2; i * i <= n; i++) {
    if (flag[i]) {
      res[i] = i;
      for (ll j = i + i; j <= n; j += i) {
        flag[j] = false;
        res[j] = i;
      }
    }
  }
  return res;
}
//-----------------------------------------

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;
  vll a(n);
  REP(i, n) cin >> a[i];
  ll tmp = gcd(a[0], a[1]);
  for (ll i = 2; i < n; i++) {
    tmp = gcd(tmp, a[i]);
  }
  if (tmp != 1) {
    no();
    return 0;
  }

  vll pt = prime_table(1000000);
  vector<bool> cnt(1000000 + 1, false);

  for (ll i = 0; i < n; i++) {
    ll x = a[i];
    while (x > 1) {
      ll k = pt[x];
      if (cnt[k]) {
        s();
        return 0;
      }
      cnt[k] = true;
      while (x % k == 0)
        x /= k;
    }
  }
  p();

  return 0;
}