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

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

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

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

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T, class U> istream &operator>>(istream &is, P<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v) {
    is >> e;
  }
  return is;
}

int main() {
  vector<int> plist;
  bitset<1000001> ppos;
  for (int i = 2; i <= 1000000; ++i) {
    bool f = true;
    for (auto e : plist) {
      if (i % e == 0) {
        f = false;
        break;
      }
      if (e * e > i)
        break;
    }
    if (f) {
      plist.push_back(i);
      ppos[i] = 1;
    }
  }
  int n;
  cin >> n;
  vector<int> v(n);
  cin >> v;
  map<int, int> mp;
  for (auto e : v) {
    for (auto plime : plist) {
      if (e == 1)
        break;
      if (ppos[e]) {
        mp[e]++;
        break;
      }
      if (e % plime == 0)
        mp[plime]++;
      while (e % plime == 0)
        e /= plime;
    }
  }
  int cnt1 = 0, cnt0 = 0, maxn = 0;
  for (auto e : mp) {
    cnt1 += e.second == 1;
    maxn = max(maxn, e.second);
    cnt0 += e.second == 0;
  }
  if (cnt1 + cnt0 == (int)mp.size())
    cout << "pairwise coprime" << endl;
  else if (maxn != n)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

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

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

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T, class U> istream &operator>>(istream &is, P<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v) {
    is >> e;
  }
  return is;
}

int main() {
  vector<int> plist;
  bitset<1000001> ppos;
  for (int i = 2; i <= 1000000; ++i) {
    bool f = true;
    for (auto e : plist) {
      if (i % e == 0) {
        f = false;
        break;
      }
      if (e * e > i)
        break;
    }
    if (f) {
      plist.push_back(i);
      ppos[i] = 1;
    }
  }
  int n;
  cin >> n;
  vector<int> v(n);
  cin >> v;
  map<int, int> mp;
  for (auto e : v) {
    for (auto plime : plist) {
      if (ppos[e]) {
        mp[e]++;
        break;
      }
      if (e % plime == 0)
        mp[plime]++;
      while (e % plime == 0)
        e /= plime;
    }
  }
  int cnt1 = 0, cnt0 = 0, maxn = 0;
  for (auto e : mp) {
    cnt1 += e.second == 1;
    maxn = max(maxn, e.second);
    cnt0 += e.second == 0;
  }
  if (cnt1 + cnt0 == (int)mp.size())
    cout << "pairwise coprime" << endl;
  else if (maxn != n)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  return 0;
}