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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
using ll = long long;
const int MOD = 1e9 + 7;
const int INF = 1001001001;

const int A = 1000005;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  bool have_mod = true;
  while (have_mod) {
    ll mod = a % b;
    if (mod == 0)
      have_mod = false;
    a = b;
    b = mod;
  }
  return a;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> c(A);
  rep(i, n) {
    cin >> a[i];
    c[a[i]]++;
  }

  bool pairwise = true;
  for (int i = 2; i < A; i++) {
    int cnt = 0;
    for (int j = i; j < A; j += i) {
      cnt += c[j];
    }
    if (cnt > 1)
      pairwise = false;
  }

  if (pairwise) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  int g = 0;
  rep(i, n) g = gcd(g, a[i]);
  if (g == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\\n'
#define co(ans) cout << ans << endl
#define COYE cout << "YES" << endl
#define COYe cout << "Yes" << endl
#define COye cout << "yes" << endl
#define CONO cout << "NO" << endl
#define CONo cout << "No" << endl
#define COno cout << "no" << endl
#define FORE(i, a) for (auto &i : a)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(), (V).end()
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define EACH(V, i)                                                             \\
  for (typeof((V).begin()) i = (V).begin(); i != (V).end(); ++i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define INF ((1LL << 62) - (1LL << 31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MMOD 998244353
#define MAX 2010101
using namespace std;
using Edge = pair<ll, ll>;
using Graph = vector<vector<int>>;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long long LL;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll N;
  cin >> N;
  ll A[N];
  REP(i, N) cin >> A[i];

  ll g = A[0];
  for (int i = 1; i < N; ++i)
    g = gcd(g, A[i]);

  if (g != 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  map<ll, ll> M;
  REP(i, N) M[A[i]]++;
  for (int i = 2; i <= 1010101; ++i) {
    if (M[i] >= 2) {
      cout << "setwise coprime" << endl;
      return 0;
    }
  }

  for (ll i = 2; i <= 1010101; ++i) {
    ll cnt = 0;

    for (ll j = i; j <= 1010101; j += i)
      cnt += M[j];

    if (cnt >= 2) {
      cout << "setwise coprime" << endl;
      return 0;
    }
  }

  cout << "pairwise coprime" << endl;
  return 0;
}