Problem p02574 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define rep(i, a) for (int i = 0; i < (a); i++)
#define repd(i, a) for (int i = (a)-1; i >= 0; i--)
#define reps(i, a, j) for (int i = (j); i < (a); i++)
#define deb(x) cout << #x << " " << x << endl;
#define debs(x) cout << #x << " " << x << " ";
#define deba(x)                                                                \
  for (auto _i : x)                                                            \
    cout << _i << " ";                                                         \
  cout << endl;
#define ms(a) memset(a, 0, sizeof(a));
#define mp make_pair
#define eb emplace_back
#define pii pair<int, int>
#define all(a) a.begin(), a.end()

int gcd(int x, int y) {
  int t;
  while (x % y)
    t = x, x = y, y = t % y;
  return y;
}

const int N = 1e6 + 1;
bool prime[N + 10];
vector<int> k;
bool present[N + 10];

void sieve() {
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= N; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= N; i += p) {
        prime[i] = false;
      }
    }
  }
  for (int p = 2; p <= N; p++)
    if (prime[p])
      k.eb(p);
}

bool pairwisecoprime(int arr[], int n) {
  repd(i, n) {
    int val = arr[i];
    if (present[val] == true && val != 1) {
      return false;
    }
    for (auto x : k) {
      if (val % x == 0) {
        if (present[x] == true)
          return false;
        present[x] = true;
        while (val % x == 0) {
          present[val] = true;
          val /= x;
        }
      }
      if (val == 1)
        break;
    }
  }
  return true;
}

bool setwisecoprime(int arr[], int n) {
  int g = arr[0];
  reps(i, n, 1) {
    g = gcd(g, arr[i]);
    if (g == 1)
      break;
  }
  if (g == 1)
    return true;
  else
    return false;
}

void solve() {
  sieve();
  int n;
  cin >> n;
  int arr[n];
  rep(i, n) cin >> arr[i];
  if (pairwisecoprime(arr, n)) {
    cout << "pairwise coprime" << endl;
  } else if (setwisecoprime(arr, n)) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  t = 1;
  while (t--)
    solve();
}

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

// #define int long long
#define rep(i, a) for (int i = 0; i < (a); i++)
#define repd(i, a) for (int i = (a)-1; i >= 0; i--)
#define reps(i, a, j) for (int i = (j); i < (a); i++)
#define deb(x) cout << #x << " " << x << endl;
#define debs(x) cout << #x << " " << x << " ";
#define deba(x)                                                                \
  for (auto _i : x)                                                            \
    cout << _i << " ";                                                         \
  cout << endl;
#define ms(a) memset(a, 0, sizeof(a));
#define mp make_pair
#define eb emplace_back
#define pii pair<int, int>
#define all(a) a.begin(), a.end()

int gcd(int x, int y) {
  int t;
  while (x % y)
    t = x, x = y, y = t % y;
  return y;
}

const int N = 1e6 + 1;
bool prime[N + 10];
vector<int> k;
bool present[N + 10];

void sieve() {
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= N; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= N; i += p) {
        prime[i] = false;
      }
    }
  }
  for (int p = 2; p <= N; p++)
    if (prime[p])
      k.eb(p);
}

bool pairwisecoprime(int arr[], int n) {
  repd(i, n) {
    int val = arr[i];
    if (present[val] == true && val != 1) {
      return false;
    }
    for (auto x : k) {
      if (prime[val] == true) {
        if (present[val] == true) {
          return false;
        } else {
          if (val != 1)
            present[val] = true;
          break;
        }
      }
      if (val % x == 0) {
        if (present[x] == true)
          return false;
        present[x] = true;
        while (val % x == 0) {
          present[val] = true;
          val /= x;
        }
      }
      if (val == 1)
        break;
    }
  }
  return true;
}

bool setwisecoprime(int arr[], int n) {
  int g = arr[0];
  reps(i, n, 1) {
    g = gcd(g, arr[i]);
    if (g == 1)
      break;
  }
  if (g == 1)
    return true;
  else
    return false;
}

void solve() {
  sieve();
  int n;
  cin >> n;
  int arr[n];
  rep(i, n) cin >> arr[i];
  if (pairwisecoprime(arr, n)) {
    cout << "pairwise coprime" << endl;
  } else if (setwisecoprime(arr, n)) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  t = 1;
  while (t--)
    solve();
}