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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const ll MOD = 1000000007;
// const ll MOD=998244353;
const long long INF = 1LL << 60;
const double pi = acos(-1.0);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// https://qiita.com/drken/items/a14e9af0ca2d857dad23

// N=1の時に自分で対応しなきゃいけないから注意
vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout << fixed << setprecision(15);

  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  /*
  vector<int> hurui(1000001,1);
  vector<ll> enum_list(0);
  for(int i=2;i<=1000000;i++){
    if(!hurui[i]) continue;
    enum_list.push_back(i);
    for(int j=2*i;j<=1000000;j+=i) hurui[j]=0;
  }
  //for(auto v:enum_list) cout<<v<<endl;
  */
  map<ll, ll> mp;
  for (auto a : A) {
    vector<pair<ll, ll>> vec = prime_factorize(a);
    for (auto v : vec)
      mp[v.first]++;
  }

  string ans = "pairwise coprime";
  for (auto p : mp) {
    if (p.second > 1)
      ans = "setwise coprime";
    if (p.second >= N) {
      ans = "not coprime";
      break;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const ll MOD = 1000000007;
// const ll MOD=998244353;
const long long INF = 1LL << 60;
const double pi = acos(-1.0);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// https://qiita.com/drken/items/a14e9af0ca2d857dad23

// N=1の時に自分で対応しなきゃいけないから注意
vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout << fixed << setprecision(15);

  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  /*
  vector<int> hurui(1000001,1);
  vector<ll> enum_list(0);
  for(int i=2;i<=1000000;i++){
    if(!hurui[i]) continue;
    enum_list.push_back(i);
    for(int j=2*i;j<=1000000;j+=i) hurui[j]=0;
  }
  //for(auto v:enum_list) cout<<v<<endl;
  */
  map<ll, ll> mp;
  for (auto a : A) {
    if (a == 1)
      continue;
    vector<pair<ll, ll>> vec = prime_factorize(a);
    for (auto v : vec)
      if (mp.count(v.first))
        mp[v.first]++;
      else
        mp[v.first] = 1;
  }

  string ans = "pairwise coprime";
  for (auto p : mp) {
    if (p.second > 1)
      ans = "setwise coprime";
    if (p.second >= N) {
      ans = "not coprime";
      break;
    }
  }

  cout << ans << endl;

  return 0;
}