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

#include <bits/stdc++.h>
const int MAX = 1e6 + 10;
const int modn = 1e9 + 7;
typedef long long ll;
#define endl '\\n'
#define cwk                                                                    \\
  freopen("D:\\\\workplace\\\\CLion\\\\in.in", "r", stdin),                          \\
      freopen("D:\\\\workplace\\\\CLion\\\\out.out", "w", stdout)
using namespace std;
int n, a[MAX];
bool vis[MAX];
vector<int> prime;
int cnt[MAX];
int mp[MAX];
void prepare() {
  for (int i = 2; i * i <= MAX; i++) {
    if (!vis[i]) {
      for (int j = i * i; j < MAX; j += i)
        vis[j] = true;
    }
  }
  int k = 1;
  for (int i = 2; i < MAX; i++) {
    if (!vis[i]) {
      prime.push_back(i);
      mp[i] = k++;
    }
  }
  vis[1] = true;
  // cout<<k;
}
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = a[0];
  for (int i = 1; i < n; i++) {
    ans = __gcd(ans, a[i]);
  }
  if (ans != 1) {
    cout << "not coprime\\n";
    return;
  }
  for (int i = 0; i < n; i++) {
    int k = a[i];
    if (k == 1) {
      continue;
    }
    if (!vis[k]) {
      cnt[mp[k] - 1]++;
      if (cnt[mp[k] - 1] >= 2) {
        cout << "setwise coprime\\n";
        return;
      }
      continue;
    }
    for (int j = 0; k != 1; j++) {
      // int flag=0;
      if (k % prime[j] == 0) {
        // flag=prime[j];
        ++cnt[j];
        if (cnt[j] >= 2) {
          cout << "setwise coprime\\n";
          return;
        }
      }
      while (k % prime[j] == 0) {
        k /= prime[j];
      }
      if (!vis[k]) {
        ++cnt[mp[k] - 1];
        if (cnt[mp[k] - 1] >= 2) {
          cout << "setwise coprime\\n";
          return;
        }
        break;
      }
    }
  }
  cout << "pairwise coprime\\n";
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // cwk;
  prepare();
  int _ = 1;
  // cin>>_;
  while (_--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
const int MAX = 1e6 + 10;
const int modn = 1e9 + 7;
typedef long long ll;
#define endl '\\n'
#define cwk                                                                    \\
  freopen("D:\\\\workplace\\\\CLion\\\\in.in", "r", stdin),                          \\
      freopen("D:\\\\workplace\\\\CLion\\\\out.out", "w", stdout)
using namespace std;
int n, a[MAX];
bool vis[MAX];
vector<int> prime;
int cnt[MAX];
void prepare() {
  for (int i = 2; i * i <= MAX; i++) {
    if (!vis[i]) {
      for (int j = i * i; j < MAX; j += i)
        vis[j] = true;
    }
  }
  for (int i = 2; i < MAX; i++) {
    if (!vis[i])
      prime.push_back(i);
  }
}
void solve() {
  cin >> n;
  bool is = true;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    int k = a[i];
    for (int j = 0; k != 1 && j < prime.size(); j++) {
      if (k % prime[j] == 0) {
        cnt[j]++;
        if (cnt[j] >= 2)
          is = false;
      }
      while (k % prime[j] == 0) {
        k /= prime[j];
      }
    }
  }
  if (is) {
    cout << "pairwise coprime\\n";
    return;
  }
  int ans = a[0];
  for (int i = 1; i < n; i++) {
    ans = __gcd(ans, a[i]);
    if (ans == 1) {
      cout << "setwise coprime\\n";
      return;
    }
  }
  cout << "not coprime\\n";
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // cwk;
  prepare();
  int _ = 1;
  // cin>>_;
  while (_--) {
    solve();
  }
  return 0;
}