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

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
#define pb push_back
#define ld long double
#define f first
#define s second
#define in insert
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int INF = 1e18 + 5;
const int mod = 1e9 + 7;
const int N = 2e5 + 314;
const long double pi = 3.1415926535897932384626433832795;
int gcd(int x, int y) { return (x == 0 ? y : gcd(y % x, x)); }
void solve() {
  int n;
  cin >> n;
  vi v(n);
  unordered_map<int, int> u, cnt;
  int gc = 0, ok = 1;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    cnt[v[i]]++;
    if (cnt[v[i]] > 1 && v[i] > 1)
      ok = 0;
    gc = gcd(gc, v[i]);
  }
  if (gc > 1) {
    cout << "not coprime";
    return;
  }
  if (!ok) {
    cout << "setwise coprime";
    return;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 2; j <= sqrt(v[i]); j++) {
      if (v[i] % j == 0) {
        u[j]++;
        if (u[j] >= 2) {
          cout << "setwise coprime";
          return;
        }
        if (j * j != v[i]) {
          u[v[i] / j]++;
          if (u[v[i] / j] >= 2) {
            cout << "setwise coprime";
            return;
          }
        }
      }
    }
    if (v[i] > 1)
      u[v[i]]++;
    if (u[v[i]] >= 2) {
      cout << "setwise coprime";
      return;
    }
  }
  cout << "pairwise coprime";
}
signed main() {
  speed;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
#define pb push_back
#define ld long double
#define f first
#define s second
#define in insert
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int INF = 1e18 + 5;
const int mod = 1e9 + 7;
const int N = 2e5 + 314;
const long double pi = 3.1415926535897932384626433832795;
int gcd(int x, int y) { return (x == 0 ? y : gcd(y % x, x)); }
void solve() {
  int n;
  cin >> n;
  vi v(n);
  map<int, int> u;
  int gc = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    for (int j = 1; j <= sqrt(v[i]); j++) {
      if (v[i] % j == 0) {
        u[j]++;
        if (j * j != v[i])
          u[v[i] / j]++;
      }
    }
    gc = gcd(gc, v[i]);
  }
  if (gc > 1)
    cout << "not coprime";
  else {
    for (auto i : u) {
      if (i.f > 1 && i.s > 1) {
        cout << "setwise coprime";
        return;
      }
    }
    cout << "pairwise coprime";
  }
}
signed main() {
  speed;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}