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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int, int>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define br break
#define con continue

template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a) {
  in >> a.fr >> a.sc;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> a) {
  out << a.fr << " " << a.sc;
  return out;
}
template <typename T, typename T1> T amax(T &a, T1 b) {
  if (b > a)
    a = b;
  return a;
}
template <typename T, typename T1> T amin(T &a, T1 b) {
  if (b < a)
    a = b;
  return a;
}

const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;

priority_queue<pii, vector<pii>, greater<pii>> q;
const int mx = 1e7 + 7;
// vector<int>p;
// int mark[mx];
// void seive(){
// 	for(int i=2;i<=mx;i++){
// 		if(mark[i]==0){
// 			p.pb(i);
// 			for(int j=i+i;j<=mx;j+=i)mark[j]=1;
// 		}
// 	}
// }
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // seive();
  int n;
  cin >> n;
  int a[n + 1];
  cin >> a[0];
  int g = a[0];
  int f = 0;
  map<int, int> mp;
  for (int i = 1; i < n; i++) {
    cin >> a[i];
    g = __gcd(g, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int x = a[i];
    // cout<<x;
    if (f == 1)
      break;
    // if(mark[x]==0){
    // 	if(mp[x]==0)mp[x]++;
    // 	else{
    // 		f=1;
    // 		break;
    // 	}
    // }
    if (f == 0) {
      for (int i = 1; i * i <= x; i++) {
        if (x % i == 0) {
          int a = x / i;
          int b = i;
          if (a == b) {
            b = 1;
          }
          if (a > 1) {
            if (mp[a]) {
              f = 1;
              break;
            }
            mp[a]++;
          }
          if (b > 1) {
            if (mp[b]) {
              f = 1;
              break;
            }
            mp[b]++;
          }
        }
      }
    }
  }
  if (f == 0)
    cout << "pairwise coprime" << endl;
  else if (g == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int, int>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define br break
#define con continue

template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a) {
  in >> a.fr >> a.sc;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> a) {
  out << a.fr << " " << a.sc;
  return out;
}
template <typename T, typename T1> T amax(T &a, T1 b) {
  if (b > a)
    a = b;
  return a;
}
template <typename T, typename T1> T amin(T &a, T1 b) {
  if (b < a)
    a = b;
  return a;
}

const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;

priority_queue<pii, vector<pii>, greater<pii>> q;
const int mx = 1e7 + 7;
vector<int> p;
int mark[mx];
void seive() {
  for (int i = 2; i <= mx; i++) {
    if (mark[i] == 0) {
      p.pb(i);
      for (int j = i + i; j <= mx; j += i)
        mark[j] = 1;
    }
  }
}
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  seive();
  int n;
  cin >> n;
  int a[n + 1];
  int g = 0;
  int f = 0, f1 = 0;
  map<int, int> mp;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    int x = a[i];
    g = __gcd(g, a[i]);
    for (int j = 0; f == 0 && j < p.size() && p[j] <= x; j++) {
      if (x % p[j] == 0) {
        if (mp[p[j]]) {
          f = 1;
          break;
        }
        while (x % p[j] == 0)
          x /= p[j];
        mp[p[j]]++;

        if (x > 1) {
          if (mp[p[j]]) {
            f = 1;
            break;
          } else
            mp[x]++;
        }
      }
    }
  }
  if (f == 0)
    cout << "pairwise coprime" << endl;
  else if (g == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}