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

#include <algorithm>
#include <assert.h>
#include <bit>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

typedef long double ld;
typedef long long ll;

using namespace std;

/* TEMPLATE ENDS HERE */

#define MAXN (1 << 20)

int p[MAXN];
int a[MAXN];
int n;
int s[MAXN];

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

int main() {
  cout << fixed << setprecision(12);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s[a[i]]++;
  }
  for (int i = 2; i < MAXN; i++) {
    p[i] = 1;
  }
  for (int i = 2; i < MAXN; i++) {
    if (p[i]) {
      for (int j = 2 * i; j < MAXN; j += i) {
        p[j] = 0;
      }
    }
  }
  bool pw = true;
  for (int i = 2; i < MAXN; i++) {
    int cnt = 0;
    if (p[i]) {
      for (int j = i; j < MAXN; j += i) {
        cnt += s[j];
      }
    }
    if (cnt > 1) {
      pw = false;
    }
  }

  int ret = a[0];
  for (int i = 1; i < n; i++) {
    ret = gcd(ret, a[i]);
  }
  bool sw = (ret == 1);

  if (sw && !pw) {
    cout << "setwise coprime" << endl;
  } else if (pw) {
    cout << "pairwise coprime" << endl;
  } else if (!sw && !pw) {
    cout << "not coprime" << endl;
  }

  return 0;
}

ll MOD = ((ll)1e9 + 7);

struct mod {
  ll x;

  mod(ll tx = 0) {
    x = tx;
    x %= MOD;
  }

  mod &operator=(const ll &tx) {
    x = tx % MOD;
    return *this;
  }

  mod &operator=(const mod &m2) {
    x = m2.x;
    return *this;
  }

  mod operator+(const mod &m2) const { return mod((x + m2.x) % MOD); }

  mod &operator+=(const mod &m2) {
    x += m2.x;
    x %= MOD;
    return *this;
  }

  mod operator-(const mod &m2) const { return mod((x - m2.x + MOD) % MOD); }

  mod &operator-=(const mod &m2) {
    x -= m2.x;
    x = (x + MOD) % MOD;
    return *this;
  }

  mod operator*(const mod &m2) const { return mod((x * m2.x) % MOD); }

  mod &operator*=(const mod &m2) {
    x *= m2.x;
    x %= MOD;
    return *this;
  }

  mod mypow(ll k) const {
    mod ret = 1;
    mod cur = x;
    while (k) {
      if (k & 1) {
        ret *= cur;
      }
      cur *= cur;
      k >>= 1;
    }
    return ret;
  }

  mod inv() const { return mypow(MOD - 2); }

  mod operator/(const mod &m2) const { return (*this) * m2.inv(); }

  mod &operator/=(const mod &m2) {
    x *= m2.inv().x;
    x %= MOD;
    return *this;
  }

  bool operator<(const mod &m2) const { return x < m2.x; }
};

ostream &operator<<(ostream &os, const mod &m) { return os << m.x; }


Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bit>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

typedef long double ld;
typedef long long ll;

using namespace std;

/* TEMPLATE ENDS HERE */

#define MAXN (1 << 20)

int p[MAXN];
int a[MAXN];
int n;
multiset<int> s;

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

int main() {
  cout << fixed << setprecision(12);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s.insert(a[i]);
  }
  for (int i = 2; i < MAXN; i++) {
    p[i] = 1;
  }
  for (int i = 2; i < MAXN; i++) {
    if (p[i]) {
      for (int j = 2 * i; j < MAXN; j += i) {
        p[j] = 0;
      }
    }
  }
  bool pw = true;
  for (int i = 2; i < MAXN; i++) {
    int cnt = 0;
    if (p[i]) {
      for (int j = i; j < MAXN; j += i) {
        cnt += s.count(j);
      }
    }
    if (cnt > 1) {
      pw = false;
    }
  }

  int ret = a[0];
  for (int i = 1; i < n; i++) {
    ret = gcd(ret, a[i]);
  }
  bool sw = (ret == 1);

  if (sw && !pw) {
    cout << "setwise coprime" << endl;
  } else if (pw) {
    cout << "pairwise coprime" << endl;
  } else if (!sw && !pw) {
    cout << "not coprime" << endl;
  }

  return 0;
}

ll MOD = ((ll)1e9 + 7);

struct mod {
  ll x;

  mod(ll tx = 0) {
    x = tx;
    x %= MOD;
  }

  mod &operator=(const ll &tx) {
    x = tx % MOD;
    return *this;
  }

  mod &operator=(const mod &m2) {
    x = m2.x;
    return *this;
  }

  mod operator+(const mod &m2) const { return mod((x + m2.x) % MOD); }

  mod &operator+=(const mod &m2) {
    x += m2.x;
    x %= MOD;
    return *this;
  }

  mod operator-(const mod &m2) const { return mod((x - m2.x + MOD) % MOD); }

  mod &operator-=(const mod &m2) {
    x -= m2.x;
    x = (x + MOD) % MOD;
    return *this;
  }

  mod operator*(const mod &m2) const { return mod((x * m2.x) % MOD); }

  mod &operator*=(const mod &m2) {
    x *= m2.x;
    x %= MOD;
    return *this;
  }

  mod mypow(ll k) const {
    mod ret = 1;
    mod cur = x;
    while (k) {
      if (k & 1) {
        ret *= cur;
      }
      cur *= cur;
      k >>= 1;
    }
    return ret;
  }

  mod inv() const { return mypow(MOD - 2); }

  mod operator/(const mod &m2) const { return (*this) * m2.inv(); }

  mod &operator/=(const mod &m2) {
    x *= m2.inv().x;
    x %= MOD;
    return *this;
  }

  bool operator<(const mod &m2) const { return x < m2.x; }
};

ostream &operator<<(ostream &os, const mod &m) { return os << m.x; }