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

#include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
#define ALL(a) a.begin(), a.end()
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<int, int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
} // GCD

int l[1000005];

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  bool pairwise = 1;
  bool setwise = 1;
  ll now = 1;
  rep(i, n) {
    cin >> a[i];

    if (i == 0) {
      now = a[i];
    } else {
      now = gcd(now, a[i]);
    }

    int b = a[i];
    int temp = 0;

    int d = 2;
    while (b % d == 0) {
      temp++;
      b /= d;
    }
    if (l[d] > 0 && temp > 0)
      pairwise = 0;
    l[d] += temp;
    temp = 0;
    for (d = 3; d <= b && b > 1 && d <= sqrt(a[i]) + 1; d += 2) {
      while (b % d == 0) {
        temp++;
        b /= d;
      }
      if (l[d] > 0 && temp > 0)
        pairwise = 0;
      l[d] += temp;
      temp = 0;
    }
    if (b > 1) {
      if (l[b] > 0)
        pairwise = 0;
      l[b]++;
    }
  }
  if (now > 1) {
    setwise = 0;
  }
  if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else if (setwise) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
#define ALL(a) a.begin(), a.end()
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<int, int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
} // GCD

int l[1000005];

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  bool pairwise = 1;
  bool setwise = 1;
  ll now = 0;
  rep(i, n) {
    cin >> a[i];
    if (i == 0) {
      now = a[i];
    } else {
      now = gcd(now, a[i]);
    }
    int b = a[i];
    int temp = 0;

    int d = 2;
    while (b % d == 0) {
      temp++;
      b /= d;
    }
    if (l[d] > 0 && temp > 0)
      pairwise = 0;
    l[d] += temp;
    temp = 0;
    for (d = 3; d <= b && b > 1; d += 2) {
      while (b % d == 0) {
        temp++;
        b /= d;
      }
      if (l[d] > 0 && temp > 0)
        pairwise = 0;
      l[d] += temp;
      temp = 0;
    }
  }
  if (now > 1) {
    setwise = 0;
  }
  if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else if (setwise) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
  return 0;
}