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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

template <typename T> inline void read(T &x) {
  T data = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    data = (data << 3) + (data << 1) + ch - '0';
    ch = getchar();
  }
  x = f * data;
}

const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e6 + 9;
bitset<maxn> num;
vector<int> prime;
inline void init(int n) {
  for (int i = 2; i <= n; ++i) {
    if (!num[i]) {
      prime.push_back(i);
    }
    for (int j = 0; j < prime.size(); ++j) {
      if (i * prime[j] >= n)
        break;
      num[i * prime[j]] = 1;
      if (i % prime[j] == 0)
        break;
    }
  }
}
vector<ll> v;
unordered_map<ll, int> ump;
int main() {
  init(1000000);
  int sz = prime.size();
  // cout << sz << endl;
  int n;
  ll maxx = 0;
  read(n);
  bool flag = true;
  for (int i = 1; i <= n; ++i) {
    ll x;
    maxx = max(maxx, x);
    read(x);
    ump[x]++;
    v.emplace_back(x);
  }
  for (int i = 0; i < sz; ++i) {
    int tot = 0;
    for (int j = 1;; ++j) {
      if (1LL * j * prime[i] > 1LL * maxx)
        break;
      tot += ump[j * prime[i]];
    }
    if (tot > 1) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  ll g = v[0];
  for (int i = 1; i < n; ++i)
    g = __gcd(g, v[i]);
  if (g == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

template <typename T> inline void read(T &x) {
  T data = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    data = (data << 3) + (data << 1) + ch - '0';
    ch = getchar();
  }
  x = f * data;
}

const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1000009;
bitset<maxn> num;
vector<int> prime;
inline void init(int n) {
  for (int i = 2; i <= n; ++i) {
    if (!num[i]) {
      prime.push_back(i);
    }
    for (int j = 0; j < prime.size(); ++j) {
      if (i * prime[j] >= n)
        break;
      num[i * prime[j]] = 1;
      if (i % prime[j] == 0)
        break;
    }
  }
}
vi v;
int bb[maxn];
int main() {
  init(1000000);
  int sz = prime.size();
  // cout << sz << endl;
  int n;
  read(n);
  bool flag = true;
  for (int i = 1; i <= n; ++i) {
    int x;
    read(x);
    v.emplace_back(x);
    if (x > 1 && flag) {
      for (int j = 0; j < sz; ++j) {
        if (x % prime[j] == 0) {
          if (bb[j] == 0)
            bb[j] = i;
          else {
            flag = false;
            break;
          }
        }
      }
    }
  }
  if (flag) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  int g = v[0];
  for (int i = 1; i < n; ++i) {
    g = __gcd(g, v[i]);
    if (g == 1)
      break;
  }
  if (g == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  return 0;
}