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

//
// AtCoder Beginner Contest 177
// E
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

const int MAX_A = 1e6 + 10;

vector<bool> primeTable(ll n) {
  vector<bool> prime(n + 1, true);
  if (n >= 0)
    prime[0] = false;
  if (n >= 1)
    prime[1] = false;
  for (ll i = 2; i * i <= n; i++) {
    if (!prime[i])
      continue;
    for (ll j = i + i; j <= n; j += i)
      prime[j] = false;
  }
  return prime;
}

int main() {
  int n;
  cin >> n;

  vector<int> a(n);
  vector<int> cntTbl(MAX_A, 0);
  REP(i, n) {
    cin >> a[i];
    cntTbl[a[i]]++;
  }
  vector<bool> pTbl = primeTable(MAX_A);

  bool pairwise = true;
  FOR(i, 2, MAX_A) {
    if (pTbl[i] == false)
      continue;
    int cnt = 0;
    for (int j = i; j < MAX_A; j += i) {
      cnt += cntTbl[j];
    }
    if (cnt > 1) {
      pairwise = false;
      break;
    }
  }
  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:

//
// AtCoder Beginner Contest 177
// E
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

int main() {
  int n;
  cin >> n;

  vector<ll> a(n);
  REP(i, n) cin >> a[i];

  ll setwise = gcd(a[0], a[1]);
  REP(i, n) {
    setwise = gcd(setwise, a[i]);
    if (setwise == 1)
      break;
  }

  bool pairwise = true;
  REP(i, n) {
    FOR(j, i + 1, n - 1) {
      if (gcd(a[i], a[j]) != 1) {
        pairwise = false;
        break;
      }
    }
    if (!pairwise)
      break;
  }
  if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else {
    if (setwise == 1)
      cout << "setwise coprime" << endl;
    else
      cout << "not coprime" << endl;
  }
  return 0;
}