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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
typedef long double ld;
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define fd(i, a, b) for (int i = a - 1; i >= b; i--)
#define print(a, n)                                                            \\
  f(i, 0, n) { cout << a[i] << " "; }
#define pf push_front
#define fi first
#define se second
#define ps(x, y) fixed << setprecision(y) << x
#define INF 1e10
const double pi = 3.14159265358979323;
const int mod = 1e9 + 7;
const int MAX = 1000000 + 5;
int p[MAX] = {0};

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void swap(int a, int b) {
  int temp = a;
  a = b;
  b = temp;
}
int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
int modinv(int y) { return binpow(y, mod - 2); }

int fact[500000 + 5];
void precal() {
  fact[0] = 1;
  for (int i = 1; i < 500000 + 5; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
  }
}
bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
int mm[1000000 + 1] = {0};
vector<pair<int, int>> vv;
int cnt = 0;
int vis[200000 + 5] = {0};
vector<vector<int>> adj;
void dfs(int j) {
  vis[j] = 1;
  cnt++;
  for (auto i : adj[j]) {
    if (vis[i] == 0)
      dfs(i);
  }
}
void bfs(int j) {
  vis[j] = 1;
  queue<int> q;
  q.push(j);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (auto i : adj[x]) {
      if (vis[i] == 0) {
        vis[i] = 1;
        q.push(i);
      }
    }
  }
}
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif
  // -------------------------------------Code starts
  // here---------------------------------------------------------------------
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    f(i, 0, n) { cin >> a[i]; }
    bool flag = true;
    f(j, 0, n) {
      int nm = a[j];
      for (int i = 2; i <= sqrt(a[j]); i++) {
        if (nm % i == 0) {
          mm[i]++;
          if (mm[i] > 1) {
            flag = false;
            break;
          }
          while (nm % i == 0) {
            nm /= i;
          }
        }
      }

      if (nm != 1) {
        mm[nm]++;
        if (mm[nm] > 1)
          flag = false;
      }
      if (!flag)
        break;
    }
    int _gcd = a[0];
    f(i, 1, n) { _gcd = gcd(a[i], _gcd); }
    if (flag) {
      cout << "pairwise coprime" << endl;
    } else {
      if (_gcd == 1)
        cout << "setwise coprime" << endl;
      else
        cout << "not coprime" << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
typedef long double ld;
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define fd(i, a, b) for (int i = a - 1; i >= b; i--)
#define print(a, n)                                                            \\
  f(i, 0, n) { cout << a[i] << " "; }
#define pf push_front
#define fi first
#define se second
#define ps(x, y) fixed << setprecision(y) << x
#define INF 1e10
const double pi = 3.14159265358979323;
const int mod = 1e9 + 7;
const int MAX = 1000000 + 5;
int p[MAX] = {0};

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void swap(int a, int b) {
  int temp = a;
  a = b;
  b = temp;
}
int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
int modinv(int y) { return binpow(y, mod - 2); }

int fact[500000 + 5];
void precal() {
  fact[0] = 1;
  for (int i = 1; i < 500000 + 5; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
  }
}
bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
int mm[1000000 + 1] = {0};
vector<pair<int, int>> vv;
int cnt = 0;
int vis[200000 + 5] = {0};
vector<vector<int>> adj;
void dfs(int j) {
  vis[j] = 1;
  cnt++;
  for (auto i : adj[j]) {
    if (vis[i] == 0)
      dfs(i);
  }
}
void bfs(int j) {
  vis[j] = 1;
  queue<int> q;
  q.push(j);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (auto i : adj[x]) {
      if (vis[i] == 0) {
        vis[i] = 1;
        q.push(i);
      }
    }
  }
}
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif
  // -------------------------------------Code starts
  // here---------------------------------------------------------------------
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    f(i, 0, n) { cin >> a[i]; }
    f(j, 0, n) {
      int nm = a[j];
      for (int i = 2; i <= sqrt(a[j]); i++) {
        if (nm % i == 0) {
          mm[i]++;
          while (nm % i == 0) {
            nm /= i;
          }
        }
      }
      if (nm != 1)
        mm[nm]++;
    }
    int _gcd = a[0];
    f(i, 1, n) { _gcd = gcd(a[i], _gcd); }
    bool flag = true;
    for (int i = 2; i <= 1000000; i++) {
      if (mm[i] > 1) {
        flag = false;
        break;
      }
    }
    if (flag) {
      cout << "pairwise coprime" << endl;
    } else {
      if (_gcd == 1)
        cout << "setwise coprime" << endl;
      else
        cout << "not coprime" << endl;
    }
  }
}