Problem p02574 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define NDEBUG
#include <bitset>
#include <cassert>
using namespace std;

#define prv(a)                                                                 \
  for (auto x : a) {                                                           \
    cout << x << " ";                                                          \
  }                                                                            \
  cout << '\n';
#define prvp(a)                                                                \
  for (auto x : a) {                                                           \
    cout << x.first << " " << x.second << '\n';                                \
  }                                                                            \
  cout << '\n';
#define ll long long
#define ld long double
#define INF pow(10, 10)
#define all(v) v.begin(), v.end()
#define bg begin()
#define ed end()
#define tr(it, v) for (auto it = v.bg; it != v.ed; it++)
#define f(a, b) for (ll i = a; i < b; i++)
#define fj(a, b) for (ll j = a; j < b; j++)
#define fb(b, a) for (ll i = b; i > a; i--)
#define dout(x) cout << x << '\n';
#define dout2(x, y) cout << x << " " << y << '\n';
#define dout3(x, y, z) cout << x << " " << y << " " << z << '\n';
#define pr1(x) cout << (x) << " ";
#define pr(x) cout << #x << " " << (x) << endl;
#define pr2(x, y) cout << #x << " " << (x) << "  " << #y << " " << (y) << endl;
#define pr3(x, y, z)                                                           \
  cout << #x << " " << (x) << "  " << #y << " " << (y) << "  " << #z << " "    \
       << (z) << endl;
#define pp(x) cout << x.first << " " << x.second << endl;
ll min(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min3(ll a, ll b, ll c) { return min(a, min(b, c)); }
ll max3(ll a, ll b, ll c) { return max(a, max(b, c)); }

ll NC = 100;
ll N = 1e9 + 7;
const ll S = 1e5 + 7;

ll mod(ll n);
ll gcd(ll a, ll b);
ll modM(ll n, ll m);
ll modA(ll n, ll m);
ll modS(ll n, ll m);
ll power(ll a, ll b);
void ipgraph(ll n, ll m);
//==============================================================================================

vector<vector<ll>> v;
vector<bool> vis;
vector<bool> pri; // is_prime
vector<ll> prime;
void printprime(ll n) {
  pri.resize(n + 1, 1);
  pri[0] = 0, pri[1] = 0;
  for (ll i = 2; i * i <= n; i++) {
    if (pri[i])
      for (ll j = i * i; j <= n; j += i)
        pri[j] = 0;
  }
  f(0, n + 1) if (pri[i]) prime.push_back(i);
}

void solve() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  f(0, n) cin >> a[i];
  // prv(prime)
  string p = "pairwise coprime";
  string s = "setwise coprime";
  string none = "not coprime";
  ll hcf = gcd(a[0], a[1]);
  f(2, n) { hcf = gcd(hcf, a[i]); }
  if (hcf > 1) {
    cout << none;
    return;
  }
  printprime(500002);
  // prv(prime)
  unordered_map<ll, ll> mp;
  f(0, n) {
    if (pri[a[i]]) {
      if (mp[a[i]] > 0) {
        cout << s;
        return;
      } else
        mp[a[i]]++;
      continue;
    }
    fj(0, prime.size()) {
      if (prime[j] > a[i])
        break;
      if (a[i] % prime[j] == 0) {
        if (mp[prime[j]] > 0) {
          cout << s;
          return;
        } else
          mp[prime[j]]++;
        while (a[i] % prime[j] == 0)
          a[i] /= prime[j];
      }
    }
  }
  cout << p;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("/Users/aturgupta/Desktop/CP/input.txt", "r", stdin);
  freopen("/Users/aturgupta/Desktop/CP/output.txt", "w", stdout);
#endif

  ll t;
  // cin >> t;
  t = 1;
  while (t--)
    solve();
  cerr << endl
       << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
  return 0;
}

void ipgraph(ll n, ll m) {
  v.assign(n, {});
  vis.assign(n, 0);
  ll x, y;
  f(0, m) {
    cin >> x >> y;
    x--, y--;
    v[y].push_back(x);
    v[x].push_back(y);
  }
}

ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  ll c = power(a, b / 2);
  if (b % 2 == 0)
    return modM(c, c);
  else
    return modM(modM(c, c), a);
}

ll mod(ll n) { return (n % N + N) % N; }
ll modM(ll n, ll m) { return ((n % N * m % N) + N) % N; }
ll modA(ll n, ll m) { return ((n % N + m % N) + N) % N; }
ll modS(ll n, ll m) { return ((n % N - m % N) + N) % N; }


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define NDEBUG
#include <bitset>
#include <cassert>
using namespace std;

#define prv(a)                                                                 \
  for (auto x : a) {                                                           \
    cout << x << " ";                                                          \
  }                                                                            \
  cout << '\n';
#define prvp(a)                                                                \
  for (auto x : a) {                                                           \
    cout << x.first << " " << x.second << '\n';                                \
  }                                                                            \
  cout << '\n';
#define ll long long
#define ld long double
#define INF pow(10, 10)
#define all(v) v.begin(), v.end()
#define bg begin()
#define ed end()
#define tr(it, v) for (auto it = v.bg; it != v.ed; it++)
#define f(a, b) for (ll i = a; i < b; i++)
#define fj(a, b) for (ll j = a; j < b; j++)
#define fb(b, a) for (ll i = b; i > a; i--)
#define dout(x) cout << x << '\n';
#define dout2(x, y) cout << x << " " << y << '\n';
#define dout3(x, y, z) cout << x << " " << y << " " << z << '\n';
#define pr1(x) cout << (x) << " ";
#define pr(x) cout << #x << " " << (x) << endl;
#define pr2(x, y) cout << #x << " " << (x) << "  " << #y << " " << (y) << endl;
#define pr3(x, y, z)                                                           \
  cout << #x << " " << (x) << "  " << #y << " " << (y) << "  " << #z << " "    \
       << (z) << endl;
#define pp(x) cout << x.first << " " << x.second << endl;
ll min(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min3(ll a, ll b, ll c) { return min(a, min(b, c)); }
ll max3(ll a, ll b, ll c) { return max(a, max(b, c)); }

ll NC = 100;
ll N = 1e9 + 7;
const ll S = 1e5 + 7;

ll mod(ll n);
ll gcd(ll a, ll b);
ll modM(ll n, ll m);
ll modA(ll n, ll m);
ll modS(ll n, ll m);
ll power(ll a, ll b);
void ipgraph(ll n, ll m);
//==============================================================================================

vector<vector<ll>> v;
vector<bool> vis;
vector<bool> pri; // is_prime
vector<ll> prime;
void printprime(ll n) {
  pri.resize(n + 1, 1);
  pri[0] = 0, pri[1] = 0;
  for (ll i = 2; i * i <= n; i++) {
    if (pri[i])
      for (ll j = i * i; j <= n; j += i)
        pri[j] = 0;
  }
  f(0, n + 1) if (pri[i]) prime.push_back(i);
}

void solve() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  f(0, n) cin >> a[i];
  // prv(prime)
  string p = "pairwise coprime";
  string s = "setwise coprime";
  string none = "not coprime";
  ll hcf = gcd(a[0], a[1]);
  f(2, n) { hcf = gcd(hcf, a[i]); }
  if (hcf > 1) {
    cout << none;
    return;
  }
  printprime(1000001);
  // prv(prime)
  unordered_map<ll, ll> mp;
  f(0, n) {
    if (pri[a[i]]) {
      if (mp[a[i]] > 0) {
        cout << s;
        return;
      } else
        mp[a[i]]++;
      continue;
    }
    fj(0, prime.size()) {
      if (prime[j] > a[i])
        break;
      if (a[i] % prime[j] == 0) {
        if (mp[prime[j]] > 0) {
          cout << s;
          return;
        } else
          mp[prime[j]]++;
        while (a[i] % prime[j] == 0)
          a[i] /= prime[j];
      }
    }
  }
  cout << p;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("/Users/aturgupta/Desktop/CP/input.txt", "r", stdin);
  freopen("/Users/aturgupta/Desktop/CP/output.txt", "w", stdout);
#endif

  ll t;
  // cin >> t;
  t = 1;
  while (t--)
    solve();
  cerr << endl
       << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
  return 0;
}

void ipgraph(ll n, ll m) {
  v.assign(n, {});
  vis.assign(n, 0);
  ll x, y;
  f(0, m) {
    cin >> x >> y;
    x--, y--;
    v[y].push_back(x);
    v[x].push_back(y);
  }
}

ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  ll c = power(a, b / 2);
  if (b % 2 == 0)
    return modM(c, c);
  else
    return modM(modM(c, c), a);
}

ll mod(ll n) { return (n % N + N) % N; }
ll modM(ll n, ll m) { return ((n % N * m % N) + N) % N; }
ll modA(ll n, ll m) { return ((n % N + m % N) + N) % N; }
ll modS(ll n, ll m) { return ((n % N - m % N) + N) % N; }