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

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;

#define vec vector<int>
#define vecll vector<ll>
#define vecd vector<double>
#define vecst vector<string>
#define vecb vector<bool>
#define vec2(var, n, m) vector<vector<int>> var(n, vector<int>(m, 0))
#define vecll2(var, n, m) vector<vector<ll>> var(n, vector<ll>(m, 0))

#define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++)
#define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define arr(var, n)                                                            \\
  vec var(n);                                                                  \\
  rep(i, n) { cin >> var[i]; }
#define arrll(var, n)                                                          \\
  vecll var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define arrst(var, n)                                                          \\
  vecst var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define all(var) (var).begin(), (var).end()
#define sortall(var) sort(all(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define f_sum(var) accumulate(all(var), 0)
#define f_sumll(var) accumulate(all(var), 0LL)
#define chmin(v1, v2) v1 = min(v1, v2)
#define chmax(v1, v2) v1 = max(v1, v2)
#define pb(var) push_back(var)
#define prt(var) cout << (var) << "\\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\\n"
#define prtall(v)                                                              \\
  rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\\n"); }
void prtok(bool ok) { prt(ok ? "Yes" : "No"); }
//----------------------------------------------------------------
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

map<ll, ll> prime_factorize(ll n) {
  map<ll, ll> mp;
  while (n % 2 == 0) {
    mp[2]++;
    n /= 2;
  }
  for (ll i = 3; i * i <= n; i += 2) {
    while (n % i == 0) {
      mp[i]++;
      n /= i;
    }
  }
  if (n != 1)
    mp[n] = 1;
  return mp;
}

int main(void) {
  int n;
  cin >> n;
  arr(a, n);

  int tmp = a[0];
  rep(i, n) { tmp = gcd(tmp, a[i]); }
  if (tmp != 1) {
    prt("not coprime");
    return 0;
  }

  map<ll, int> chk;
  rep(i, n) {
    map<ll, ll> plist = prime_factorize(a[i]);
    for (auto k : plist) {
      chk[k.first]++;
    }
  }

  for (auto k : chk) {
    if (k.second > 1) {
      prt("setwise coprime");
      return 0;
    }
  }

  prt("pairwise coprime");
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;

#define vec vector<int>
#define vecll vector<ll>
#define vecd vector<double>
#define vecst vector<string>
#define vecb vector<bool>
#define vec2(var, n, m) vector<vector<int>> var(n, vector<int>(m, 0))
#define vecll2(var, n, m) vector<vector<ll>> var(n, vector<ll>(m, 0))

#define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++)
#define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define arr(var, n)                                                            \\
  vec var(n);                                                                  \\
  rep(i, n) { cin >> var[i]; }
#define arrll(var, n)                                                          \\
  vecll var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define arrst(var, n)                                                          \\
  vecst var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define all(var) (var).begin(), (var).end()
#define sortall(var) sort(all(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define f_sum(var) accumulate(all(var), 0)
#define f_sumll(var) accumulate(all(var), 0LL)
#define chmin(v1, v2) v1 = min(v1, v2)
#define chmax(v1, v2) v1 = max(v1, v2)
#define pb(var) push_back(var)
#define prt(var) cout << (var) << "\\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\\n"
#define prtall(v)                                                              \\
  rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\\n"); }
void prtok(bool ok) { prt(ok ? "Yes" : "No"); }
//----------------------------------------------------------------
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

map<ll, ll> prime_factorize(ll n) {
  map<ll, ll> mp;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      mp[i]++;
      n /= i;
    }
  }
  if (n != 1)
    mp[n] = 1;
  return mp;
}

int main(void) {
  int n;
  cin >> n;
  arr(a, n);

  int tmp = a[0];
  rep(i, n) { tmp = gcd(tmp, a[i]); }
  if (tmp != 1) {
    prt("not coprime");
    return 0;
  }

  map<ll, int> chk;
  rep(i, n) {
    map<ll, ll> plist = prime_factorize(a[i]);
    for (auto k : plist) {
      chk[k.first]++;
    }
  }

  for (auto k : chk) {
    if (k.second > 1) {
      prt("setwise coprime");
      return 0;
    }
  }

  prt("pairwise coprime");
}