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

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

typedef long long ll;
typedef pair<int, int> pii;

long long MOD = 10000000000 + 7;

template <typename T> map<T, int> prime_factorize(T x) {
  map<T, int> res;

  while (x % 2 == 0) {
    x /= 2;
    res[2]++;
  }

  for (int i = 3; i * i <= x; i += 2) {
    while (x % i == 0) {
      x /= i;
      res[i]++;
    }
  }
  if (x != 1)
    res[x]++;
  return res;
}

map<int, int> merge_map(map<int, int> m1, map<int, int> m2) {
  map<int, int> m;
  for (auto x : m1) {
    m[x.first] = 1;
  }
  for (auto x : m2) {
    m[x.first] = 1;
  }
  return m;
}

template <typename T> T gcd(T a, T b) {
  if (a < b)
    swap(a, b);

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

int main() {
  cout << setprecision(10);
  int N;
  cin >> N;
  vector<int> A;

  map<int, int> m;
  bool pairwise_flg = true;
  bool setwise_flg = true;
  int gcd_val;

  int one_count = 0;

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    if (tmp == 1) {
      one_count++;
    } else {
      A.push_back(tmp);
    }
  }

  if (one_count == N) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  N = A.size();

  for (int i = 0; i < N; i++) {
    if (i == 0) {
      m = prime_factorize(A[i]);
    } else {
      map<int, int> m_tmp1, m_tmp2;
      m_tmp1 = prime_factorize(A[i]);
      int before = m.size();
      m.insert(m_tmp1.begin(), m_tmp1.end());
      if (m.size() == m_tmp1.size() + before) {
        // OK
      } else {
        pairwise_flg = false;
        break;
      }
    }
  }

  for (int i = 0; i < N; i++) {
    if (i == 0) {
      gcd_val = A[i];
    } else {
      // cerr << i << " " << A[i] << " " << gcd_val << endl;
      gcd_val = gcd(gcd_val, A[i]);
    }
  }
  // cerr << "gcd_val: " << gcd_val << endl;

  setwise_flg = (gcd_val == 1);
  if (one_count > 0) {
    setwise_flg = true;
  }

  if (pairwise_flg) {
    cout << "pairwise coprime" << endl;
  } else if (setwise_flg) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;

long long MOD = 10000000000 + 7;

template <typename T> map<T, int> prime_factorize(T x) {
  map<T, int> res;

  while (x % 2 == 0) {
    x /= 2;
    res[2]++;
  }

  for (int i = 3; i * i <= x; i += 2) {
    while (x % i == 0) {
      x /= i;
      res[i]++;
    }
  }
  if (x != 1)
    res[x]++;
  return res;
}

map<int, int> merge_map(map<int, int> m1, map<int, int> m2) {
  map<int, int> m;
  for (auto x : m1) {
    m[x.first] = 1;
  }
  for (auto x : m2) {
    m[x.first] = 1;
  }
  return m;
}

template <typename T> T gcd(T a, T b) {
  if (a < b)
    swap(a, b);

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

int main() {
  cout << setprecision(10);
  int N;
  cin >> N;
  vector<int> A;

  map<int, int> m;
  bool pairwise_flg = true;
  bool setwise_flg = true;
  int gcd_val;

  int one_count = 0;

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    if (tmp == 1) {
      one_count++;
    } else {
      A.push_back(tmp);
    }
  }

  if (one_count == N) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  N = A.size();

  for (int i = 0; i < N; i++) {
    if (i == 0) {
      m = prime_factorize(A[i]);
    } else {
      map<int, int> m_tmp1, m_tmp2;
      m_tmp1 = prime_factorize(A[i]);
      m_tmp2 = merge_map(m, m_tmp1);
      if (m.size() + m_tmp1.size() == m_tmp2.size()) {
        // OK
      } else {
        pairwise_flg = false;
        break;
      }
      m = m_tmp2;
    }
  }

  for (int i = 0; i < N; i++) {
    if (i == 0) {
      gcd_val = A[i];
    } else {
      // cerr << i << " " << A[i] << " " << gcd_val << endl;
      gcd_val = gcd(gcd_val, A[i]);
    }
  }
  // cerr << "gcd_val: " << gcd_val << endl;

  setwise_flg = (gcd_val == 1);
  if (one_count > 0) {
    setwise_flg = true;
  }

  if (pairwise_flg) {
    cout << "pairwise coprime" << endl;
  } else if (setwise_flg) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}