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

#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define nl '\\n'
#define print(vec, l, r)                                                       \\
  for (int i = (l); i < (r); i++)                                              \\
    cout << vec[i] << " ";                                                     \\
  cout << endl;
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define forr(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define input(vec, N)                                                          \\
  for (int i = 0; i < (N); i++)                                                \\
    cin >> vec[i];
#define debug(x) cerr << #x << " = " << (x) << endl;

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

const int SIZE = 1e6 + 1;

void sieve(vi &isprime) {
  for (int i = 2; i < SIZE; i++) {
    if (isprime[i] != 0)
      continue;
    for (int j = i; j < SIZE; j += i) {
      if (isprime[j] == 0)
        isprime[j] = i;
    }
  }
}

void solve() {
  vector<int> isprime(SIZE);
  sieve(isprime);
  int n;
  cin >> n;
  map<int, int> count;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    while (temp != 1) {
      int t = isprime[temp];
      count[t]++;
      while (temp % t == 0)
        temp /= t;
    }
  }
  bool pair = true, set = true;
  for (auto it : count) {
    if (it.S > 1)
      pair = false;
    if (it.S == n)
      set = false;
  }
  if (pair)
    cout << "pairwise coprime\\n";
  else if (set)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define nl '\\n'
#define print(vec, l, r)                                                       \\
  for (int i = (l); i < (r); i++)                                              \\
    cout << vec[i] << " ";                                                     \\
  cout << endl;
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define forr(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define input(vec, N)                                                          \\
  for (int i = 0; i < (N); i++)                                                \\
    cin >> vec[i];
#define debug(x) cerr << #x << " = " << (x) << endl;

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

const int SIZE = 1e6 + 1;

void sieve(vector<bool> &isprime, vi &primes) {
  isprime[0] = false;
  isprime[1] = false;
  for (int i = 2; i < SIZE; i++) {
    if (!isprime[i])
      continue;
    primes.pb(i);
    for (int j = 2 * i; j < SIZE; j += i) {
      isprime[j] = false;
    }
  }
}

void solve() {
  vector<bool> isprime(SIZE, true);
  vi primes;
  sieve(isprime, primes);
  int n;
  int m = primes.size();
  cin >> n;
  map<int, int> count;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    for (int j = 0; j < m; j++) {
      if (isprime[temp]) {
        count[temp]++;
        break;
      }
      if (temp % primes[j] == 0) {
        count[primes[j]]++;
        while (temp % primes[j] == 0)
          temp /= primes[j];
      }
    }
  }
  bool pair = true, set = true;
  for (int j = 0; j < m; j++) {
    if (count[primes[j]] > 1)
      pair = false;
    if (count[primes[j]] == n)
      set = false;
  }
  if (pair)
    cout << "pairwise coprime\\n";
  else if (set)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}