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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define int long long
#define endl "\\n"
#define all(x) (x).begin(), (x).end()
const int MOD = 1e9 + 7;
const int mxN = 2e6 + 3;

void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  vector<bool> isPrime(mxN, true);
  vector<int> D(mxN, 0);
  isPrime[0] = false;
  D[0] = 1;
  isPrime[1] = false;
  D[1] = 1;
  for (int i = 2; i < mxN; ++i) {
    if (isPrime[i]) {
      D[i] = i;
      for (int j = i * i; j < mxN; j += i) {
        isPrime[j] = false;
        D[j] = i;
      }
    }
  }
  map<int, int> mp;
  set<int> st;
  bool kr = false;
  for (int i = 0; i < n; ++i) {
    if ((a[i] != 1) and (st.find(a[i]) != st.end())) {
      kr = true;
      break;
    }
    st.insert(a[i]);
    int k = a[i];
    while (D[k] != k) {
      mp[D[k]]++;
      int p = D[k];
      while (k % p == 0)
        k /= p;
    }
    if (k > 1 and D[k] == k)
      mp[k]++;
  }

  bool pr = true;
  if (kr == false) {
    for (auto it : mp) {
      if (it.second > 1) {
        pr = false;
        break;
      }
    }
  } else {
    pr = false;
  }
  if (pr)
    cout << "pairwise coprime\\n";
  else {
    int gd = a[0];
    for (int i = 1; i < n; ++i) {
      gd = __gcd(gd, a[i]);
    }
    if (gd == 1)
      cout << "setwise coprime\\n";
    else
      cout << "not coprime\\n";
  }
}

int32_t main() {
  IO;

  int nT = 1;
  // cin >> nT;
  for (int i = 1; i <= nT; ++i)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define int long long
#define endl "\\n"
#define all(x) (x).begin(), (x).end()
const int MOD = 1e9 + 7;
const int mxN = 2e6 + 3;

void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  // vector<bool> isPrime(mxN, true);
  // isPrime[0] = false;
  // isPrime[1] = false;
  // for(int i = 2; i < mxN; ++i){
  // if(isPrime[i]){
  // for(int j = i * i; j < mxN; j += i){
  // isPrime[j] = false;
  //}
  //}
  //}
  map<int, int> mp;
  for (int j = 0; j < n; ++j) {
    int k = a[j];
    if (k % 2 == 0) {
      mp[2]++;
      while (k % 2 == 0)
        k /= 2;
    }
    int p = k;
    for (int i = 3; i * i <= p; i += 2) {
      if (k == 1)
        break;
      if (k % i == 0) {
        mp[i]++;
        while (k % i == 0)
          k /= i;
      }
    }
    if (k > 2)
      mp[k]++;
  }
  bool pr = true;
  for (auto it : mp) {
    if (it.second > 1) {
      pr = false;
      break;
    }
  }
  if (pr)
    cout << "pairwise coprime\\n";
  else {
    int gd = a[0];
    for (int i = 1; i < n; ++i) {
      gd = __gcd(gd, a[i]);
    }
    if (gd == 1)
      cout << "setwise coprime\\n";
    else
      cout << "not coprime\\n";
  }
}

int32_t main() {
  IO;

  int nT = 1;
  // cin >> nT;
  for (int i = 1; i <= nT; ++i)
    solve();
  return 0;
}