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

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

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

#define MAX 1001000

signed main() {
  int sosuu[MAX] = {0};
  vector<int> pri;

  for (int i = 2; i < sqrt(MAX) + 10; i++) {
    if (sosuu[i] == 0) {
      pri.push_back(i);
      for (int j = i; j < sqrt(MAX) + 10; j += i) {
        sosuu[j] = 1;
      }
    }
  }

  int N;
  int A[MAX];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  // pairwise coprime
  bool f = true;
  set<int> s;
  set<int> s2;
  s.insert(-1);
  for (int i = 0; i < N; i++) {
    int n = A[i];
    for (int j = 0; j < pri.size(); j++) {
      if (pri[j] == -1)
        continue;
      if (n % pri[j] == 0) {
        if (s2.find(pri[j]) != s2.end()) {
          n = -1;
          break;
        }
        while (n % pri[j] == 0)
          n /= pri[j];
        s2.insert(pri[j]);
      }
    }
    // cout<<n<<endl;
    if (n != 1 && s.find(n) != s.end()) {
      f = false;
      break;
    }
    s.insert(n);
  }
  if (f) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  int g = A[0];
  for (int i = 1; i < N; i++) {
    g = gcd(g, A[i]);
  }
  if (g == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }

  cout << "not coprime" << endl;
}


Error: TLE

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

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

#define MAX 1001000

signed main() {
  int sosuu[MAX] = {0};
  vector<int> pri;

  for (int i = 2; i < MAX; i++) {
    if (sosuu[i] == 0) {
      pri.push_back(i);
      for (int j = i; j < MAX; j += i) {
        sosuu[j] = 1;
      }
    }
  }

  int N;
  int A[MAX];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  // pairwise coprime
  bool f = true;
  for (int i = 0; i < N; i++) {
    int n = A[i];
    for (int j = 0; j < pri.size(); j++) {
      if (pri[j] == -1)
        continue;
      if (n % pri[j] == 0) {
        while (n % pri[j] == 0)
          n /= pri[j];
        pri[j] = -1;
      }
    }
    // cout<<n<<endl;
    if (n != 1) {
      f = false;
      break;
    }
  }
  if (f) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  int g = A[0];
  for (int i = 1; i < N; i++) {
    g = gcd(g, A[i]);
  }
  if (g == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }

  cout << "not coprime" << endl;
}