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

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a - 1; i >= b; i--)
#define iter(m) for (auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for (auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed                                                                  \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int main() {
  speed;
  int n;
  cin >> n;
  vi v(n);
  unordered_map<int, int> m;
  bool coprime = true;
  rep(i, 0, n) {
    cin >> v[i];
    int k = v[i];
    unordered_map<int, int> mm;
    for (int j = 2; j * j <= k; j++) {
      if (k % j == 0) {
        while (k % j == 0) {
          k /= j;
          mm[j]++;
        }
      }
    }
    if (k != 1)
      mm[k]++;
    for (auto it = mm.begin(); it != mm.end(); it++) {
      if (m[it->first] != 0) {
        coprime = false;
      } else {
        m[it->first] += it->second;
      }
    }
  }
  if (coprime) {
    cout << "pairwise coprime";
  } else {
    int res = v[0];
    for (int i = 1; i < n; i++) {
      res = __gcd(res, v[i]);
    }
    if (res == 1) {
      cout << "setwise coprime";
    } else {
      cout << "not coprime";
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define int long long
#define vi vector<int>
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a - 1; i >= b; i--)
#define iter(m) for (auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for (auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed                                                                  \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int mod = (int)1e9 + 7;
signed main() {
  int n;
  cin >> n;
  vi v(n);
  map<int, int> m;
  bool coprime = true;
  rep(i, 0, n) {
    cin >> v[i];
    int k = v[i];
    map<int, int> mm;
    for (int j = 2; j * j <= k; j++) {
      if (k % j == 0) {
        while (k % j == 0) {
          k /= j;
          mm[j]++;
        }
      }
    }
    if (k != 1)
      mm[k]++;
    for (auto it = mm.begin(); it != mm.end(); it++) {
      if (m[it->first] != 0) {
        coprime = false;
      } else {
        m[it->first] += it->second;
      }
    }
  }
  if (coprime) {
    cout << "pairwise coprime";
  } else {
    int res = v[0];
    for (int i = 1; i < n; i++) {
      res = __gcd(res, v[i]);
    }
    if (res == 1) {
      cout << "setwise coprime";
    } else {
      cout << "not coprime";
    }
  }
}