Problem p02574 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <string>
#include <vector>

using ll = long long;
ll n;
const ll MAX = 100000000;
std::vector<bool> table(MAX + 1, true);
std::vector<ll> fact;

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

void initTable() {
  table[0] = false;
  table[1] = false;
  for (ll i = 2; i * i <= MAX; i++) {
    if (table[i]) {
      fact.push_back(i);
      for (ll j = i * i; j <= MAX; j += i) {
        table[j] = false;
      }
    }
  }
}

int main() {
  initTable();
  std::cin >> n;
  std::vector<ll> a(n);
  std::cin >> a[0];
  std::map<ll, ll> mp;
  ll g = a[0];
  ll maxCnt = 0, maxMap = 0;
  ll k = fact.size();
  std::vector<ll> cnt(k, 0);
  for (int j = 0; j < k; j++) {
    if (a[0] % fact[j] == 0) {
      a[0] /= fact[j];
      cnt[j]++;
      maxCnt = std::max(maxCnt, cnt[j]);
    }
  }
  if (table[a[0]])
    mp[a[0]]++;

  for (int i = 1; i < n; i++) {
    std::cin >> a[i];
    g = gcd(g, a[i]);
    for (int j = 0; j < k; j++) {
      if (a[i] % fact[j] == 0) {
        a[i] /= fact[j];
        cnt[j]++;
        maxCnt = std::max(maxCnt, cnt[j]);
      }
    }

    if (table[a[i]]) {
      mp[a[i]]++;
      maxMap = std::max(maxMap, mp[a[i]]);
    }
  }

  maxCnt = std::max(maxMap, maxCnt);
  // std::cout << maxCnt << " " << g << std::endl;

  if (maxCnt >= 2 && g > 1) {
    std::cout << "not coprime" << std::endl;
  } else if (maxCnt >= 2 && g == 1) {
    std::cout << "setwise coprime" << std::endl;
  } else {
    std::cout << "pairwise coprime" << std::endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <map>
#include <string>
#include <vector>

using ll = long long;
ll n;
const ll MAX = 1000000;
std::vector<bool> table(MAX + 1, true);
std::vector<ll> fact;

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

void initTable() {
  table[0] = false;
  table[1] = false;
  for (ll i = 2; i * i <= MAX; i++) {
    if (table[i]) {
      fact.push_back(i);
      for (ll j = i * i; j <= MAX; j += i) {
        table[j] = false;
      }
    }
  }
}

int main() {
  initTable();
  std::cin >> n;
  std::vector<ll> a(n);
  std::cin >> a[0];
  std::map<ll, ll> mp;
  ll g = a[0];
  ll maxCnt = 0, maxMap = 0;
  ll k = fact.size();
  std::vector<ll> cnt(k, 0);
  for (int j = 0; j < k; j++) {
    if (a[0] % fact[j] == 0) {
      a[0] /= fact[j];
      cnt[j]++;
      maxCnt = std::max(maxCnt, cnt[j]);
    }
  }
  if (table[a[0]])
    mp[a[0]]++;

  for (int i = 1; i < n; i++) {
    std::cin >> a[i];
    g = gcd(g, a[i]);
    for (int j = 0; j < k; j++) {
      if (a[i] % fact[j] == 0) {
        a[i] /= fact[j];
        cnt[j]++;
        maxCnt = std::max(maxCnt, cnt[j]);
      }
    }

    if (table[a[i]]) {
      mp[a[i]]++;
      maxMap = std::max(maxMap, mp[a[i]]);
    }
  }

  maxCnt = std::max(maxMap, maxCnt);
  // std::cout << maxCnt << " " << g << std::endl;

  if (maxCnt >= 2 && g > 1) {
    std::cout << "not coprime" << std::endl;
  } else if (maxCnt >= 2 && g == 1) {
    std::cout << "setwise coprime" << std::endl;
  } else {
    std::cout << "pairwise coprime" << std::endl;
  }

  return 0;
}