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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n - 1; i >= (int)(k); i--)
#define all(x) (x).begin(), (x).end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素
#define PB push_back // 挿入
#define MP make_pair // pairのコンストラクタ
// V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
const ll mod = 1000000007;
const ll inf = 1000000000000; // 10の12乗
// GCD, LCM
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; }
template <typename T> vector<T> smallest_prime_factors(T n) {

  vector<T> spf(n + 1);
  for (int i = 0; i <= n; i++)
    spf[i] = i;

  for (T i = 2; i * i <= n; i++) {

    // 素数だったら
    if (spf[i] == i) {

      for (T j = i * i; j <= n; j += i) {

        // iを持つ整数かつまだ素数が決まっていないなら
        if (spf[j] == j) {
          spf[j] = i;
        }
      }
    }
  }

  return spf;
}

int main() {
  int n;
  cin >> n;
  Vi v(n);
  rep(i, n) cin >> v[i];
  Vi prime(2000001, 0);
  rrep(i, 2, 1000001) {
    if (prime[i] != 0)
      continue;
    ll x = 2 * i;
    while (x <= 1000000) {

      prime[x] = 1;
      x += i;
    }
  }
  Vi cnt(1000001, 0);
  auto spf = smallest_prime_factors(2000000);
  rep(i, n) {
    int x = v[i];
    while (x != 1) {
      int a = spf[x];
      cnt[a]++;
      while (x % a == 0) {
        x /= a;
      }
    }
  }
  bool b = 1;
  rrep(i, 2, 1000001) {
    if ((!prime[i]) && cnt[i] >= 2)
      b = 0;
  }
  if (b) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  ll g = v[0];
  rep(i, n) g = gcd(g, v[i]);
  if (g == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n - 1; i >= (int)(k); i--)
#define all(x) (x).begin(), (x).end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素
#define PB push_back // 挿入
#define MP make_pair // pairのコンストラクタ
// V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
const ll mod = 1000000007;
const ll inf = 1000000000000; // 10の12乗
// GCD, LCM
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; }

int main() {
  int n;
  cin >> n;
  Vi v(n);
  rep(i, n) cin >> v[i];
  Vi prime(2000001, 0);
  rrep(i, 2, 1000001) {
    if (prime[i] != 0)
      continue;
    ll x = 2 * i;
    while (x <= 1000000) {

      prime[x] = 1;
      x += i;
    }
  }
  Vi cnt(1000001, 0);
  rep(i, n) {
    int x = v[i];
    rrep(i, 1, sqrt(x) + 1) {
      if (x % i != 0)
        continue;
      int y = x / i, z = i;
      cnt[y]++;
      if (y != z)
        cnt[z]++;
    }
  }
  bool b = 1;
  rrep(i, 2, 1000001) {
    if ((!prime[i]) && cnt[i] >= 2)
      b = 0;
  }
  if (b) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  ll g = v[0];
  rep(i, n) g = gcd(g, v[i]);
  if (g == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}