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

/*
        JAI JAGANNATH!
*/
//@Author : zanj0

#include <bits/stdc++.h>
using namespace std;

typedef long long int lli;

#define ff first
#define ss second
#define pb push_back
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  lli x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)

void zanj0() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void solve() {
  lli n;
  cin >> n;
  vector<lli> v(n);
  for (lli i = 0; i < n; i++) {
    cin >> v[i];
  }
  bool pairwise = true;
  bool setwise = true;
  lli x = v[0];
  for (lli i : v) {
    x = __gcd(x, i);
  }
  setwise = (x == 1);
  unordered_set<lli> visited;
  for (lli i = 0; i < n && pairwise; i++) {
    lli val = v[i];
    lli temp = val;
    for (lli curr = 2; curr * curr <= val && pairwise;
         curr = (curr == 2 ? curr + 1 : curr + 2)) {
      if (temp % curr == 0) {
        if (visited.count(curr)) {
          pairwise = false;
        }
        visited.insert(curr);
        while (temp % curr == 0) {
          temp /= curr;
        }
      }
    }
    if (temp > 1) {
      if (visited.count(temp)) {
        pairwise = false;
      }
      visited.insert(temp);
    }
  }
  if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else if (setwise) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}
int main() {
  zanj0();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

/*
        JAI JAGANNATH!
*/
//@Author : zanj0

#include <bits/stdc++.h>
using namespace std;

typedef long long int lli;

#define ff first
#define ss second
#define pb push_back
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  lli x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)

void zanj0() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void solve() {
  lli n;
  cin >> n;
  vector<lli> v(n);
  for (lli i = 0; i < n; i++) {
    cin >> v[i];
  }
  bool pairwise = true;
  bool setwise = true;
  lli x = v[0];
  for (lli i : v) {
    x = __gcd(x, i);
  }
  setwise = (x == 1);
  unordered_set<lli> visited;
  for (lli i = 0; i < n; i++) {
    lli val = v[i];
    lli temp = val;
    for (lli curr = 2; curr * curr <= val;
         curr = (curr == 2 ? curr + 1 : curr + 2)) {
      if (temp % curr == 0) {
        if (visited.count(curr)) {
          pairwise = false;
        }
        visited.insert(curr);
        while (temp % curr == 0) {
          temp /= curr;
        }
      }
    }
    if (temp > 1) {
      if (visited.count(temp)) {
        pairwise = false;
      }
      visited.insert(temp);
    }
  }
  if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else if (setwise) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}
int main() {
  zanj0();
  solve();
  return 0;
}