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

#include <bits/stdc++.h>
#include <cctype>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<typename key, typename val>
// using ordered_tree =
//   tree<key, val, std::less<>, rb_tree_tag,
//   tree_order_statistics_node_update>;
using namespace std;
typedef long long int64;
typedef unsigned long long uint64;
typedef complex<double> comp;
const double pi = 3.14159265358979323846;
const int inf = (int)1e+9 + 2;
const int64 inf64 = (int64)1e+18 + 2;
const double dinf = 1e+20;
const int mod = 1'000'000'007; //*/998244353;
const int base = 2187;
const double eps = 1e-9;
const int N = 500'000;
const int LOGN = 19;

int n, m, k;

int lt[1'000'001];

void solve(int test) {
  cin >> n;
  vector<int> divs(1'000'000);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    while (x > 1) {
      ++divs[lt[x]];
      int d = lt[x];
      while (x % d == 0) {
        x /= d;
      }
    }
  }
  int no = 0;
  int pairwise = 1;
  for (const auto &d : divs) {
    no |= d == n;
    pairwise &= d <= 1;
  }
  if (no) {
    cout << "not coprime\\n";
  } else if (pairwise) {
    cout << "pairwise coprime\\n";
  } else {
    cout << "setwise coprime\\n";
  }
}

void precalc() {
  vector<int> divs;
  for (int i = 2; i <= 1'000'000; ++i) {
    if (lt[i] == 0) {
      lt[i] = i;
      divs.push_back(i);
    }
    for (int j = 0;
         j < (int)divs.size() && divs[j] <= lt[i] && i * divs[j] <= 1'000'000;
         ++j) {
      lt[i * divs[j]] = divs[j];
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  precalc();
  int test = 1;
  // cin >> test;
  // auto start = chrono::high_resolution_clock::now();
  for (int i = 1; i <= test; ++i) {
    solve(i);
  }
  // cerr <<
  // chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()
  // - start).count() << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cctype>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<typename key, typename val>
// using ordered_tree =
//   tree<key, val, std::less<>, rb_tree_tag,
//   tree_order_statistics_node_update>;
using namespace std;
typedef long long int64;
typedef unsigned long long uint64;
typedef complex<double> comp;
const double pi = 3.14159265358979323846;
const int inf = (int)1e+9 + 2;
const int64 inf64 = (int64)1e+18 + 2;
const double dinf = 1e+20;
const int mod = 1'000'000'007; //*/998244353;
const int base = 2187;
const double eps = 1e-9;
const int N = 500'000;
const int LOGN = 19;

int n, m, k;

int lt[1'000'001];

void solve(int test) {
  cin >> n;
  vector<set<int>> divs(1'000'000);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    while (x > 1) {
      divs[lt[x]].insert(i);
      x /= lt[x];
    }
  }
  int no = 0;
  int pairwise = 1;
  for (const auto &st : divs) {
    no |= st.size() == n;
    pairwise &= st.size() <= 1;
  }
  if (no) {
    cout << "not coprime\\n";
  } else if (pairwise) {
    cout << "pairwise coprime\\n";
  } else {
    cout << "setwise coprime\\n";
  }
}

void precalc() {
  vector<int> divs;
  for (int i = 2; i <= 1'000'000; ++i) {
    if (lt[i] == 0) {
      lt[i] = i;
      divs.push_back(i);
    }
    for (int j = 0;
         j < (int)divs.size() && divs[j] <= lt[i] && i * divs[j] <= 1'000'000;
         ++j) {
      lt[i * divs[j]] = divs[j];
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  precalc();
  int test = 1;
  // cin >> test;
  // auto start = chrono::high_resolution_clock::now();
  for (int i = 1; i <= test; ++i) {
    solve(i);
  }
  // cerr <<
  // chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()
  // - start).count() << '\\n';
  return 0;
}