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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define itn int
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; i--)
#define all(vec) vec.begin(), vec.end()
#define sortt(vec) sort((vec).begin(), (vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long double ld;
const int mod = 1e9 + 7;
const int inf = 2e9;

map<int, int> pfmp; // 各素因数の冪指数の総和を格納
vector<int> pfv(0); // 異なる素因数を昇順に格納

bool f1;
void pf(int N) {
  int n = N;
  if (n <= 0)
    return;
  if (n % 2 == 0) {
    if (pfmp[2] == 0)
      pfv.push_back(2);
    else
      f1 = 0;
    while (n % 2 == 0) {
      n /= 2;
      pfmp[2]++;
    }
  }
  if (n == 1)
    return;
  for (int i = 3; i <= sqrt(N); i += 2) {
    if (n % i == 0) {
      if (pfmp[i] == 0)
        pfv.push_back(i);
      else
        f1 = 0;
      while (n % i == 0) {
        n /= i;
        pfmp[i]++;
      }
    }
  }
  if (n != 1) {
    if (pfmp[n] == 0)
      pfv.push_back(n);
    else
      f1 = 0;
    pfmp[n]++;
  }
  // sort(all(pfv));
}

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

int main() {
  itn n;
  cin >> n;
  f1 = 1;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n) pf(a[i]);
  if (f1) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  int GCD = a[0];
  rep(i, n - 1) { GCD = gcd(GCD, a[i + 1]); }
  if (GCD == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }
  cout << "not coprime" << endl;
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define itn int
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; i--)
#define all(vec) vec.begin(), vec.end()
#define sortt(vec) sort((vec).begin(), (vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 2e9;
const ll lnf = 9e18;

map<ll, ll> pfmp;  // 各素因数の冪指数の総和を格納
vector<ll> pfv(0); // 異なる素因数を昇順に格納

bool f1;
void pf(ll N) {
  ll n = N;
  if (n <= 0)
    return;
  if (n % 2 == 0) {
    if (pfmp[2] == 0)
      pfv.push_back(2);
    else
      f1 = 0;
    while (n % 2 == 0) {
      n /= 2;
      pfmp[2]++;
    }
  }
  if (n == 1)
    return;
  for (ll i = 3; i <= sqrt(N); i += 2) {
    if (n % i == 0) {
      if (pfmp[i] == 0)
        pfv.push_back(i);
      else
        f1 = 0;
      while (n % i == 0) {
        n /= i;
        pfmp[i]++;
      }
    }
  }
  if (n != 1) {
    if (pfmp[n] == 0)
      pfv.push_back(n);
    else
      f1 = 0;
    pfmp[n]++;
  }
  // sort(all(pfv));
}

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

int main() {
  itn n;
  cin >> n;
  f1 = 1;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n) pf(a[i]);
  if (f1) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  ll GCD = a[0];
  rep(i, n - 1) { GCD = gcd(GCD, a[i + 1]); }
  if (GCD == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }
  cout << "not coprime" << endl;
}