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

#include "bits/stdc++.h"
using namespace std;

// 定義
typedef long long ll;
typedef pair<ll, ll> P;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll i = 0; i < (ll)n; ++i)
#define REPN(i, m, n) for (ll i = m; i < (ll)n; ++i)
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VVVL vector<vector<vector<ll>>>
#define VC vector<char>
#define VVC vector<vector<char>>
#define INF (ll)2e9
#define INF_LL 1LL << 60
// #define MOD             998244353
#define MOD 1000000007

ll Ceil(ll val, ll div) { return (val + div - 1) / div; }
ll CeilN(ll val, ll div) { return Ceil(val, div) * div; }
ll FloorN(ll x, ll n) { return (x - x % n); }
bool IsOdd(ll x) { return ((x & 1) == 1); }
bool IsEven(ll x) { return ((x & 1) == 0); }
template <class T> bool chmax(T &a, T b) {
  if (a <= b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a >= b) {
    a = b;
    return 1;
  }
  return 0;
}

/* 最大公約数 */
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

/* 最小公倍数 */
ll lcm(ll a, ll b) {
  ll gcdRes = gcd(a, b);
  return (a * (b / gcdRes));
}

/* 素数判定 */
bool isPrime(ll p) {
  for (ll i = 2; i <= sqrt(p); i++)
    if (p % i == 0)
      return false;
  return p > 1;
}

/* 素因数分解 */
void primeFactor(ll n, map<ll, ll> &factMap) {
  if ((n % 2) == 0) {
    while ((n % 2) == 0) {
      n = n / 2;
    }
    factMap[2]++;
  }

  VL isFind(1000005, 0);
  for (ll i = 3; i <= sqrt(n); i += 2) {
    while ((n % i) == 0) {
      if (!isFind[n]) {
        isFind[n] = true;
        factMap[i]++;
      }
      n = n / i;
    }
  }

  if (n != 1) {
    if (!isFind[n]) {
      factMap[n] = 1;
    }
  }
}

void Solve() {
  ll N;
  cin >> N;
  VL A(N);
  VL val(1000005, 0);
  REP(i, N) {
    cin >> A[i];
    val[A[i]]++;
  }
  sort(ALL(A));

  ll ans = 0;
  ll maxVal = A.back();
  VL isFin(1000005, 0);
  for (ll a = 2; a <= maxVal; a++) {
    if (isFin[a]) {
      continue;
    }
    ll ansOne = 0;
    ll nowVal = a;
    while (nowVal <= maxVal) {
      ansOne += val[nowVal];
      isFin[nowVal] = 1;
      nowVal += a;
    }
    ans = max(ansOne, ans);
  }

  if (ans == N) {
    cout << "not coprime" << endl;
  } else {
    if (1 < ans) {
      cout << "setwise coprime" << endl;
    } else {
      cout << "pairwise coprime" << endl;
    }
  }
  return;
}

// メイン
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Solve();
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

// 定義
typedef long long ll;
typedef pair<ll, ll> P;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll i = 0; i < (ll)n; ++i)
#define REPN(i, m, n) for (ll i = m; i < (ll)n; ++i)
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VVVL vector<vector<vector<ll>>>
#define VC vector<char>
#define VVC vector<vector<char>>
#define INF (ll)2e9
#define INF_LL 1LL << 60
// #define MOD             998244353
#define MOD 1000000007

ll Ceil(ll val, ll div) { return (val + div - 1) / div; }
ll CeilN(ll val, ll div) { return Ceil(val, div) * div; }
ll FloorN(ll x, ll n) { return (x - x % n); }
bool IsOdd(ll x) { return ((x & 1) == 1); }
bool IsEven(ll x) { return ((x & 1) == 0); }
template <class T> bool chmax(T &a, T b) {
  if (a <= b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a >= b) {
    a = b;
    return 1;
  }
  return 0;
}

/* 最大公約数 */
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

/* 最小公倍数 */
ll lcm(ll a, ll b) {
  ll gcdRes = gcd(a, b);
  return (a * (b / gcdRes));
}

/* 素数判定 */
bool isPrime(ll p) {
  for (ll i = 2; i <= sqrt(p); i++)
    if (p % i == 0)
      return false;
  return p > 1;
}

/* 素因数分解 */
map<ll, ll> factMap;
void primeFactor(ll n) {
  if ((n % 2) == 0) {
    while ((n % 2) == 0) {
      n = n / 2;
    }
    factMap[2]++;
  }

  VL isFind(1000005, 0);
  for (ll i = 3; i <= sqrt(n); i += 2) {
    while ((n % i) == 0) {
      if (!isFind[n]) {
        isFind[n] = true;
        factMap[i]++;
      }
      n = n / i;
    }
  }

  if (n != 1) {
    if (!isFind[n]) {
      factMap[n] = 1;
    }
  }
}

void Solve() {
  ll N;
  cin >> N;
  VL A(N);
  REP(i, N) cin >> A[i];

  REP(i, N) { primeFactor(A[i]); }

  ll maxVal = 0;
  for (auto one : factMap) {
    maxVal = max(maxVal, one.second);
  }

  if (maxVal == N) {
    cout << "not coprime" << endl;
  } else {
    if (1 < maxVal) {
      cout << "setwise coprime" << endl;
    } else {
      cout << "pairwise coprime" << endl;
    }
  }
  return;
}

// メイン
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Solve();
  return 0;
}