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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl '\\n'
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define F first
#define S second
const ll MOD = 1e9 + 7;
template <typename ForwardIterator, typename T>
ForwardIterator first_less_than(ForwardIterator first, ForwardIterator last,
                                T val) {
  auto it = lower_bound(first, last, val);
  return (it == first ? first : it);
}
bool isPrime(ll n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

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

  return true;
}

void solve() {
  ll n;
  cin >> n;
  ll arr[n];
  rep(i, n) { cin >> arr[i]; }
  map<ll, ll> m;
  bool temp = false;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j * j <= arr[i]; j++) {
      if (arr[i] % j == 0) {
        if (arr[i] != 1 && j == 1) {
          if (m.find(arr[i]) != m.end()) {
            temp = true;
            break;
          } else {
            m[arr[i]]++;
          }
        } else if (arr[i] != 1) {

          ll val = arr[i] / j;
          if (val != j) {
            if (m.find(j) != m.end() || m.find(val) != m.end()) {
              temp = true;
              break;
            } else {
              m[val]++;
              m[j]++;
            }
          } else if (m.find(j) != m.end()) {
            temp = true;
            break;
          } else {
            m[j]++;
          }
        }
      }
    }
  }
  if (temp) {
    ll res = arr[0];
    for (int i = 1; i < n; i++) {
      res = __gcd(res, arr[i]);
      if (res == 1) {
        break;
      }
    }
    if (res == 1) {
      cout << "setwise coprime" << endl;
    } else {
      cout << "not coprime" << endl;
    }
  } else {
    cout << "pairwise coprime" << endl;
  }
}

int main() {
  //	ios_base::sync_with_stdio(false);
  //	 cin.tie(NULL);
  int tt;
  tt = 1;
  while (tt--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl '\\n'
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define F first
#define S second
const ll MOD = 1e9 + 7;
template <typename ForwardIterator, typename T>
ForwardIterator first_less_than(ForwardIterator first, ForwardIterator last,
                                T val) {
  auto it = lower_bound(first, last, val);
  return (it == first ? first : it);
}
bool isPrime(ll n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

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

  return true;
}

void solve() {
  ll n;
  cin >> n;
  ll arr[n];
  map<ll, ll> m;
  rep(i, n) {
    cin >> arr[i];
    m[arr[i]]++;
  }
  bool temp = false;
  for (auto x : m) {
    if (x.first != 1 && x.second > 1) {
      temp = true;
      break;
    }
  }
  if (m[1] == n) {
    cout << "pairwise coprime" << endl;
  } else {
    for (auto x : m) {
      if (x.second == n) {
        temp = true;
      }
    }
    if (!temp) {
      for (int i = 0; i < n; i++) {
        if (!isPrime(arr[i])) {
          for (int j = i + 1; j < n; j++) {
            if (__gcd(arr[i], arr[j]) != 1) {
              temp = true;
              break;
            }
          }
          if (temp) {
            break;
          }
        }
      }
    }
    if (temp) {
      ll res = arr[0];
      for (int i = 1; i < n; i++) {
        res = __gcd(res, arr[i]);
        if (res == 1) {
          break;
        }
      }
      if (res == 1) {
        cout << "setwise coprime" << endl;
      } else {
        cout << "not coprime" << endl;
      }
    } else {
      cout << "pairwise coprime" << endl;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt;
  tt = 1;
  while (tt--) {
    solve();
  }
}