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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;
const ll mmod = 998244353;

vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, 1, -1};

vector<string> g;
vector<vector<bool>> used;
vector<ll> d;

long long gcd(const long long &a, const long long &b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}

vector<long long> sieve(const long long &n) {
  vector<long long> v(n + 1);
  vector<bool> c(n + 1, 1);
  vector<long long> ret;
  for (long long i = 2; i <= n; ++i) {
    if (c[i]) {
      for (long long j = 2 * i; j <= n; j += i) {
        c[j] = 0;
      }
      ret.push_back(i);
    }
  }
  return ret;
}

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  ll n;
  cin >> n;
  vector<ll> a(n);

  ll gc = -1;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (gc < 0) {
      gc = a[i];
      continue;
    }
    gc = gcd(a[i], gc);
  }

  vector<ll> c(1e6 + 1);

  vector<ll> p = sieve(1e6);

  vector<vector<ll>> div(1e6 + 1, vector<ll>(1, 1));
  for (int i = 0; i < p.size(); ++i) {
    for (int j = p[i]; j <= 1e6; j += p[i]) {
      div[j].push_back(p[i]);
    }
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 1; j < div[a[i]].size(); ++j) {
      c[div[a[i]][j]]++;
    }
  }
  bool f = 0;
  for (int i = 2; i <= 1e6; ++i) {
    if (c[i] > 1) {
      f = 1;
      break;
    }
  }

  if (!f) {
    cout << "pairwise coprime" << endl;
  } else {
    if (gc == 1) {
      cout << "setwise coprime" << endl;
    } else {
      cout << "not coprime" << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;
const ll mmod = 998244353;

vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, 1, -1};

vector<string> g;
vector<vector<bool>> used;
vector<ll> d;

long long gcd(const long long &a, const long long &b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}

vector<long long> sieve(const long long &n) {
  vector<long long> v(n + 1);
  vector<bool> c(n + 1, 1);
  vector<long long> ret;
  for (long long i = 2; i <= n; ++i) {
    if (c[i]) {
      for (long long j = 2 * i; j <= n; j += i) {
        c[j] = 0;
      }
      ret.push_back(i);
    }
  }
  return ret;
}

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  ll n;
  cin >> n;
  vector<ll> a(n);

  ll gc = -1;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (gc < 0) {
      gc = a[i];
      continue;
    }
    gc = gcd(a[i], gc);
  }

  vector<ll> c(1e6 + 1);

  vector<ll> p = sieve(1e6);

  vector<vector<ll>> div(1e6 + 1, vector<ll>(1, 1));
  for (int i = 0; i <= p.size(); ++i) {
    for (int j = p[i]; j <= 1e6; j += i) {
      div[j].push_back(p[i]);
    }
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < p.size(); ++j) {
      c[div[a[i]][j]]++;
    }
  }
  bool f = 0;
  for (int i = 2; i <= 1e6; ++i) {
    if (c[i] > 1) {
      f = 1;
      break;
    }
  }

  if (!f) {
    cout << "pairwise coprime" << endl;
  } else {
    if (gc == 1) {
      cout << "setwise coprime" << endl;
    } else {
      cout << "not coprime" << endl;
    }
  }

  return 0;
}